From 816c22c0004733c9b224cf5a0cd0e24dac1fcbd3 Mon Sep 17 00:00:00 2001 From: ninetailsrabbit Date: Tue, 11 Jun 2024 17:10:02 +0100 Subject: [PATCH] detect sequence shapes automatically on cells when not provided --- .../tests/src/components/SequenceTests.cs | 71 +++++++++++++++++-- Match3Maker/Match3Maker.csproj | 2 +- Match3Maker/src/components/Sequence.cs | 45 ++++++++++-- 3 files changed, 109 insertions(+), 9 deletions(-) diff --git a/Match3Maker.Tests/tests/src/components/SequenceTests.cs b/Match3Maker.Tests/tests/src/components/SequenceTests.cs index 2b8de10..41d9b39 100644 --- a/Match3Maker.Tests/tests/src/components/SequenceTests.cs +++ b/Match3Maker.Tests/tests/src/components/SequenceTests.cs @@ -91,6 +91,69 @@ public void Sequence_Pieces_Can_Be_Retrieved() { Assert.True(sequence.Pieces().All(piece => piece.MatchWith(new Piece(_pieceFactory.CreateNormalPiece(shape))))); } + [Fact] + public void Should_Detect_Shape_Vertical_Automatically_When_Null() { + string shape = "square"; + + var cells = new List() { + new(2, 1, new Piece(_pieceFactory.CreateNormalPiece(shape))), + new(2, 2, new Piece(_pieceFactory.CreateNormalPiece(shape))), + new(2, 3, new Piece(_pieceFactory.CreateNormalPiece(shape))) + }; + + var sequence = new Sequence(cells); + + Assert.Equal(Sequence.SHAPES.VERTICAL, sequence.Shape); + + + } + + [Fact] + public void Should_Detect_Shape_Horizontal_Automatically_When_Null() { + string shape = "square"; + + var cells = new List() { + new(2, 1, new Piece(_pieceFactory.CreateNormalPiece(shape))), + new(3, 1, new Piece(_pieceFactory.CreateNormalPiece(shape))), + new(4, 1, new Piece(_pieceFactory.CreateNormalPiece(shape))) + }; + + var sequence = new Sequence(cells); + + Assert.Equal(Sequence.SHAPES.HORIZONTAL, sequence.Shape); + } + + [Fact] + public void Should_Detect_Shape_Diagonal_Automatically_When_Null() { + string shape = "square"; + + var cells = new List() { + new(2, 1, new Piece(_pieceFactory.CreateNormalPiece(shape))), + new(3, 2, new Piece(_pieceFactory.CreateNormalPiece(shape))), + new(4, 3, new Piece(_pieceFactory.CreateNormalPiece(shape))) + }; + + var sequence = new Sequence(cells); + + Assert.Equal(Sequence.SHAPES.DIAGONAL, sequence.Shape); + } + + + [Fact] + public void Should_Detect_Shape_Irregular_Automatically_When_Null() { + string shape = "square"; + + var cells = new List() { + new(2, 4, new Piece(_pieceFactory.CreateNormalPiece(shape))), + new(6, 3, new Piece(_pieceFactory.CreateNormalPiece(shape))), + new(7, 5, new Piece(_pieceFactory.CreateNormalPiece(shape))) + }; + + var sequence = new Sequence(cells); + + Assert.Equal(Sequence.SHAPES.IRREGULAR, sequence.Shape); + } + [Fact] public void Should_Retrieve_Correct_Horizontal_Edge_Cells() { string shape = "triangle"; @@ -130,10 +193,10 @@ public void Should_Retrieve_Correct_Horizontal_Edge_Cells() { public void Should_Retrieve_Correct_Vertical_Edge_Cells() { string shape = "triangle"; - var cells = new List() { - new(1, 1, new Piece(_pieceFactory.CreateNormalPiece(shape))), - new(1, 2, new Piece(_pieceFactory.CreateNormalPiece(shape))), - new(1, 3, new Piece(_pieceFactory.CreateNormalPiece(shape))) + var cells = new List() { + new(1, 1, new Piece(_pieceFactory.CreateNormalPiece(shape))), + new(1, 2, new Piece(_pieceFactory.CreateNormalPiece(shape))), + new(1, 3, new Piece(_pieceFactory.CreateNormalPiece(shape))) }; var sequence = new Sequence(cells, Sequence.SHAPES.VERTICAL); diff --git a/Match3Maker/Match3Maker.csproj b/Match3Maker/Match3Maker.csproj index 77c3a44..1a27c07 100644 --- a/Match3Maker/Match3Maker.csproj +++ b/Match3Maker/Match3Maker.csproj @@ -14,7 +14,7 @@ Ninetailsrabbit.Match3Maker - 1.1.1 + 1.1.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 diff --git a/Match3Maker/src/components/Sequence.cs b/Match3Maker/src/components/Sequence.cs index 40ce77a..5ead687 100644 --- a/Match3Maker/src/components/Sequence.cs +++ b/Match3Maker/src/components/Sequence.cs @@ -2,16 +2,28 @@ namespace Match3Maker { - public class Sequence(List cells, Sequence.SHAPES shape) : ICloneable { + public class Sequence : ICloneable { public enum SHAPES { HORIZONTAL, VERTICAL, T_SHAPE, - L_SHAPE + L_SHAPE, + DIAGONAL, + LINE_CONNECTED, + IRREGULAR } - public List Cells = [.. ValidCells(cells)]; - public SHAPES Shape = shape; + public List Cells; + public SHAPES? Shape; + + + public Sequence(List cells, SHAPES? shape = null) { + Cells = [.. ValidCells(cells)]; + + shape ??= DetectShape(Cells); + + Shape = shape; + } public int Size() => Cells.Count; @@ -58,6 +70,9 @@ public void Consume() { public bool IsVertical() => Shape.Equals(SHAPES.VERTICAL); public bool IsTShape() => Shape.Equals(SHAPES.T_SHAPE); public bool IsLShape() => Shape.Equals(SHAPES.L_SHAPE); + public bool IsDiagonalShape() => Shape.Equals(SHAPES.DIAGONAL); + public bool IsLineConnectedShape() => Shape.Equals(SHAPES.LINE_CONNECTED); + public bool IsHorizontalOrVertical() => IsHorizontal() || IsVertical(); #endregion @@ -80,6 +95,28 @@ private static List ValidCells(List cells) { return [.. validCells]; } + + private static SHAPES DetectShape(List cells) { + var cellsByIndex = cells.Select((value, index) => new { value, index }); + + bool isHorizontalShape = cellsByIndex.All(item => item.index == 0 || item.value.InSameRowAs(cells[item.index - 1])); + + if (isHorizontalShape) + return SHAPES.HORIZONTAL; + + bool isVerticalShape = cellsByIndex.All(item => item.index == 0 || item.value.InSameColumnAs(cells[item.index - 1])); + + if (isVerticalShape) + return SHAPES.VERTICAL; + + bool isDiagonalShape = cellsByIndex.All(item => item.index == 0 || item.value.InDiagonalWith(cells[item.index - 1])); + + if (isDiagonalShape) + return SHAPES.DIAGONAL; + + + return SHAPES.IRREGULAR; + } } } \ No newline at end of file