Check mate implementation

This commit is contained in:
2024-10-28 19:21:16 -05:00
parent a3d6065e95
commit a7e26f5210
4 changed files with 96 additions and 59 deletions

View File

@@ -51,15 +51,15 @@ public static class Extensions
for (var x = 0; x < 8; x++) builder.Append("- - ");
builder.Append("- -");
builder.AppendLine();
builder.Append(" ");
builder.Append("Player 1");
builder.AppendLine();
builder.AppendLine();
// Print File ruler.
builder.Append(" ");
builder.Append(" A B C D E F G H I ");
builder.AppendLine();
builder.AppendLine();
builder.Append(" ");
builder.Append("Player 1");
return builder.ToString();
}

View File

@@ -65,10 +65,11 @@ namespace UnitTests
board["D5"].Should().BeNull();
// Act
var act = () => shogi.Move("D5", "D6", false);
var moveResult = shogi.Move("D5", "D6", false);
// Assert
act.Should().Throw<InvalidOperationException>();
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse();
board["D5"].Should().BeNull();
board["D6"].Should().BeNull();
board.Player1Hand.Should().BeEmpty();
@@ -84,13 +85,13 @@ namespace UnitTests
var expectedPiece = board["A3"];
// Act - P1 "moves" pawn to the position it already exists at.
var act = () => shogi.Move("A3", "A3", false);
var moveResult = shogi.Move("A3", "A3", false);
// Assert
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board["A3"].Should().Be(expectedPiece);
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse(); board["A3"].Should().Be(expectedPiece);
board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty();
}
@@ -106,13 +107,13 @@ namespace UnitTests
expectedPiece!.WhichPiece.Should().Be(WhichPiece.Lance);
// Act - Move Lance illegally
var act = () => shogi.Move("A1", "D5", false);
var moveResult = shogi.Move("A1", "D5", false);
// Assert
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board["A1"].Should().Be(expectedPiece);
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse(); board["A1"].Should().Be(expectedPiece);
board["A5"].Should().BeNull();
board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty();
@@ -130,13 +131,13 @@ namespace UnitTests
board.WhoseTurn.Should().Be(WhichPlayer.Player1);
// Act - Move Player2 Pawn when it is Player1 turn.
var act = () => shogi.Move("A7", "A6", false);
var moveResult = shogi.Move("A7", "A6", false);
// Assert
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board["A7"].Should().Be(expectedPiece);
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse(); board["A7"].Should().Be(expectedPiece);
board["A6"].Should().BeNull();
}
}
@@ -152,13 +153,13 @@ namespace UnitTests
lance!.Owner.Should().Be(pawn!.Owner);
// Act - Move P1 Lance through P1 Pawn.
var act = () => shogi.Move("A1", "A5", false);
var moveResult = shogi.Move("A1", "A5", false);
// Assert
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board["A1"].Should().Be(lance);
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse(); board["A1"].Should().Be(lance);
board["A3"].Should().Be(pawn);
board["A5"].Should().BeNull();
}
@@ -175,13 +176,13 @@ namespace UnitTests
knight!.Owner.Should().Be(pawn!.Owner);
// Act - P1 Knight tries to capture P1 Pawn.
var act = () => shogi.Move("B1", "C3", false);
var moveResult = shogi.Move("B1", "C3", false);
// Arrange
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board["B1"].Should().Be(knight);
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse(); board["B1"].Should().Be(knight);
board["C3"].Should().Be(pawn);
board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty();
@@ -204,13 +205,13 @@ namespace UnitTests
var lance = board["I9"];
// Act - P2 moves Lance while in check.
var act = () => shogi.Move("I9", "I8", false);
var moveResult = shogi.Move("I9", "I8", false);
// Assert
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board.InCheck.Should().Be(WhichPlayer.Player2);
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse(); board.InCheck.Should().Be(WhichPlayer.Player2);
board["I9"].Should().Be(lance);
board["I8"].Should().BeNull();
}
@@ -251,29 +252,33 @@ namespace UnitTests
// Act | Assert - Illegally placing Knight from the hand in farthest rank.
board["H9"].Should().BeNull();
var act = () => shogi.Move(WhichPiece.Knight, "H9");
act.Should().Throw<InvalidOperationException>();
var moveResult = shogi.Move(WhichPiece.Knight, "H9");
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse();
board["H9"].Should().BeNull();
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight);
// Act | Assert - Illegally placing Knight from the hand in second farthest row.
board["H8"].Should().BeNull();
act = () => shogi.Move(WhichPiece.Knight, "H8");
act.Should().Throw<InvalidOperationException>();
moveResult = shogi.Move(WhichPiece.Knight, "H8");
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse();
board["H8"].Should().BeNull();
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight);
// Act | Assert - Illegally place Lance from the hand.
board["H9"].Should().BeNull();
act = () => shogi.Move(WhichPiece.Knight, "H9");
act.Should().Throw<InvalidOperationException>();
moveResult = shogi.Move(WhichPiece.Knight, "H9");
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse();
board["H9"].Should().BeNull();
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
// Act | Assert - Illegally place Pawn from the hand.
board["H9"].Should().BeNull();
act = () => shogi.Move(WhichPiece.Pawn, "H9");
act.Should().Throw<InvalidOperationException>();
moveResult = shogi.Move(WhichPiece.Pawn, "H9");
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse();
board["H9"].Should().BeNull();
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn);
@@ -305,11 +310,12 @@ namespace UnitTests
shogi.BoardState["E5"].Should().BeNull();
// Act - P2 places a Bishop while in check.
var act = () => shogi.Move(WhichPiece.Bishop, "E5");
var moveResult = shogi.Move(WhichPiece.Bishop, "E5");
// Assert
using var scope = new AssertionScope();
act.Should().Throw<InvalidOperationException>();
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse();
shogi.BoardState["E5"].Should().BeNull();
shogi.BoardState.InCheck.Should().Be(WhichPlayer.Player2);
shogi.BoardState.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
@@ -334,11 +340,12 @@ namespace UnitTests
shogi.BoardState["I9"]!.Owner.Should().Be(WhichPlayer.Player2);
// Act - P1 tries to place a piece where an opponent's piece resides.
var act = () => shogi.Move(WhichPiece.Bishop, "I9");
var moveResult = shogi.Move(WhichPiece.Bishop, "I9");
// Assert
using var scope = new AssertionScope();
act.Should().Throw<InvalidOperationException>();
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse();
shogi.BoardState.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.BoardState["I9"].Should().NotBeNull();
shogi.BoardState["I9"]!.WhichPiece.Should().Be(WhichPiece.Lance);
@@ -439,12 +446,13 @@ namespace UnitTests
// P2 King retreat
shogi.Move("E8", "E9", false);
console.WriteLine(shogi.ToStringStateAsAscii());
// Act - P1 Pawn wins by checkmate.
shogi.Move("E7", "E8", false);
// Assert - checkmate
console.WriteLine(shogi.ToStringStateAsAscii());
console.WriteLine(string.Join(",", shogi.BoardState.Player1Hand.Select(p => p.WhichPiece.ToString())));
board.IsCheckmate.Should().BeTrue();
board.InCheck.Should().Be(WhichPlayer.Player2);
}