Skip to content

Commit

Permalink
created two helper methods to retrieve adjacent and cross cells, upda…
Browse files Browse the repository at this point in the history
…ted dependencies
  • Loading branch information
ninetailsrabbit committed Jul 15, 2024
1 parent 4ced36e commit 863db00
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
38 changes: 38 additions & 0 deletions Match3Maker.Tests/tests/src/components/BoardTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,44 @@ public void Should_Assign_Neighbours_On_Prepared_Grid_Cells() {
Assert.Null(bottomLeftCornerCell.DiagonalNeighbourBottomRight);
}

[Fact]
public void Should_Retrieve_Adjacent_Cells_From_Origin_One_With_Diagonals_As_Option() {
var board = new Board(5, 6, 10, _mockPieceSelector.Object, _mockSequenceFinder.Object);

board.PrepareGridCells();

var originCell = board.TopLeftCornerCell();
var adjacentCells = board.AdjacentCellsFrom(originCell);

Check failure on line 258 in Match3Maker.Tests/tests/src/components/BoardTests.cs

View workflow job for this annotation

GitHub Actions / build

Member 'Board.AdjacentCellsFrom(GridCell, bool)' cannot be accessed with an instance reference; qualify it with a type name instead

Assert.Equal(2, adjacentCells.Count);

foreach (var cell in adjacentCells) {
Assert.True(cell.IsColumnNeighbourOf(originCell) || cell.IsRowNeighbourOf(originCell));
}

adjacentCells = board.AdjacentCellsFrom(originCell, true);

Check failure on line 266 in Match3Maker.Tests/tests/src/components/BoardTests.cs

View workflow job for this annotation

GitHub Actions / build

Member 'Board.AdjacentCellsFrom(GridCell, bool)' cannot be accessed with an instance reference; qualify it with a type name instead

Assert.Equal(3, adjacentCells.Count);
Assert.Contains(originCell.DiagonalNeighbourBottomRight, adjacentCells);

originCell = board.Cell(1, 1);
adjacentCells = board.AdjacentCellsFrom(originCell);

Check failure on line 272 in Match3Maker.Tests/tests/src/components/BoardTests.cs

View workflow job for this annotation

GitHub Actions / build

Member 'Board.AdjacentCellsFrom(GridCell, bool)' cannot be accessed with an instance reference; qualify it with a type name instead

Assert.Equal(4, adjacentCells.Count);

foreach (var cell in adjacentCells) {
Assert.True(cell.IsColumnNeighbourOf(originCell) || cell.IsRowNeighbourOf(originCell));
}

adjacentCells = board.AdjacentCellsFrom(originCell, true);

Check failure on line 280 in Match3Maker.Tests/tests/src/components/BoardTests.cs

View workflow job for this annotation

GitHub Actions / build

Member 'Board.AdjacentCellsFrom(GridCell, bool)' cannot be accessed with an instance reference; qualify it with a type name instead

Assert.Equal(4 * 2, adjacentCells.Count);
Assert.Contains(originCell.DiagonalNeighbourBottomLeft, adjacentCells);
Assert.Contains(originCell.DiagonalNeighbourBottomRight, adjacentCells);
Assert.Contains(originCell.DiagonalNeighbourTopLeft, adjacentCells);
Assert.Contains(originCell.DiagonalNeighbourTopRight, adjacentCells);
}

[Fact]
public void Should_Detect_Border_And_Corners() {
var board = new Board(8, 7, 10, _mockPieceSelector.Object, _mockSequenceFinder.Object);
Expand Down
4 changes: 2 additions & 2 deletions Match3Maker/Match3Maker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<PropertyGroup>
<Title>Ninetailsrabbit.Match3Maker</Title>
<Version>1.2.0</Version>
<Version>1.2.2</Version>
<Description>This lightweight library provides the core logic and functionality you need to build engaging match-3 games. Focus on game design and mechanics while leaving the complex logic to this library</Description>
<Copyright>© 2024 Ninetailsrabbit</Copyright>
<Authors>Ninetailsrabbit</Authors>
Expand All @@ -39,7 +39,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Ninetailsrabbit.Extensionator" Version="0.1.0" />
<PackageReference Include="Ninetailsrabbit.Extensionator" Version="0.2.2" />
</ItemGroup>

</Project>
41 changes: 33 additions & 8 deletions Match3Maker/src/components/Board.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Numerics;
using Extensionator;
using Extensionator;
using System.Numerics;
using static Match3Maker.BoardCellUpdate;

namespace Match3Maker {
Expand All @@ -22,7 +22,7 @@ public VirtualBoard(List<List<GridCell>> gridCells) {

public GridCell? Cell(int column, int row) => GridCells.FirstOrDefault(cell => cell.Column.Equals(column) && cell.Row.Equals(row));
public List<GridCell> EmptyCells() => GridCells.Where(cell => cell.IsEmpty() && cell.CanContainPiece).ToList();
public void AddUpdate(BoardCellUpdate update) {
public void Update(BoardCellUpdate update) {
Updates.Add(update);
}
public List<BoardCellUpdate> MovementUpdates() => Updates.Where(update => update.CurrentUpdateType.Equals(UPDATE_TYPE.MOVEMENT)).ToList();
Expand Down Expand Up @@ -220,7 +220,6 @@ public void Unlock() {
#endregion

#region Cells

public GridCell? Cell(Vector2 position) => Cell((int)position.Y, ((int)position.X));
public GridCell? Cell(int column, int row) {
if (GridCells.Count > 0
Expand All @@ -233,8 +232,7 @@ public void Unlock() {
return null;
}

public Vector2 CellPosition(GridCell cell)
=> new(CellSize.X * cell.Column + Offset.X, CellSize.Y * cell.Row + Offset.Y);
public Vector2 CellPosition(GridCell cell) => new(CellSize.X * cell.Column + Offset.X, CellSize.Y * cell.Row + Offset.Y);

public Board PrepareGridCells(List<Vector2>? disabledCells = null, bool overwrite = false) {
if (GridCells.IsEmpty() || overwrite) {
Expand Down Expand Up @@ -499,6 +497,33 @@ public List<GridCell> CellsWithShape(string shape) {
.ToList();
}

public static List<GridCell> AdjacentCellsFrom(GridCell originCell, bool includeDiagonals = false) {
#pragma warning disable 8601
List<GridCell> adjacentCells = [
originCell.NeighbourUp,
originCell.NeighbourRight,
originCell.NeighbourLeft,
originCell.NeighbourBottom,
];

if (includeDiagonals)
adjacentCells.AddRange([originCell.DiagonalNeighbourBottomRight,
originCell.DiagonalNeighbourBottomLeft,
originCell.DiagonalNeighbourTopRight,
originCell.DiagonalNeighbourTopLeft
]);

return adjacentCells.RemoveNullables().RemoveDuplicates().ToList();
}

public List<GridCell> CrossCellsFrom(GridCell originCell) {
return CellsFromRow(originCell.Row)
.Concat(CellsFromColumn(originCell.Column))
.RemoveDuplicates()
.Select(cell => GridCells[cell.Column][cell.Row])
.ToList();
}


public void UpdateGridCellsNeighbours() {
GridCells.SelectMany(cells => cells).ToList().ForEach(cell => {
Expand Down Expand Up @@ -612,7 +637,7 @@ public VirtualBoard MovePiecesAndFillEmptyCells() {
currentCell.RemovePiece();
}

virtualBoard.AddUpdate(new BoardCellUpdate(UPDATE_TYPE.MOVEMENT, new(currentCell, bottomCell, bottomCell.Piece)));
virtualBoard.Update(new BoardCellUpdate(UPDATE_TYPE.MOVEMENT, new(currentCell, bottomCell, bottomCell.Piece)));
}
}

Expand All @@ -622,7 +647,7 @@ public VirtualBoard MovePiecesAndFillEmptyCells() {
foreach (var emptyCell in virtualBoard.EmptyCells()) {
GenerateRandomPieceOnCell(emptyCell);

virtualBoard.AddUpdate(new BoardCellUpdate(UPDATE_TYPE.FILL, null, new(emptyCell, emptyCell.Piece)));
virtualBoard.Update(new BoardCellUpdate(UPDATE_TYPE.FILL, null, new(emptyCell, emptyCell.Piece)));
}

return virtualBoard;
Expand Down
1 change: 0 additions & 1 deletion Match3Maker/src/components/Sequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public enum SHAPES {
public List<GridCell> Cells;
public SHAPES? Shape;


public Sequence(List<GridCell> cells, SHAPES? shape = null) {
Cells = [.. ValidCells(cells)];

Expand Down
4 changes: 1 addition & 3 deletions Match3Maker/src/features/PieceWeightGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using Extensionator;
using Extensionator;

namespace Match3Maker {
public sealed class PieceWeightGenerator : IPieceGenerator {
Expand Down Expand Up @@ -30,7 +29,6 @@ public Piece Roll(List<Piece> pieces, IEnumerable<Type>? only = null) {

float roll = _rng.NextFloat(0f, totalWeight);


foreach (Piece piece in selectedPieces) {

if (roll <= piece.TotalAccumWeight) {
Expand Down

0 comments on commit 863db00

Please sign in to comment.