This commit is contained in:
2024-10-20 22:27:08 -05:00
parent f75553a0ad
commit 3593785421
27 changed files with 1020 additions and 1081 deletions

View File

@@ -0,0 +1,97 @@
using Shogi.Domain.ValueObjects;
using System;
using System.Text;
namespace UnitTests;
public static class Extensions
{
/// <summary>
/// Prints a ASCII representation of the board for debugging board state.
/// </summary>
/// <returns></returns>
public static string ToStringStateAsAscii(this ShogiBoard board)
{
var boardState = board.BoardState;
var builder = new StringBuilder();
builder.Append(" ");
builder.Append("Player 2");
builder.AppendLine();
for (var rank = 8; rank >= 0; rank--)
{
// Horizontal line
builder.Append(" - ");
for (var file = 0; file < 8; file++) builder.Append("- - ");
builder.Append("- -");
// Print Rank ruler.
builder.AppendLine();
builder.Append($"{rank + 1} ");
// Print pieces.
builder.Append(" |");
for (var x = 0; x < 9; x++)
{
var piece = boardState[x, rank];
if (piece == null)
{
builder.Append(" ");
}
else
{
builder.AppendFormat("{0}", ToAscii(piece));
}
builder.Append('|');
}
builder.AppendLine();
}
// Horizontal line
builder.Append(" - ");
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 ");
return builder.ToString();
}
/// <returns>
/// A string with three characters.
/// The first character indicates promotion status.
/// The second character indicates piece.
/// The third character indicates ownership.
/// </returns>
private static string ToAscii(Piece piece)
{
var builder = new StringBuilder();
if (piece.IsPromoted) builder.Append('^');
else builder.Append(' ');
var name = piece.WhichPiece switch
{
WhichPiece.King => "K",
WhichPiece.GoldGeneral => "G",
WhichPiece.SilverGeneral => "S",
WhichPiece.Bishop => "B",
WhichPiece.Rook => "R",
WhichPiece.Knight => "k",
WhichPiece.Lance => "L",
WhichPiece.Pawn => "P",
_ => throw new ArgumentException($"Unknown value for {nameof(WhichPiece)}."),
};
builder.Append(name);
if (piece.Owner == WhichPlayer.Player2) builder.Append('.');
else builder.Append(' ');
return builder.ToString();
}
}

View File

@@ -1,9 +1,9 @@
using System.Numerics;
using Shogi.Domain.YetToBeAssimilatedIntoDDD;
namespace Shogi.Domain.UnitTests
namespace UnitTests
{
public class NotationShould
public class NotationShould
{
[Fact]
public void ConvertFromNotationToVector()

View File

@@ -2,221 +2,221 @@
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
using System.Numerics;
namespace Shogi.Domain.UnitTests;
namespace UnitTests;
public class RookShould
{
public class MoveSet
{
private readonly Rook rook1;
private readonly Rook rook2;
public class MoveSet
{
private readonly Rook rook1;
private readonly Rook rook2;
public MoveSet()
{
this.rook1 = new Rook(WhichPlayer.Player1);
this.rook2 = new Rook(WhichPlayer.Player2);
}
public MoveSet()
{
rook1 = new Rook(WhichPlayer.Player1);
rook2 = new Rook(WhichPlayer.Player2);
}
[Fact]
public void Player1_HasCorrectMoveSet()
{
var moveSet = rook1.MoveSet;
moveSet.Should().HaveCount(4);
moveSet.Should().ContainEquivalentOf(new Path(Direction.Forward, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Left, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Right, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Backward, Distance.MultiStep));
}
[Fact]
public void Player1_HasCorrectMoveSet()
{
var moveSet = rook1.MoveSet;
moveSet.Should().HaveCount(4);
moveSet.Should().ContainEquivalentOf(new Path(Direction.Forward, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Left, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Right, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Backward, Distance.MultiStep));
}
[Fact]
public void Player1_Promoted_HasCorrectMoveSet()
{
// Arrange
rook1.Promote();
rook1.IsPromoted.Should().BeTrue();
[Fact]
public void Player1_Promoted_HasCorrectMoveSet()
{
// Arrange
rook1.Promote();
rook1.IsPromoted.Should().BeTrue();
// Assert
var moveSet = rook1.MoveSet;
moveSet.Should().HaveCount(8);
moveSet.Should().ContainEquivalentOf(new Path(Direction.Forward, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Left, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Right, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Backward, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.ForwardLeft, Distance.OneStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.BackwardLeft, Distance.OneStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.ForwardRight, Distance.OneStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.BackwardRight, Distance.OneStep));
}
// Assert
var moveSet = rook1.MoveSet;
moveSet.Should().HaveCount(8);
moveSet.Should().ContainEquivalentOf(new Path(Direction.Forward, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Left, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Right, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Backward, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.ForwardLeft, Distance.OneStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.BackwardLeft, Distance.OneStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.ForwardRight, Distance.OneStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.BackwardRight, Distance.OneStep));
}
[Fact]
public void Player2_HasCorrectMoveSet()
{
var moveSet = rook2.MoveSet;
moveSet.Should().HaveCount(4);
moveSet.Should().ContainEquivalentOf(new Path(Direction.Forward, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Left, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Right, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Backward, Distance.MultiStep));
}
[Fact]
public void Player2_HasCorrectMoveSet()
{
var moveSet = rook2.MoveSet;
moveSet.Should().HaveCount(4);
moveSet.Should().ContainEquivalentOf(new Path(Direction.Forward, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Left, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Right, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Backward, Distance.MultiStep));
}
[Fact]
public void Player2_Promoted_HasCorrectMoveSet()
{
// Arrange
rook2.Promote();
rook2.IsPromoted.Should().BeTrue();
[Fact]
public void Player2_Promoted_HasCorrectMoveSet()
{
// Arrange
rook2.Promote();
rook2.IsPromoted.Should().BeTrue();
// Assert
var moveSet = rook2.MoveSet;
moveSet.Should().HaveCount(8);
moveSet.Should().ContainEquivalentOf(new Path(Direction.Forward, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Left, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Right, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Backward, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.ForwardLeft, Distance.OneStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.BackwardLeft, Distance.OneStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.ForwardRight, Distance.OneStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.BackwardRight, Distance.OneStep));
}
// Assert
var moveSet = rook2.MoveSet;
moveSet.Should().HaveCount(8);
moveSet.Should().ContainEquivalentOf(new Path(Direction.Forward, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Left, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Right, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.Backward, Distance.MultiStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.ForwardLeft, Distance.OneStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.BackwardLeft, Distance.OneStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.ForwardRight, Distance.OneStep));
moveSet.Should().ContainEquivalentOf(new Path(Direction.BackwardRight, Distance.OneStep));
}
}
private readonly Rook rookPlayer1;
}
private readonly Rook rookPlayer1;
public RookShould()
{
this.rookPlayer1 = new Rook(WhichPlayer.Player1);
}
public RookShould()
{
rookPlayer1 = new Rook(WhichPlayer.Player1);
}
[Fact]
public void Promote()
{
this.rookPlayer1.IsPromoted.Should().BeFalse();
this.rookPlayer1.CanPromote.Should().BeTrue();
this.rookPlayer1.Promote();
this.rookPlayer1.IsPromoted.Should().BeTrue();
this.rookPlayer1.CanPromote.Should().BeFalse();
}
[Fact]
public void Promote()
{
rookPlayer1.IsPromoted.Should().BeFalse();
rookPlayer1.CanPromote.Should().BeTrue();
rookPlayer1.Promote();
rookPlayer1.IsPromoted.Should().BeTrue();
rookPlayer1.CanPromote.Should().BeFalse();
}
[Fact]
public void GetStepsFromStartToEnd_Player1NotPromoted_LateralMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(0, 5);
[Fact]
public void GetStepsFromStartToEnd_Player1NotPromoted_LateralMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(0, 5);
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
rookPlayer1.IsPromoted.Should().BeFalse();
steps.Should().HaveCount(5);
steps.Should().Contain(new Vector2(0, 1));
steps.Should().Contain(new Vector2(0, 2));
steps.Should().Contain(new Vector2(0, 3));
steps.Should().Contain(new Vector2(0, 4));
steps.Should().Contain(new Vector2(0, 5));
}
rookPlayer1.IsPromoted.Should().BeFalse();
steps.Should().HaveCount(5);
steps.Should().Contain(new Vector2(0, 1));
steps.Should().Contain(new Vector2(0, 2));
steps.Should().Contain(new Vector2(0, 3));
steps.Should().Contain(new Vector2(0, 4));
steps.Should().Contain(new Vector2(0, 5));
}
[Fact]
public void GetStepsFromStartToEnd_Player1NotPromoted_DiagonalMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(1, 1);
[Fact]
public void GetStepsFromStartToEnd_Player1NotPromoted_DiagonalMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(1, 1);
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
rookPlayer1.IsPromoted.Should().BeFalse();
steps.Should().BeEmpty();
}
rookPlayer1.IsPromoted.Should().BeFalse();
steps.Should().BeEmpty();
}
[Fact]
public void GetStepsFromStartToEnd_Player1Promoted_LateralMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(0, 5);
rookPlayer1.Promote();
[Fact]
public void GetStepsFromStartToEnd_Player1Promoted_LateralMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(0, 5);
rookPlayer1.Promote();
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
rookPlayer1.IsPromoted.Should().BeTrue();
steps.Should().HaveCount(5);
steps.Should().Contain(new Vector2(0, 1));
steps.Should().Contain(new Vector2(0, 2));
steps.Should().Contain(new Vector2(0, 3));
steps.Should().Contain(new Vector2(0, 4));
steps.Should().Contain(new Vector2(0, 5));
}
rookPlayer1.IsPromoted.Should().BeTrue();
steps.Should().HaveCount(5);
steps.Should().Contain(new Vector2(0, 1));
steps.Should().Contain(new Vector2(0, 2));
steps.Should().Contain(new Vector2(0, 3));
steps.Should().Contain(new Vector2(0, 4));
steps.Should().Contain(new Vector2(0, 5));
}
[Fact]
public void GetStepsFromStartToEnd_Player1Promoted_DiagonalMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(1, 1);
rookPlayer1.Promote();
[Fact]
public void GetStepsFromStartToEnd_Player1Promoted_DiagonalMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(1, 1);
rookPlayer1.Promote();
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
rookPlayer1.IsPromoted.Should().BeTrue();
steps.Should().HaveCount(1);
steps.Should().Contain(new Vector2(1, 1));
}
rookPlayer1.IsPromoted.Should().BeTrue();
steps.Should().HaveCount(1);
steps.Should().Contain(new Vector2(1, 1));
}
[Fact]
public void GetStepsFromStartToEnd_Player2NotPromoted_LateralMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(0, 5);
[Fact]
public void GetStepsFromStartToEnd_Player2NotPromoted_LateralMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(0, 5);
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
rookPlayer1.IsPromoted.Should().BeFalse();
steps.Should().HaveCount(5);
steps.Should().Contain(new Vector2(0, 1));
steps.Should().Contain(new Vector2(0, 2));
steps.Should().Contain(new Vector2(0, 3));
steps.Should().Contain(new Vector2(0, 4));
steps.Should().Contain(new Vector2(0, 5));
}
rookPlayer1.IsPromoted.Should().BeFalse();
steps.Should().HaveCount(5);
steps.Should().Contain(new Vector2(0, 1));
steps.Should().Contain(new Vector2(0, 2));
steps.Should().Contain(new Vector2(0, 3));
steps.Should().Contain(new Vector2(0, 4));
steps.Should().Contain(new Vector2(0, 5));
}
[Fact]
public void GetStepsFromStartToEnd_Player2NotPromoted_DiagonalMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(1, 1);
[Fact]
public void GetStepsFromStartToEnd_Player2NotPromoted_DiagonalMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(1, 1);
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
rookPlayer1.IsPromoted.Should().BeFalse();
steps.Should().BeEmpty();
}
rookPlayer1.IsPromoted.Should().BeFalse();
steps.Should().BeEmpty();
}
[Fact]
public void GetStepsFromStartToEnd_Player2Promoted_LateralMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(0, 5);
rookPlayer1.Promote();
[Fact]
public void GetStepsFromStartToEnd_Player2Promoted_LateralMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(0, 5);
rookPlayer1.Promote();
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
rookPlayer1.IsPromoted.Should().BeTrue();
steps.Should().HaveCount(5);
steps.Should().Contain(new Vector2(0, 1));
steps.Should().Contain(new Vector2(0, 2));
steps.Should().Contain(new Vector2(0, 3));
steps.Should().Contain(new Vector2(0, 4));
steps.Should().Contain(new Vector2(0, 5));
}
rookPlayer1.IsPromoted.Should().BeTrue();
steps.Should().HaveCount(5);
steps.Should().Contain(new Vector2(0, 1));
steps.Should().Contain(new Vector2(0, 2));
steps.Should().Contain(new Vector2(0, 3));
steps.Should().Contain(new Vector2(0, 4));
steps.Should().Contain(new Vector2(0, 5));
}
[Fact]
public void GetStepsFromStartToEnd_Player2Promoted_DiagonalMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(1, 1);
rookPlayer1.Promote();
[Fact]
public void GetStepsFromStartToEnd_Player2Promoted_DiagonalMove()
{
Vector2 start = new(0, 0);
Vector2 end = new(1, 1);
rookPlayer1.Promote();
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
var steps = rookPlayer1.GetPathFromStartToEnd(start, end);
rookPlayer1.IsPromoted.Should().BeTrue();
steps.Should().HaveCount(1);
steps.Should().Contain(new Vector2(1, 1));
}
rookPlayer1.IsPromoted.Should().BeTrue();
steps.Should().HaveCount(1);
steps.Should().Contain(new Vector2(1, 1));
}
}

View File

@@ -1,6 +1,6 @@
using Shogi.Domain.ValueObjects;
namespace Shogi.Domain.UnitTests;
namespace UnitTests;
public class ShogiBoardStateShould
{

View File

@@ -2,453 +2,453 @@
using System;
using System.Linq;
namespace Shogi.Domain.UnitTests
namespace UnitTests
{
public class ShogiShould
{
private readonly ITestOutputHelper console;
public ShogiShould(ITestOutputHelper console)
{
this.console = console;
}
public class ShogiShould
{
private readonly ITestOutputHelper console;
public ShogiShould(ITestOutputHelper console)
{
this.console = console;
}
[Fact]
public void MoveAPieceToAnEmptyPosition()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
[Fact]
public void MoveAPieceToAnEmptyPosition()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
board["A4"].Should().BeNull();
var expectedPiece = board["A3"];
expectedPiece.Should().NotBeNull();
board["A4"].Should().BeNull();
var expectedPiece = board["A3"];
expectedPiece.Should().NotBeNull();
// Act
shogi.Move("A3", "A4", false);
// Act
shogi.Move("A3", "A4", false);
// Assert
board["A3"].Should().BeNull();
board["A4"].Should().Be(expectedPiece);
}
// Assert
board["A3"].Should().BeNull();
board["A4"].Should().Be(expectedPiece);
}
[Fact]
public void AllowValidMoves_AfterCheck()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
// P1 Pawn
shogi.Move("C3", "C4", false);
// P2 Pawn
shogi.Move("G7", "G6", false);
// P1 Bishop puts P2 in check
shogi.Move("B2", "G7", false);
board.InCheck.Should().Be(WhichPlayer.Player2);
[Fact]
public void AllowValidMoves_AfterCheck()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
// P1 Pawn
shogi.Move("C3", "C4", false);
// P2 Pawn
shogi.Move("G7", "G6", false);
// P1 Bishop puts P2 in check
shogi.Move("B2", "G7", false);
board.InCheck.Should().Be(WhichPlayer.Player2);
// Act - P2 is able to un-check theirself.
/// P2 King moves out of check
shogi.Move("E9", "E8", false);
// Act - P2 is able to un-check theirself.
/// P2 King moves out of check
shogi.Move("E9", "E8", false);
// Assert
using (new AssertionScope())
{
board.InCheck.Should().BeNull();
}
}
// Assert
using (new AssertionScope())
{
board.InCheck.Should().BeNull();
}
}
[Fact]
public void PreventInvalidMoves_MoveFromEmptyPosition()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
board["D5"].Should().BeNull();
[Fact]
public void PreventInvalidMoves_MoveFromEmptyPosition()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
board["D5"].Should().BeNull();
// Act
var act = () => shogi.Move("D5", "D6", false);
// Act
var act = () => shogi.Move("D5", "D6", false);
// Assert
act.Should().Throw<InvalidOperationException>();
board["D5"].Should().BeNull();
board["D6"].Should().BeNull();
board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty();
}
// Assert
act.Should().Throw<InvalidOperationException>();
board["D5"].Should().BeNull();
board["D6"].Should().BeNull();
board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty();
}
[Fact]
public void PreventInvalidMoves_MoveToCurrentPosition()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
var expectedPiece = board["A3"];
[Fact]
public void PreventInvalidMoves_MoveToCurrentPosition()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
var expectedPiece = board["A3"];
// Act - P1 "moves" pawn to the position it already exists at.
var act = () => shogi.Move("A3", "A3", false);
// Act - P1 "moves" pawn to the position it already exists at.
var act = () => shogi.Move("A3", "A3", false);
// Assert
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board["A3"].Should().Be(expectedPiece);
board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty();
}
}
// Assert
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board["A3"].Should().Be(expectedPiece);
board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty();
}
}
[Fact]
public void PreventInvalidMoves_MoveSet()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
var expectedPiece = board["A1"];
expectedPiece!.WhichPiece.Should().Be(WhichPiece.Lance);
[Fact]
public void PreventInvalidMoves_MoveSet()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
var expectedPiece = board["A1"];
expectedPiece!.WhichPiece.Should().Be(WhichPiece.Lance);
// Act - Move Lance illegally
var act = () => shogi.Move("A1", "D5", false);
// Act - Move Lance illegally
var act = () => shogi.Move("A1", "D5", false);
// Assert
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board["A1"].Should().Be(expectedPiece);
board["A5"].Should().BeNull();
board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty();
}
}
// Assert
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board["A1"].Should().Be(expectedPiece);
board["A5"].Should().BeNull();
board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty();
}
}
[Fact]
public void PreventInvalidMoves_Ownership()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
var expectedPiece = board["A7"];
expectedPiece!.Owner.Should().Be(WhichPlayer.Player2);
board.WhoseTurn.Should().Be(WhichPlayer.Player1);
[Fact]
public void PreventInvalidMoves_Ownership()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
var expectedPiece = board["A7"];
expectedPiece!.Owner.Should().Be(WhichPlayer.Player2);
board.WhoseTurn.Should().Be(WhichPlayer.Player1);
// Act - Move Player2 Pawn when it is Player1 turn.
var act = () => shogi.Move("A7", "A6", false);
// Act - Move Player2 Pawn when it is Player1 turn.
var act = () => shogi.Move("A7", "A6", false);
// Assert
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board["A7"].Should().Be(expectedPiece);
board["A6"].Should().BeNull();
}
}
// Assert
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board["A7"].Should().Be(expectedPiece);
board["A6"].Should().BeNull();
}
}
[Fact]
public void PreventInvalidMoves_MoveThroughAllies()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
var lance = board["A1"];
var pawn = board["A3"];
lance!.Owner.Should().Be(pawn!.Owner);
[Fact]
public void PreventInvalidMoves_MoveThroughAllies()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
var lance = board["A1"];
var pawn = board["A3"];
lance!.Owner.Should().Be(pawn!.Owner);
// Act - Move P1 Lance through P1 Pawn.
var act = () => shogi.Move("A1", "A5", false);
// Act - Move P1 Lance through P1 Pawn.
var act = () => shogi.Move("A1", "A5", false);
// Assert
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board["A1"].Should().Be(lance);
board["A3"].Should().Be(pawn);
board["A5"].Should().BeNull();
}
}
// Assert
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board["A1"].Should().Be(lance);
board["A3"].Should().Be(pawn);
board["A5"].Should().BeNull();
}
}
[Fact]
public void PreventInvalidMoves_CaptureAlly()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
var knight = board["B1"];
var pawn = board["C3"];
knight!.Owner.Should().Be(pawn!.Owner);
[Fact]
public void PreventInvalidMoves_CaptureAlly()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
var knight = board["B1"];
var pawn = board["C3"];
knight!.Owner.Should().Be(pawn!.Owner);
// Act - P1 Knight tries to capture P1 Pawn.
var act = () => shogi.Move("B1", "C3", false);
// Act - P1 Knight tries to capture P1 Pawn.
var act = () => shogi.Move("B1", "C3", false);
// Arrange
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board["B1"].Should().Be(knight);
board["C3"].Should().Be(pawn);
board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty();
}
}
// Arrange
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board["B1"].Should().Be(knight);
board["C3"].Should().Be(pawn);
board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty();
}
}
[Fact]
public void PreventInvalidMoves_Check()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
// P1 Pawn
shogi.Move("C3", "C4", false);
// P2 Pawn
shogi.Move("G7", "G6", false);
// P1 Bishop puts P2 in check
shogi.Move("B2", "G7", false);
board.InCheck.Should().Be(WhichPlayer.Player2);
var lance = board["I9"];
[Fact]
public void PreventInvalidMoves_Check()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
// P1 Pawn
shogi.Move("C3", "C4", false);
// P2 Pawn
shogi.Move("G7", "G6", false);
// P1 Bishop puts P2 in check
shogi.Move("B2", "G7", false);
board.InCheck.Should().Be(WhichPlayer.Player2);
var lance = board["I9"];
// Act - P2 moves Lance while in check.
var act = () => shogi.Move("I9", "I8", false);
// Act - P2 moves Lance while in check.
var act = () => shogi.Move("I9", "I8", false);
// Assert
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board.InCheck.Should().Be(WhichPlayer.Player2);
board["I9"].Should().Be(lance);
board["I8"].Should().BeNull();
}
}
// Assert
using (new AssertionScope())
{
act.Should().Throw<InvalidOperationException>();
board.InCheck.Should().Be(WhichPlayer.Player2);
board["I9"].Should().Be(lance);
board["I8"].Should().BeNull();
}
}
[Fact]
public void PreventInvalidDrops_MoveSet()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
// P1 Pawn
shogi.Move("C3", "C4", false);
// P2 Pawn
shogi.Move("I7", "I6", false);
// P1 Bishop takes P2 Pawn.
shogi.Move("B2", "G7", false);
// P2 Gold, block check from P1 Bishop.
shogi.Move("F9", "F8", false);
// P1 Bishop takes P2 Bishop, promotes so it can capture P2 Knight and P2 Lance
shogi.Move("G7", "H8", true);
// P2 Pawn again
shogi.Move("I6", "I5", false);
// P1 Bishop takes P2 Knight
shogi.Move("H8", "H9", false);
// P2 Pawn again
shogi.Move("I5", "I4", false);
// P1 Bishop takes P2 Lance
shogi.Move("H9", "I9", false);
// P2 Pawn captures P1 Pawn
shogi.Move("I4", "I3", false);
board.Player1Hand.Count.Should().Be(4);
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight);
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn);
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
board.WhoseTurn.Should().Be(WhichPlayer.Player1);
[Fact]
public void PreventInvalidDrops_MoveSet()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
// P1 Pawn
shogi.Move("C3", "C4", false);
// P2 Pawn
shogi.Move("I7", "I6", false);
// P1 Bishop takes P2 Pawn.
shogi.Move("B2", "G7", false);
// P2 Gold, block check from P1 Bishop.
shogi.Move("F9", "F8", false);
// P1 Bishop takes P2 Bishop, promotes so it can capture P2 Knight and P2 Lance
shogi.Move("G7", "H8", true);
// P2 Pawn again
shogi.Move("I6", "I5", false);
// P1 Bishop takes P2 Knight
shogi.Move("H8", "H9", false);
// P2 Pawn again
shogi.Move("I5", "I4", false);
// P1 Bishop takes P2 Lance
shogi.Move("H9", "I9", false);
// P2 Pawn captures P1 Pawn
shogi.Move("I4", "I3", false);
board.Player1Hand.Count.Should().Be(4);
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight);
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn);
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
board.WhoseTurn.Should().Be(WhichPlayer.Player1);
// 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>();
board["H9"].Should().BeNull();
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight);
// 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>();
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>();
board["H8"].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>();
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>();
board["H9"].Should().BeNull();
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
// Act | Assert - Illegally place Lance from the hand.
board["H9"].Should().BeNull();
act = () => shogi.Move(WhichPiece.Knight, "H9");
act.Should().Throw<InvalidOperationException>();
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>();
board["H9"].Should().BeNull();
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn);
// Act | Assert - Illegally place Pawn from the hand.
board["H9"].Should().BeNull();
act = () => shogi.Move(WhichPiece.Pawn, "H9");
act.Should().Throw<InvalidOperationException>();
board["H9"].Should().BeNull();
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn);
// // Act | Assert - Illegally place Pawn from the hand in a row which already has an unpromoted Pawn.
// // TODO
}
// // Act | Assert - Illegally place Pawn from the hand in a row which already has an unpromoted Pawn.
// // TODO
}
[Fact]
public void PreventInvalidDrop_Check()
{
// Arrange
var shogi = MockShogiBoard();
// P1 Pawn
shogi.Move("C3", "C4", false);
// P2 Pawn
shogi.Move("G7", "G6", false);
// P1 Pawn, arbitrary move.
shogi.Move("A3", "A4", false);
// P2 Bishop takes P1 Bishop
shogi.Move("H8", "B2", false);
// P1 Silver takes P2 Bishop
shogi.Move("C1", "B2", false);
// P2 Pawn, arbtrary move
shogi.Move("A7", "A6", false);
// P1 drop Bishop, place P2 in check
shogi.Move(WhichPiece.Bishop, "G7");
shogi.BoardState.InCheck.Should().Be(WhichPlayer.Player2);
shogi.BoardState.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.BoardState["E5"].Should().BeNull();
[Fact]
public void PreventInvalidDrop_Check()
{
// Arrange
var shogi = MockShogiBoard();
// P1 Pawn
shogi.Move("C3", "C4", false);
// P2 Pawn
shogi.Move("G7", "G6", false);
// P1 Pawn, arbitrary move.
shogi.Move("A3", "A4", false);
// P2 Bishop takes P1 Bishop
shogi.Move("H8", "B2", false);
// P1 Silver takes P2 Bishop
shogi.Move("C1", "B2", false);
// P2 Pawn, arbtrary move
shogi.Move("A7", "A6", false);
// P1 drop Bishop, place P2 in check
shogi.Move(WhichPiece.Bishop, "G7");
shogi.BoardState.InCheck.Should().Be(WhichPlayer.Player2);
shogi.BoardState.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.BoardState["E5"].Should().BeNull();
// Act - P2 places a Bishop while in check.
var act = () => shogi.Move(WhichPiece.Bishop, "E5");
// Act - P2 places a Bishop while in check.
var act = () => shogi.Move(WhichPiece.Bishop, "E5");
// Assert
using var scope = new AssertionScope();
act.Should().Throw<InvalidOperationException>();
shogi.BoardState["E5"].Should().BeNull();
shogi.BoardState.InCheck.Should().Be(WhichPlayer.Player2);
shogi.BoardState.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
}
// Assert
using var scope = new AssertionScope();
act.Should().Throw<InvalidOperationException>();
shogi.BoardState["E5"].Should().BeNull();
shogi.BoardState.InCheck.Should().Be(WhichPlayer.Player2);
shogi.BoardState.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
}
[Fact]
public void PreventInvalidDrop_Capture()
{
// Arrange
var shogi = MockShogiBoard();
// P1 Pawn
shogi.Move("C3", "C4", false);
// P2 Pawn
shogi.Move("G7", "G6", false);
// P1 Bishop capture P2 Bishop
shogi.Move("B2", "H8", false);
// P2 Pawn
shogi.Move("G6", "G5", false);
shogi.BoardState.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.BoardState["I9"].Should().NotBeNull();
shogi.BoardState["I9"]!.WhichPiece.Should().Be(WhichPiece.Lance);
shogi.BoardState["I9"]!.Owner.Should().Be(WhichPlayer.Player2);
[Fact]
public void PreventInvalidDrop_Capture()
{
// Arrange
var shogi = MockShogiBoard();
// P1 Pawn
shogi.Move("C3", "C4", false);
// P2 Pawn
shogi.Move("G7", "G6", false);
// P1 Bishop capture P2 Bishop
shogi.Move("B2", "H8", false);
// P2 Pawn
shogi.Move("G6", "G5", false);
shogi.BoardState.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.BoardState["I9"].Should().NotBeNull();
shogi.BoardState["I9"]!.WhichPiece.Should().Be(WhichPiece.Lance);
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");
// Act - P1 tries to place a piece where an opponent's piece resides.
var act = () => shogi.Move(WhichPiece.Bishop, "I9");
// Assert
using var scope = new AssertionScope();
act.Should().Throw<InvalidOperationException>();
shogi.BoardState.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.BoardState["I9"].Should().NotBeNull();
shogi.BoardState["I9"]!.WhichPiece.Should().Be(WhichPiece.Lance);
shogi.BoardState["I9"]!.Owner.Should().Be(WhichPlayer.Player2);
}
// Assert
using var scope = new AssertionScope();
act.Should().Throw<InvalidOperationException>();
shogi.BoardState.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.BoardState["I9"].Should().NotBeNull();
shogi.BoardState["I9"]!.WhichPiece.Should().Be(WhichPiece.Lance);
shogi.BoardState["I9"]!.Owner.Should().Be(WhichPlayer.Player2);
}
[Fact]
public void Check()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
// P1 Pawn
shogi.Move("C3", "C4", false);
// P2 Pawn
shogi.Move("G7", "G6", false);
[Fact]
public void Check()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
// P1 Pawn
shogi.Move("C3", "C4", false);
// P2 Pawn
shogi.Move("G7", "G6", false);
// Act - P1 Bishop, check
shogi.Move("B2", "G7", false);
// Act - P1 Bishop, check
shogi.Move("B2", "G7", false);
// Assert
board.InCheck.Should().Be(WhichPlayer.Player2);
}
// Assert
board.InCheck.Should().Be(WhichPlayer.Player2);
}
[Fact]
public void Promote()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
// P1 Pawn
shogi.Move("C3", "C4", false);
// P2 Pawn
shogi.Move("G7", "G6", false);
[Fact]
public void Promote()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
// P1 Pawn
shogi.Move("C3", "C4", false);
// P2 Pawn
shogi.Move("G7", "G6", false);
// Act - P1 moves across promote threshold.
shogi.Move("B2", "G7", true);
// Act - P1 moves across promote threshold.
shogi.Move("B2", "G7", true);
// Assert
using (new AssertionScope())
{
board["B2"].Should().BeNull();
board["G7"].Should().NotBeNull();
board["G7"]!.WhichPiece.Should().Be(WhichPiece.Bishop);
board["G7"]!.Owner.Should().Be(WhichPlayer.Player1);
board["G7"]!.IsPromoted.Should().BeTrue();
}
}
// Assert
using (new AssertionScope())
{
board["B2"].Should().BeNull();
board["G7"].Should().NotBeNull();
board["G7"]!.WhichPiece.Should().Be(WhichPiece.Bishop);
board["G7"]!.Owner.Should().Be(WhichPlayer.Player1);
board["G7"]!.IsPromoted.Should().BeTrue();
}
}
[Fact]
public void Capture()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
var p1Bishop = board["B2"];
p1Bishop!.WhichPiece.Should().Be(WhichPiece.Bishop);
shogi.Move("C3", "C4", false);
shogi.Move("G7", "G6", false);
[Fact]
public void Capture()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
var p1Bishop = board["B2"];
p1Bishop!.WhichPiece.Should().Be(WhichPiece.Bishop);
shogi.Move("C3", "C4", false);
shogi.Move("G7", "G6", false);
// Act - P1 Bishop captures P2 Bishop
shogi.Move("B2", "H8", false);
// Act - P1 Bishop captures P2 Bishop
shogi.Move("B2", "H8", false);
// Assert
board["B2"].Should().BeNull();
board["H8"].Should().Be(p1Bishop);
// Assert
board["B2"].Should().BeNull();
board["H8"].Should().Be(p1Bishop);
board
.Player1Hand
.Should()
.ContainSingle(p => p.WhichPiece == WhichPiece.Bishop && p.Owner == WhichPlayer.Player1);
}
board
.Player1Hand
.Should()
.ContainSingle(p => p.WhichPiece == WhichPiece.Bishop && p.Owner == WhichPlayer.Player1);
}
[Fact]
public void CheckMate()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
// P1 Rook
shogi.Move("H2", "E2", false);
// P2 Gold
shogi.Move("F9", "G8", false);
// P1 Pawn
shogi.Move("E3", "E4", false);
// P2 other Gold
shogi.Move("D9", "C8", false);
// P1 same Pawn
shogi.Move("E4", "E5", false);
// P2 Pawn
shogi.Move("E7", "E6", false);
// P1 Pawn takes P2 Pawn
shogi.Move("E5", "E6", false);
// P2 King
shogi.Move("E9", "E8", false);
// P1 Pawn promotes; threatens P2 King
shogi.Move("E6", "E7", true);
// P2 King retreat
shogi.Move("E8", "E9", false);
[Fact]
public void CheckMate()
{
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
// P1 Rook
shogi.Move("H2", "E2", false);
// P2 Gold
shogi.Move("F9", "G8", false);
// P1 Pawn
shogi.Move("E3", "E4", false);
// P2 other Gold
shogi.Move("D9", "C8", false);
// P1 same Pawn
shogi.Move("E4", "E5", false);
// P2 Pawn
shogi.Move("E7", "E6", false);
// P1 Pawn takes P2 Pawn
shogi.Move("E5", "E6", false);
// P2 King
shogi.Move("E9", "E8", false);
// P1 Pawn promotes; threatens P2 King
shogi.Move("E6", "E7", true);
// P2 King retreat
shogi.Move("E8", "E9", false);
// Act - P1 Pawn wins by checkmate.
shogi.Move("E7", "E8", false);
// 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);
}
// 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);
}
private static ShogiBoard MockShogiBoard() => new(BoardState.StandardStarting);
}
private static ShogiBoard MockShogiBoard() => new(BoardState.StandardStarting);
}
}