convert to blazor server side render

This commit is contained in:
2025-12-24 16:43:51 -06:00
parent 357c3d9932
commit dcbf8a3ac3
215 changed files with 1867 additions and 2350 deletions

View File

@@ -1,15 +1,9 @@
using Shogi.Domain.ValueObjects;
using Shogi.Domains.ValueObjects;
namespace UnitTests;
public class ShogiShould
{
private readonly ITestOutputHelper console;
public ShogiShould(ITestOutputHelper console)
{
this.console = console;
}
[Fact]
public void MoveAPieceToAnEmptyPosition()
{
@@ -17,16 +11,16 @@ public class ShogiShould
var shogi = MockShogiBoard();
var board = shogi.BoardState;
board["A4"].Should().BeNull();
Assert.Null(board["A4"]);
var expectedPiece = board["A3"];
expectedPiece.Should().NotBeNull();
Assert.NotNull(expectedPiece);
// Act
shogi.Move("A3", "A4", false);
// Assert
board["A3"].Should().BeNull();
board["A4"].Should().Be(expectedPiece);
Assert.Null(board["A3"]);
Assert.Equal(expectedPiece, board["A4"]);
}
[Fact]
@@ -41,17 +35,14 @@ public class ShogiShould
shogi.Move("G7", "G6", false);
// P1 Bishop puts P2 in check
shogi.Move("B2", "G7", false);
board.InCheck.Should().Be(WhichPlayer.Player2);
Assert.Equal(WhichPlayer.Player2, board.InCheck);
// 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.Null(board.InCheck);
}
[Fact]
@@ -60,18 +51,18 @@ public class ShogiShould
// Arrange
var shogi = MockShogiBoard();
var board = shogi.BoardState;
board["D5"].Should().BeNull();
Assert.Null(board["D5"]);
// Act
var moveResult = shogi.Move("D5", "D6", false);
// Assert
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse();
board["D5"].Should().BeNull();
board["D6"].Should().BeNull();
board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty();
Assert.NotNull(moveResult);
Assert.False(moveResult.IsSuccess);
Assert.Null(board["D5"]);
Assert.Null(board["D6"]);
Assert.Empty(board.Player1Hand);
Assert.Empty(board.Player2Hand);
}
[Fact]
@@ -86,13 +77,11 @@ public class ShogiShould
var moveResult = shogi.Move("A3", "A3", false);
// Assert
using (new AssertionScope())
{
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse(); board["A3"].Should().Be(expectedPiece);
board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty();
}
Assert.NotNull(moveResult);
Assert.False(moveResult.IsSuccess);
Assert.Equal(expectedPiece, board["A3"]);
Assert.Empty(board.Player1Hand);
Assert.Empty(board.Player2Hand);
}
[Fact]
@@ -102,21 +91,18 @@ public class ShogiShould
var shogi = MockShogiBoard();
var board = shogi.BoardState;
var expectedPiece = board["D1"];
expectedPiece!.WhichPiece.Should().Be(WhichPiece.GoldGeneral);
Assert.Equal(WhichPiece.GoldGeneral, expectedPiece!.WhichPiece);
// Act - Move General illegally
var moveResult = shogi.Move("D1", "D5", false);
// Assert
using (new AssertionScope())
{
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse();
board["D1"].Should().Be(expectedPiece);
board["D5"].Should().BeNull();
board.Player1Hand.Should().BeEmpty();
board.Player2Hand.Should().BeEmpty();
}
Assert.NotNull(moveResult);
Assert.False(moveResult.IsSuccess);
Assert.Equal(expectedPiece, board["D1"]);
Assert.Null(board["D5"]);
Assert.Empty(board.Player1Hand);
Assert.Empty(board.Player2Hand);
}
[Fact]
@@ -126,19 +112,17 @@ public class ShogiShould
var shogi = MockShogiBoard();
var board = shogi.BoardState;
var expectedPiece = board["A7"];
expectedPiece!.Owner.Should().Be(WhichPlayer.Player2);
board.WhoseTurn.Should().Be(WhichPlayer.Player1);
Assert.Equal(WhichPlayer.Player2, expectedPiece!.Owner);
Assert.Equal(WhichPlayer.Player1, board.WhoseTurn);
// Act - Move Player2 Pawn when it is Player1 turn.
var moveResult = shogi.Move("A7", "A6", false);
// Assert
using (new AssertionScope())
{
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse(); board["A7"].Should().Be(expectedPiece);
board["A6"].Should().BeNull();
}
Assert.NotNull(moveResult);
Assert.False(moveResult.IsSuccess);
Assert.Equal(expectedPiece, board["A7"]);
Assert.Null(board["A6"]);
}
[Fact]
@@ -149,19 +133,17 @@ public class ShogiShould
var board = shogi.BoardState;
var lance = board["A1"];
var pawn = board["A3"];
lance!.Owner.Should().Be(pawn!.Owner);
Assert.Equal(lance!.Owner, pawn!.Owner);
// Act - Move P1 Lance through P1 Pawn.
var moveResult = shogi.Move("A1", "A5", false);
// Assert
using (new AssertionScope())
{
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse(); board["A1"].Should().Be(lance);
board["A3"].Should().Be(pawn);
board["A5"].Should().BeNull();
}
Assert.NotNull(moveResult);
Assert.False(moveResult.IsSuccess);
Assert.Equal(lance, board["A1"]);
Assert.Equal(pawn, board["A3"]);
Assert.Null(board["A5"]);
}
[Fact]
@@ -172,20 +154,18 @@ public class ShogiShould
var board = shogi.BoardState;
var knight = board["B1"];
var pawn = board["C3"];
knight!.Owner.Should().Be(pawn!.Owner);
Assert.Equal(knight!.Owner, pawn!.Owner);
// Act - P1 Knight tries to capture P1 Pawn.
var moveResult = shogi.Move("B1", "C3", false);
// Arrange
using (new AssertionScope())
{
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();
}
// Assert
Assert.NotNull(moveResult);
Assert.False(moveResult.IsSuccess);
Assert.Equal(knight, board["B1"]);
Assert.Equal(pawn, board["C3"]);
Assert.Empty(board.Player1Hand);
Assert.Empty(board.Player2Hand);
}
[Fact]
@@ -200,20 +180,18 @@ public class ShogiShould
shogi.Move("G7", "G6", false);
// P1 Bishop puts P2 in check
shogi.Move("B2", "G7", false);
board.InCheck.Should().Be(WhichPlayer.Player2);
Assert.Equal(WhichPlayer.Player2, board.InCheck);
var lance = board["I9"];
// Act - P2 moves Lance while in check.
var moveResult = shogi.Move("I9", "I8", false);
// Assert
using (new AssertionScope())
{
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse(); board.InCheck.Should().Be(WhichPlayer.Player2);
board["I9"].Should().Be(lance);
board["I8"].Should().BeNull();
}
Assert.NotNull(moveResult);
Assert.False(moveResult.IsSuccess);
Assert.Equal(WhichPlayer.Player2, board.InCheck);
Assert.Equal(lance, board["I9"]);
Assert.Null(board["I8"]);
}
[Fact]
@@ -242,44 +220,44 @@ public class ShogiShould
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);
Assert.Equal(4, board.Player1Hand.Count);
Assert.Single(board.Player1Hand, p => p.WhichPiece == WhichPiece.Knight);
Assert.Single(board.Player1Hand, p => p.WhichPiece == WhichPiece.Lance);
Assert.Single(board.Player1Hand, p => p.WhichPiece == WhichPiece.Pawn);
Assert.Single(board.Player1Hand, p => p.WhichPiece == WhichPiece.Bishop);
Assert.Equal(WhichPlayer.Player1, board.WhoseTurn);
// Act | Assert - Illegally placing Knight from the hand in farthest rank.
board["H9"].Should().BeNull();
Assert.Null(board["H9"]);
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);
Assert.NotNull(moveResult);
Assert.False(moveResult.IsSuccess);
Assert.Null(board["H9"]);
Assert.Single(board.Player1Hand, p => p.WhichPiece == WhichPiece.Knight);
// Act | Assert - Illegally placing Knight from the hand in second farthest row.
board["H8"].Should().BeNull();
Assert.Null(board["H8"]);
moveResult = shogi.Move(WhichPiece.Knight, "H8");
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse();
board["H8"].Should().BeNull();
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight);
Assert.NotNull(moveResult);
Assert.False(moveResult.IsSuccess);
Assert.Null(board["H8"]);
Assert.Single(board.Player1Hand, p => p.WhichPiece == WhichPiece.Knight);
// Act | Assert - Illegally place Lance from the hand.
board["H9"].Should().BeNull();
Assert.Null(board["H9"]);
moveResult = shogi.Move(WhichPiece.Knight, "H9");
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse();
board["H9"].Should().BeNull();
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
Assert.NotNull(moveResult);
Assert.False(moveResult.IsSuccess);
Assert.Null(board["H9"]);
Assert.Single(board.Player1Hand, p => p.WhichPiece == WhichPiece.Lance);
// Act | Assert - Illegally place Pawn from the hand.
board["H9"].Should().BeNull();
Assert.Null(board["H9"]);
moveResult = shogi.Move(WhichPiece.Pawn, "H9");
moveResult.Should().NotBeNull();
moveResult.IsSuccess.Should().BeFalse();
board["H9"].Should().BeNull();
board.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn);
Assert.NotNull(moveResult);
Assert.False(moveResult.IsSuccess);
Assert.Null(board["H9"]);
Assert.Single(board.Player1Hand, p => p.WhichPiece == WhichPiece.Pawn);
// // Act | Assert - Illegally place Pawn from the hand in a row which already has an unpromoted Pawn.
// // TODO
@@ -291,33 +269,32 @@ public class ShogiShould
// Arrange
var shogi = MockShogiBoard();
// P1 Pawn
shogi.Move("C3", "C4", false).IsSuccess.Should().BeTrue();
Assert.True(shogi.Move("C3", "C4", false).IsSuccess);
// P2 Pawn
shogi.Move("G7", "G6", false).IsSuccess.Should().BeTrue();
Assert.True(shogi.Move("G7", "G6", false).IsSuccess);
// P1 Pawn, arbitrary move.
shogi.Move("A3", "A4", false).IsSuccess.Should().BeTrue();
Assert.True(shogi.Move("A3", "A4", false).IsSuccess);
// P2 Bishop takes P1 Bishop
shogi.Move("H8", "B2", false).IsSuccess.Should().BeTrue();
Assert.True(shogi.Move("H8", "B2", false).IsSuccess);
// P1 Silver takes P2 Bishop
shogi.Move("C1", "B2", false).IsSuccess.Should().BeTrue();
Assert.True(shogi.Move("C1", "B2", false).IsSuccess);
// P2 Pawn, arbtrary move
shogi.Move("A7", "A6", false).IsSuccess.Should().BeTrue();
Assert.True(shogi.Move("A7", "A6", false).IsSuccess);
// P1 drop Bishop, place P2 in check
shogi.Move(WhichPiece.Bishop, "G7").IsSuccess.Should().BeTrue();
shogi.BoardState.InCheck.Should().Be(WhichPlayer.Player2);
shogi.BoardState.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.BoardState["E5"].Should().BeNull();
Assert.True(shogi.Move(WhichPiece.Bishop, "G7").IsSuccess);
Assert.Equal(WhichPlayer.Player2, shogi.BoardState.InCheck);
Assert.Single(shogi.BoardState.Player2Hand, p => p.WhichPiece == WhichPiece.Bishop);
Assert.Null(shogi.BoardState["E5"]);
// Act - P2 places a Bishop while in check.
var moveResult = shogi.Move(WhichPiece.Bishop, "E5");
// Assert
using var scope = new AssertionScope();
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);
Assert.NotNull(moveResult);
Assert.False(moveResult.IsSuccess);
Assert.Null(shogi.BoardState["E5"]);
Assert.Equal(WhichPlayer.Player2, shogi.BoardState.InCheck);
Assert.Single(shogi.BoardState.Player2Hand, p => p.WhichPiece == WhichPiece.Bishop);
}
[Fact]
@@ -333,22 +310,21 @@ public class ShogiShould
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);
Assert.Single(shogi.BoardState.Player1Hand, p => p.WhichPiece == WhichPiece.Bishop);
Assert.NotNull(shogi.BoardState["I9"]);
Assert.Equal(WhichPiece.Lance, shogi.BoardState["I9"]!.WhichPiece);
Assert.Equal(WhichPlayer.Player2, shogi.BoardState["I9"]!.Owner);
// Act - P1 tries to place a piece where an opponent's piece resides.
var moveResult = shogi.Move(WhichPiece.Bishop, "I9");
// Assert
using var scope = new AssertionScope();
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);
shogi.BoardState["I9"]!.Owner.Should().Be(WhichPlayer.Player2);
Assert.NotNull(moveResult);
Assert.False(moveResult.IsSuccess);
Assert.Single(shogi.BoardState.Player1Hand, p => p.WhichPiece == WhichPiece.Bishop);
Assert.NotNull(shogi.BoardState["I9"]);
Assert.Equal(WhichPiece.Lance, shogi.BoardState["I9"]!.WhichPiece);
Assert.Equal(WhichPlayer.Player2, shogi.BoardState["I9"]!.Owner);
}
[Fact]
@@ -366,7 +342,7 @@ public class ShogiShould
shogi.Move("B2", "G7", false);
// Assert
board.InCheck.Should().Be(WhichPlayer.Player2);
Assert.Equal(WhichPlayer.Player2, board.InCheck);
}
[Fact]
@@ -384,14 +360,11 @@ public class ShogiShould
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.Null(board["B2"]);
Assert.NotNull(board["G7"]);
Assert.Equal(WhichPiece.Bishop, board["G7"]!.WhichPiece);
Assert.Equal(WhichPlayer.Player1, board["G7"]!.Owner);
Assert.True(board["G7"]!.IsPromoted);
}
[Fact]
@@ -401,7 +374,7 @@ public class ShogiShould
var shogi = MockShogiBoard();
var board = shogi.BoardState;
var p1Bishop = board["B2"];
p1Bishop!.WhichPiece.Should().Be(WhichPiece.Bishop);
Assert.Equal(WhichPiece.Bishop, p1Bishop!.WhichPiece);
shogi.Move("C3", "C4", false);
shogi.Move("G7", "G6", false);
@@ -409,13 +382,9 @@ public class ShogiShould
shogi.Move("B2", "H8", false);
// Assert
board["B2"].Should().BeNull();
board["H8"].Should().Be(p1Bishop);
board
.Player1Hand
.Should()
.ContainSingle(p => p.WhichPiece == WhichPiece.Bishop && p.Owner == WhichPlayer.Player1);
Assert.Null(board["B2"]);
Assert.Equal(p1Bishop, board["H8"]);
Assert.Single(board.Player1Hand, p => p.WhichPiece == WhichPiece.Bishop && p.Owner == WhichPlayer.Player1);
}
[Fact]
@@ -449,8 +418,8 @@ public class ShogiShould
shogi.Move("E7", "E8", false);
// Assert - checkmate
board.IsCheckmate.Should().BeTrue();
board.InCheck.Should().Be(WhichPlayer.Player2);
Assert.True(board.IsCheckmate);
Assert.Equal(WhichPlayer.Player2, board.InCheck);
}
private static ShogiBoard MockShogiBoard() => new(BoardState.StandardStarting);