Skip to content

Commit

Permalink
detect sequence shapes automatically on cells when not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
ninetailsrabbit committed Jun 11, 2024
1 parent 3436e33 commit 816c22c
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 9 deletions.
71 changes: 67 additions & 4 deletions Match3Maker.Tests/tests/src/components/SequenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GridCell>() {
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<GridCell>() {
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<GridCell>() {
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<GridCell>() {
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";
Expand Down Expand Up @@ -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<GridCell>() {
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<GridCell>() {
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);

Expand Down
2 changes: 1 addition & 1 deletion 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.1.1</Version>
<Version>1.1.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 Down
45 changes: 41 additions & 4 deletions Match3Maker/src/components/Sequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@

namespace Match3Maker {

public class Sequence(List<GridCell> 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<GridCell> Cells = [.. ValidCells(cells)];
public SHAPES Shape = shape;
public List<GridCell> Cells;
public SHAPES? Shape;


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

shape ??= DetectShape(Cells);

Shape = shape;
}

public int Size() => Cells.Count;

Expand Down Expand Up @@ -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

Expand All @@ -80,6 +95,28 @@ private static List<GridCell> ValidCells(List<GridCell> cells) {

return [.. validCells];
}

private static SHAPES DetectShape(List<GridCell> 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;
}
}

}

0 comments on commit 816c22c

Please sign in to comment.