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