diff --git a/Match3Maker.Tests/tests/src/components/BoardTests.cs b/Match3Maker.Tests/tests/src/components/BoardTests.cs index 458cadf..58623f8 100644 --- a/Match3Maker.Tests/tests/src/components/BoardTests.cs +++ b/Match3Maker.Tests/tests/src/components/BoardTests.cs @@ -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); + + Assert.Equal(2, adjacentCells.Count); + + foreach (var cell in adjacentCells) { + Assert.True(cell.IsColumnNeighbourOf(originCell) || cell.IsRowNeighbourOf(originCell)); + } + + adjacentCells = board.AdjacentCellsFrom(originCell, true); + + Assert.Equal(3, adjacentCells.Count); + Assert.Contains(originCell.DiagonalNeighbourBottomRight, adjacentCells); + + originCell = board.Cell(1, 1); + adjacentCells = board.AdjacentCellsFrom(originCell); + + Assert.Equal(4, adjacentCells.Count); + + foreach (var cell in adjacentCells) { + Assert.True(cell.IsColumnNeighbourOf(originCell) || cell.IsRowNeighbourOf(originCell)); + } + + adjacentCells = board.AdjacentCellsFrom(originCell, true); + + 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); diff --git a/Match3Maker/Match3Maker.csproj b/Match3Maker/Match3Maker.csproj index d85d651..3404bcd 100644 --- a/Match3Maker/Match3Maker.csproj +++ b/Match3Maker/Match3Maker.csproj @@ -14,7 +14,7 @@ Ninetailsrabbit.Match3Maker - 1.2.0 + 1.2.2 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 © 2024 Ninetailsrabbit Ninetailsrabbit @@ -39,7 +39,7 @@ - + \ No newline at end of file diff --git a/Match3Maker/src/components/Board.cs b/Match3Maker/src/components/Board.cs index 2e54589..a74303f 100644 --- a/Match3Maker/src/components/Board.cs +++ b/Match3Maker/src/components/Board.cs @@ -1,5 +1,5 @@ -using System.Numerics; -using Extensionator; +using Extensionator; +using System.Numerics; using static Match3Maker.BoardCellUpdate; namespace Match3Maker { @@ -22,7 +22,7 @@ public VirtualBoard(List> gridCells) { public GridCell? Cell(int column, int row) => GridCells.FirstOrDefault(cell => cell.Column.Equals(column) && cell.Row.Equals(row)); public List EmptyCells() => GridCells.Where(cell => cell.IsEmpty() && cell.CanContainPiece).ToList(); - public void AddUpdate(BoardCellUpdate update) { + public void Update(BoardCellUpdate update) { Updates.Add(update); } public List MovementUpdates() => Updates.Where(update => update.CurrentUpdateType.Equals(UPDATE_TYPE.MOVEMENT)).ToList(); @@ -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 @@ -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? disabledCells = null, bool overwrite = false) { if (GridCells.IsEmpty() || overwrite) { @@ -499,6 +497,33 @@ public List CellsWithShape(string shape) { .ToList(); } + public static List AdjacentCellsFrom(GridCell originCell, bool includeDiagonals = false) { +#pragma warning disable 8601 + List 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 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 => { @@ -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))); } } @@ -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; diff --git a/Match3Maker/src/components/Sequence.cs b/Match3Maker/src/components/Sequence.cs index 321210b..9283d9e 100644 --- a/Match3Maker/src/components/Sequence.cs +++ b/Match3Maker/src/components/Sequence.cs @@ -16,7 +16,6 @@ public enum SHAPES { public List Cells; public SHAPES? Shape; - public Sequence(List cells, SHAPES? shape = null) { Cells = [.. ValidCells(cells)]; diff --git a/Match3Maker/src/features/PieceWeightGenerator.cs b/Match3Maker/src/features/PieceWeightGenerator.cs index bf3675a..dbccf8a 100644 --- a/Match3Maker/src/features/PieceWeightGenerator.cs +++ b/Match3Maker/src/features/PieceWeightGenerator.cs @@ -1,5 +1,4 @@ -using System.Diagnostics; -using Extensionator; +using Extensionator; namespace Match3Maker { public sealed class PieceWeightGenerator : IPieceGenerator { @@ -30,7 +29,6 @@ public Piece Roll(List pieces, IEnumerable? only = null) { float roll = _rng.NextFloat(0f, totalWeight); - foreach (Piece piece in selectedPieces) { if (roll <= piece.TotalAccumWeight) {