This commit is contained in:
2021-08-01 17:32:43 -05:00
parent 178cb00253
commit b10f61a489
76 changed files with 1655 additions and 1185 deletions

View File

@@ -3,464 +3,13 @@ using Gameboard.ShogiUI.Sockets.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using System.Numerics;
using WhichPlayer = Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types.WhichPlayer;
using WhichPiece = Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types.WhichPiece;
using WhichPlayer = Gameboard.ShogiUI.Sockets.ServiceModels.Types.WhichPlayer;
using WhichPiece = Gameboard.ShogiUI.Sockets.ServiceModels.Types.WhichPiece;
namespace Gameboard.ShogiUI.UnitTests.Rules
{
[TestClass]
public class ShogiBoardShould
{
[TestMethod]
public void InitializeBoardState()
{
// Assert
var board = new Shogi().Board;
// Assert pieces do not start promoted.
foreach (var piece in board) piece?.IsPromoted.Should().BeFalse();
// Assert Player1.
for (var y = 0; y < 3; y++)
for (var x = 0; x < 9; x++)
board[y, x]?.Owner.Should().Be(WhichPlayer.Player2);
board[0, 0].WhichPiece.Should().Be(WhichPiece.Lance);
board[0, 1].WhichPiece.Should().Be(WhichPiece.Knight);
board[0, 2].WhichPiece.Should().Be(WhichPiece.SilverGeneral);
board[0, 3].WhichPiece.Should().Be(WhichPiece.GoldGeneral);
board[0, 4].WhichPiece.Should().Be(WhichPiece.King);
board[0, 5].WhichPiece.Should().Be(WhichPiece.GoldGeneral);
board[0, 6].WhichPiece.Should().Be(WhichPiece.SilverGeneral);
board[0, 7].WhichPiece.Should().Be(WhichPiece.Knight);
board[0, 8].WhichPiece.Should().Be(WhichPiece.Lance);
board[1, 0].Should().BeNull();
board[1, 1].WhichPiece.Should().Be(WhichPiece.Rook);
for (var x = 2; x < 7; x++) board[1, x].Should().BeNull();
board[1, 7].WhichPiece.Should().Be(WhichPiece.Bishop);
board[1, 8].Should().BeNull();
for (var x = 0; x < 9; x++) board[2, x].WhichPiece.Should().Be(WhichPiece.Pawn);
// Assert empty locations.
for (var y = 3; y < 6; y++)
for (var x = 0; x < 9; x++)
board[y, x].Should().BeNull();
// Assert Player2.
for (var y = 6; y < 9; y++)
for (var x = 0; x < 9; x++)
board[y, x]?.Owner.Should().Be(WhichPlayer.Player1);
board[8, 0].WhichPiece.Should().Be(WhichPiece.Lance);
board[8, 1].WhichPiece.Should().Be(WhichPiece.Knight);
board[8, 2].WhichPiece.Should().Be(WhichPiece.SilverGeneral);
board[8, 3].WhichPiece.Should().Be(WhichPiece.GoldGeneral);
board[8, 4].WhichPiece.Should().Be(WhichPiece.King);
board[8, 5].WhichPiece.Should().Be(WhichPiece.GoldGeneral);
board[8, 6].WhichPiece.Should().Be(WhichPiece.SilverGeneral);
board[8, 7].WhichPiece.Should().Be(WhichPiece.Knight);
board[8, 8].WhichPiece.Should().Be(WhichPiece.Lance);
board[7, 0].Should().BeNull();
board[7, 1].WhichPiece.Should().Be(WhichPiece.Bishop);
for (var x = 2; x < 7; x++) board[7, x].Should().BeNull();
board[7, 7].WhichPiece.Should().Be(WhichPiece.Rook);
board[7, 8].Should().BeNull();
for (var x = 0; x < 9; x++) board[6, x].WhichPiece.Should().Be(WhichPiece.Pawn);
}
[TestMethod]
public void InitializeBoardStateWithMoves()
{
var moves = new[]
{
// Pawn
new Move(new Vector2(0, 6), new Vector2(0, 5))
};
var shogi = new Shogi(moves);
shogi.Board[6, 0].Should().BeNull();
shogi.Board[5, 0].WhichPiece.Should().Be(WhichPiece.Pawn);
}
[TestMethod]
public void PreventInvalidMoves_MoveFromEmptyPosition()
{
// Arrange
var shogi = new Shogi();
// Prerequisit
shogi.Board[4, 4].Should().BeNull();
// Act
var moveSuccess = shogi.Move(new Move(new Vector2(4, 4), new Vector2(4, 5)));
// Assert
moveSuccess.Should().BeFalse();
shogi.Board[4, 4].Should().BeNull();
shogi.Board[5, 4].Should().BeNull();
}
[TestMethod]
public void PreventInvalidMoves_MoveToCurrentPosition()
{
// Arrange
var shogi = new Shogi();
// Act - P1 "moves" pawn to the position it already exists at.
var moveSuccess = shogi.Move(new Move(new Vector2(0, 6), new Vector2(0, 6)));
// Assert
moveSuccess.Should().BeFalse();
shogi.Board[6, 0].WhichPiece.Should().Be(WhichPiece.Pawn);
}
[TestMethod]
public void PreventInvalidMoves_MoveSet()
{
// Bishop moving lateral
var invalidLanceMove = new Move(new Vector2(1, 1), new Vector2(2, 1));
var shogi = new Shogi();
var moveSuccess = shogi.Move(invalidLanceMove);
moveSuccess.Should().BeFalse();
// Assert the Lance has not actually moved.
shogi.Board[0, 0].WhichPiece.Should().Be(WhichPiece.Lance);
}
[TestMethod]
public void PreventInvalidMoves_Ownership()
{
// Arrange
var shogi = new Shogi();
shogi.WhoseTurn.Should().Be(WhichPlayer.Player1);
shogi.Board[2, 8].Owner.Should().Be(WhichPlayer.Player2);
// Act - Move Player2 Pawn when it's Player1 turn.
var moveSuccess = shogi.Move(new Move(new Vector2(8, 2), new Vector2(8, 3)));
// Assert
moveSuccess.Should().BeFalse();
shogi.Board[6, 8].WhichPiece.Should().Be(WhichPiece.Pawn);
shogi.Board[5, 8].Should().BeNull();
}
[TestMethod]
public void PreventInvalidMoves_MoveThroughAllies()
{
// Lance moving through the pawn before it.
var invalidLanceMove = new Move(new Vector2(0, 8), new Vector2(0, 4));
var shogi = new Shogi();
var moveSuccess = shogi.Move(invalidLanceMove);
moveSuccess.Should().BeFalse();
// Assert the Lance has not actually moved.
shogi.Board[0, 0].WhichPiece.Should().Be(WhichPiece.Lance);
}
[TestMethod]
public void PreventInvalidMoves_CaptureAlly()
{
// Knight capturing allied Pawn
var invalidKnightMove = new Move(new Vector2(1, 8), new Vector2(0, 6));
var shogi = new Shogi();
var moveSuccess = shogi.Move(invalidKnightMove);
moveSuccess.Should().BeFalse();
// Assert the Knight has not actually moved or captured.
shogi.Board[0, 1].WhichPiece.Should().Be(WhichPiece.Knight);
shogi.Board[2, 0].WhichPiece.Should().Be(WhichPiece.Pawn);
}
[TestMethod]
public void PreventInvalidMoves_Check()
{
// Arrange
var moves = new[]
{
// P1 Pawn
new Move(new Vector2(2, 6), new Vector2(2, 5)),
// P2 Pawn
new Move(new Vector2(6, 2), new Vector2(6, 3)),
// P1 Bishop puts P2 in check
new Move(new Vector2(1, 7), new Vector2(6, 2))
};
var shogi = new Shogi(moves);
// Prerequisit
shogi.InCheck.Should().Be(WhichPlayer.Player2);
// Act - P2 moves Lance while remaining in check.
var moveSuccess = shogi.Move(new Move(new Vector2(0, 8), new Vector2(0, 7)));
// Assert
moveSuccess.Should().BeFalse();
shogi.InCheck.Should().Be(WhichPlayer.Player2);
shogi.Board[8, 8].WhichPiece.Should().Be(WhichPiece.Lance);
shogi.Board[7, 8].Should().BeNull();
}
[TestMethod]
public void PreventInvalidDrops_MoveSet()
{
// Arrange
var moves = new[]
{
// P1 Pawn
new Move(new Vector2(2, 6), new Vector2(2, 5) ),
// P2 Pawn
new Move(new Vector2(0, 2), new Vector2(0, 3) ),
// P1 Bishop takes P2 Pawn
new Move(new Vector2(1, 7), new Vector2(6, 2) ),
// P2 Gold, block check from P1 Bishop.
new Move(new Vector2(5, 0), new Vector2(5, 1) ),
// P1 Bishop takes P2 Bishop, promotes so it can capture P2 Knight and P2 Lance
new Move(new Vector2(6, 2), new Vector2(7, 1), true ),
// P2 Pawn again
new Move(new Vector2(0, 3), new Vector2(0, 4) ),
// P1 Bishop takes P2 Knight
new Move(new Vector2(7, 1), new Vector2(7, 0) ),
// P2 Pawn again
new Move(new Vector2(0, 4), new Vector2(0, 5) ),
// P1 Bishop takes P2 Lance
new Move(new Vector2(7, 0), new Vector2(8, 0) ),
// P2 Lance (move to make room for attempted P1 Pawn placement)
new Move(new Vector2(0, 0), new Vector2(0, 1) ),
// P1 arbitrary move
new Move(new Vector2(4, 8), new Vector2(4, 7) ),
// P2 Pawn again, takes P1 Pawn
new Move(new Vector2(0, 5) , new Vector2(0, 6) ),
};
var shogi = new Shogi(moves);
// Prerequisites
shogi.Hands[WhichPlayer.Player1].Count.Should().Be(4);
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight);
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn);
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
// Act | Assert - It is P1 turn
/// try illegally placing Knight from the hand.
shogi.Board[0, 7].Should().BeNull();
var dropSuccess = shogi.Move(new Move(WhichPiece.Knight, new Vector2(7, 0)));
dropSuccess.Should().BeFalse();
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
shogi.Board[0, 7].Should().BeNull();
dropSuccess = shogi.Move(new Move(WhichPiece.Knight, new Vector2(7, 1)));
dropSuccess.Should().BeFalse();
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
shogi.Board[1, 7].Should().BeNull();
/// try illegally placing Pawn from the hand
dropSuccess = shogi.Move(new Move(WhichPiece.Pawn, new Vector2(7, 0)));
dropSuccess.Should().BeFalse();
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn);
shogi.Board[0, 7].Should().BeNull();
/// try illegally placing Lance from the hand
dropSuccess = shogi.Move(new Move(WhichPiece.Lance, new Vector2(7, 0)));
dropSuccess.Should().BeFalse();
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
shogi.Board[0, 7].Should().BeNull();
}
[TestMethod]
public void PreventInvalidDrop_Check()
{
// Arrange
var moves = new[]
{
// P1 Pawn
new Move(new Vector2(2, 6), new Vector2(2, 5)),
// P2 Pawn
new Move(new Vector2(8, 2), new Vector2(8, 3)),
// P1 Bishop, check
new Move(new Vector2(1, 7), new Vector2(6, 2)),
// P2 Gold, block check
new Move(new Vector2(5, 0), new Vector2(5, 1)),
// P1 arbitrary move
new Move(new Vector2(0, 6), new Vector2(0, 5)),
// P2 Bishop
new Move(new Vector2(7, 1), new Vector2(8, 2)),
// P1 Bishop takes P2 Lance
new Move(new Vector2(6, 2), new Vector2(8, 0)),
// P2 Bishop
new Move(new Vector2(8, 2), new Vector2(7, 1)),
// P1 arbitrary move
new Move(new Vector2(0, 5), new Vector2(0, 4)),
// P2 Bishop, check
new Move(new Vector2(7, 1), new Vector2(2, 6)),
};
var shogi = new Shogi(moves);
// Prerequisites
shogi.InCheck.Should().Be(WhichPlayer.Player1);
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
// Act - P1 tries to place a Lance while in check.
var dropSuccess = shogi.Move(new Move(WhichPiece.Lance, new Vector2(4, 4)));
// Assert
dropSuccess.Should().BeFalse();
shogi.Board[4, 4].Should().BeNull();
shogi.InCheck.Should().Be(WhichPlayer.Player1);
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
}
[TestMethod]
public void PreventInvalidDrop_Capture()
{
// Arrange
var moves = new[]
{
// P1 Pawn
new Move(new Vector2(2, 6), new Vector2(2, 5)),
// P2 Pawn
new Move(new Vector2(6, 2), new Vector2(6, 3)),
// P1 Bishop, capture P2 Pawn, check
new Move(new Vector2(1, 7), new Vector2(6, 2)),
// P2 Gold, block check
new Move(new Vector2(5, 0), new Vector2(5, 1)),
// P1 Bishop capture P2 Bishop
new Move(new Vector2(6, 2), new Vector2(7, 1)),
// P2 arbitrary move
new Move(new Vector2(0, 0), new Vector2(0, 1)),
};
var shogi = new Shogi(moves);
// Prerequisites
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.Board[0, 4].Should().NotBeNull();
// Act - P1 tries to place Bishop from hand to an already-occupied position
var dropSuccess = shogi.Move(new Move(WhichPiece.Bishop, new Vector2(4, 0)));
// Assert
dropSuccess.Should().BeFalse();
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.Board[0, 4].WhichPiece.Should().Be(WhichPiece.King);
}
[TestMethod]
public void Check()
{
// Arrange
var moves = new[]
{
// P1 Pawn
new Move(new Vector2(2, 6), new Vector2(2, 5) ),
// P2 Pawn
new Move(new Vector2(6, 2), new Vector2(6, 3) ),
};
var shogi = new Shogi(moves);
// Act - P1 Bishop, check
shogi.Move(new Move(new Vector2(1, 7), new Vector2(6, 2)));
// Assert
shogi.InCheck.Should().Be(WhichPlayer.Player2);
}
[TestMethod]
public void Capture()
{
// Arrange
var moves = new[]
{
// P1 Pawn
new Move(new Vector2(2, 6), new Vector2(2, 5)),
// P2 Pawn
new Move(new Vector2(6, 2), new Vector2(6, 3))
};
var shogi = new Shogi(moves);
// Act - P1 Bishop captures P2 Bishop
var moveSuccess = shogi.Move(new Move(new Vector2(1, 7), new Vector2(7, 1)));
// Assert
moveSuccess.Should().BeTrue();
shogi.Board
.Cast<Piece>()
.Count(piece => piece?.WhichPiece == WhichPiece.Bishop)
.Should()
.Be(1);
shogi.Board[7, 1].Should().BeNull();
shogi.Board[1, 7].WhichPiece.Should().Be(WhichPiece.Bishop);
shogi.Hands[WhichPlayer.Player1]
.Should()
.ContainSingle(piece => piece.WhichPiece == WhichPiece.Bishop && piece.Owner == WhichPlayer.Player1);
// Act - P2 Silver captures P1 Bishop
moveSuccess = shogi.Move(new Move(new Vector2(6, 0), new Vector2(7, 1)));
// Assert
moveSuccess.Should().BeTrue();
shogi.Board[0, 6].Should().BeNull();
shogi.Board[1, 7].WhichPiece.Should().Be(WhichPiece.SilverGeneral);
shogi.Board
.Cast<Piece>()
.Count(piece => piece?.WhichPiece == WhichPiece.Bishop)
.Should().Be(0);
shogi.Hands[WhichPlayer.Player2]
.Should()
.ContainSingle(piece => piece.WhichPiece == WhichPiece.Bishop && piece.Owner == WhichPlayer.Player2);
}
[TestMethod]
public void Promote()
{
// Arrange
var moves = new[]
{
// P1 Pawn
new Move(new Vector2(2, 6), new Vector2(2, 5) ),
// P2 Pawn
new Move(new Vector2(6, 2), new Vector2(6, 3) )
};
var shogi = new Shogi(moves);
// Act - P1 moves across promote threshold.
var moveSuccess = shogi.Move(new Move(new Vector2(1, 7), new Vector2(6, 2), true));
// Assert
moveSuccess.Should().BeTrue();
shogi.Board[7, 1].Should().BeNull();
shogi.Board[2, 6].Should().Match<Piece>(piece => piece.WhichPiece == WhichPiece.Bishop && piece.IsPromoted == true);
}
[TestMethod]
public void CheckMate()
{
// Arrange
var moves = new[]
{
// P1 Rook
new Move(new Vector2(7, 7), new Vector2(4, 7) ),
// P2 Gold
new Move(new Vector2(3, 0), new Vector2(2, 1) ),
// P1 Pawn
new Move(new Vector2(4, 6), new Vector2(4, 5) ),
// P2 other Gold
new Move(new Vector2(5, 0), new Vector2(6, 1) ),
// P1 same Pawn
new Move(new Vector2(4, 5), new Vector2(4, 4) ),
// P2 Pawn
new Move(new Vector2(4, 2), new Vector2(4, 3) ),
// P1 Pawn takes P2 Pawn
new Move(new Vector2(4, 4), new Vector2(4, 3) ),
// P2 King
new Move(new Vector2(4, 0), new Vector2(4, 1) ),
// P1 Pawn promotes, threatens P2 King
new Move(new Vector2(4, 3), new Vector2(4, 2), true ),
// P2 King retreat
new Move(new Vector2(4, 1), new Vector2(4, 0) ),
};
var shogi = new Shogi(moves);
// Act - P1 Pawn wins by checkmate.
var moveSuccess = shogi.Move(new Move(new Vector2(4, 2), new Vector2(4, 1)));
// Assert - checkmate
moveSuccess.Should().BeTrue();
shogi.IsCheckmate.Should().BeTrue();
}
}
}