From e64f75e3cc8047233e9fb90b4e6d23a779e78964 Mon Sep 17 00:00:00 2001 From: Lucas Morgan Date: Wed, 3 Mar 2021 07:33:12 -0600 Subject: [PATCH] tests --- Benchmarking/Benchmarking.csproj | 1 + .../Gameboard.ShogiUI.BoardState.csproj | 1 + Gameboard.ShogiUI.BoardState/ShogiBoard.cs | 11 +-- ...board.ShogiUI.Sockets.ServiceModels.csproj | 1 + .../Gameboard.ShogiUI.Sockets.csproj | 1 + .../BoardState/ShogiBoardShould.cs | 86 +++++++++++++++++-- PathFinding/PathFinding.csproj | 1 + 7 files changed, 90 insertions(+), 12 deletions(-) diff --git a/Benchmarking/Benchmarking.csproj b/Benchmarking/Benchmarking.csproj index 78da0b4..470d7e6 100644 --- a/Benchmarking/Benchmarking.csproj +++ b/Benchmarking/Benchmarking.csproj @@ -2,6 +2,7 @@ net5.0 + true Exe diff --git a/Gameboard.ShogiUI.BoardState/Gameboard.ShogiUI.BoardState.csproj b/Gameboard.ShogiUI.BoardState/Gameboard.ShogiUI.BoardState.csproj index e1f728c..23d6a53 100644 --- a/Gameboard.ShogiUI.BoardState/Gameboard.ShogiUI.BoardState.csproj +++ b/Gameboard.ShogiUI.BoardState/Gameboard.ShogiUI.BoardState.csproj @@ -2,6 +2,7 @@ net5.0 + true diff --git a/Gameboard.ShogiUI.BoardState/ShogiBoard.cs b/Gameboard.ShogiUI.BoardState/ShogiBoard.cs index 43da70b..bb1cb62 100644 --- a/Gameboard.ShogiUI.BoardState/ShogiBoard.cs +++ b/Gameboard.ShogiUI.BoardState/ShogiBoard.cs @@ -16,7 +16,7 @@ namespace Gameboard.ShogiUI.BoardState { private delegate void MoveSetCallback(Piece piece, Vector2 position); private readonly PathFinder2D pathFinder; - public ShogiBoard validationBoard; + private ShogiBoard validationBoard; private Vector2 player1King; private Vector2 player2King; public IReadOnlyDictionary> Hands { get; } @@ -46,6 +46,7 @@ namespace Gameboard.ShogiUI.BoardState { if (!Move(moves[i])) { + // Todo: Add some smarts to know why a move was invalid. In check? Piece not found? etc. throw new InvalidOperationException($"Unable to construct ShogiBoard with the given move at index {i}."); } } @@ -135,16 +136,16 @@ namespace Gameboard.ShogiUI.BoardState { case WhichPiece.Knight: // Knight cannot be placed onto the farthest two ranks from the hand. - minimumY = WhoseTurn == WhichPlayer.Player1 ? 2 : 6; + minimumY = WhoseTurn == WhichPlayer.Player1 ? 6 : 2; break; case WhichPiece.Lance: case WhichPiece.Pawn: // Lance and Pawn cannot be placed onto the farthest rank from the hand. - minimumY = WhoseTurn == WhichPlayer.Player1 ? 1 : 7; + minimumY = WhoseTurn == WhichPlayer.Player1 ? 7 : 1; break; } - if (WhoseTurn == WhichPlayer.Player1 && move.To.Y < minimumY) return false; - if (WhoseTurn == WhichPlayer.Player2 && move.To.Y > minimumY) return false; + if (WhoseTurn == WhichPlayer.Player1 && move.To.Y > minimumY) return false; + if (WhoseTurn == WhichPlayer.Player2 && move.To.Y < minimumY) return false; // Mutate the board. Board[move.To.X, move.To.Y] = Hands[WhoseTurn][index]; diff --git a/Gameboard.ShogiUI.Sockets.ServiceModels/Gameboard.ShogiUI.Sockets.ServiceModels.csproj b/Gameboard.ShogiUI.Sockets.ServiceModels/Gameboard.ShogiUI.Sockets.ServiceModels.csproj index f208d30..423f013 100644 --- a/Gameboard.ShogiUI.Sockets.ServiceModels/Gameboard.ShogiUI.Sockets.ServiceModels.csproj +++ b/Gameboard.ShogiUI.Sockets.ServiceModels/Gameboard.ShogiUI.Sockets.ServiceModels.csproj @@ -2,6 +2,7 @@ net5.0 + true diff --git a/Gameboard.ShogiUI.Sockets/Gameboard.ShogiUI.Sockets.csproj b/Gameboard.ShogiUI.Sockets/Gameboard.ShogiUI.Sockets.csproj index 50c2ac9..053dcd9 100644 --- a/Gameboard.ShogiUI.Sockets/Gameboard.ShogiUI.Sockets.csproj +++ b/Gameboard.ShogiUI.Sockets/Gameboard.ShogiUI.Sockets.csproj @@ -2,6 +2,7 @@ net5.0 + true diff --git a/Gameboard.ShogiUI.UnitTests/BoardState/ShogiBoardShould.cs b/Gameboard.ShogiUI.UnitTests/BoardState/ShogiBoardShould.cs index 21eed69..e466a58 100644 --- a/Gameboard.ShogiUI.UnitTests/BoardState/ShogiBoardShould.cs +++ b/Gameboard.ShogiUI.UnitTests/BoardState/ShogiBoardShould.cs @@ -81,6 +81,24 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState shogi.Board[0, 3].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() { @@ -193,6 +211,66 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState shogi.Board[8, 7].Should().BeNull(); } + [TestMethod] + public void PreventInvalidDrops_MoveSet() + { + // Arrange + var moves = new[] + { + // P1 Pawn + new Move { From = new Vector2(2, 2), To = new Vector2(2, 3) }, + // P2 Pawn + new Move { From = new Vector2(0, 6), To = new Vector2(0, 5) }, + // P1 Bishop takes P2 Pawn + new Move { From = new Vector2(1, 1), To = new Vector2(6, 6) }, + // P2 Gold, block check from P1 Bishop. + new Move { From = new Vector2(5, 8), To = new Vector2(5, 7) }, + // P1 Bishop takes P2 Bishop, promotes so it can capture P2 Knight and P2 Lance + new Move { From = new Vector2(6, 6), To = new Vector2(7, 7), IsPromotion = true }, + // P2 Pawn again + new Move { From = new Vector2(0, 5), To = new Vector2(0, 4) }, + // P1 Bishop takes P2 Knight + new Move { From = new Vector2(7, 7), To = new Vector2(7, 8) }, + // P2 Pawn again + new Move { From = new Vector2(0, 4), To = new Vector2(0, 3) }, + // P1 Bishop takes P2 Lance + new Move { From = new Vector2(7, 8), To = new Vector2(8, 8) }, + // P2 Lance (move to make room for attempted P1 Pawn placement) + new Move { From = new Vector2(0, 8), To = new Vector2(0, 7) }, + // P1 arbitrary move + new Move { From = new Vector2(4, 0), To = new Vector2(4, 1) }, + // P2 Pawn again, takes P1 Pawn + new Move { From = new Vector2(0, 3), To = new Vector2(0, 2) }, + }; + var shogi = new ShogiBoard(moves); + shogi.PrintStateAsAscii(); + + // Prerequisite + 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); + + // Act | Assert - It is P1 turn + // try illegally placing Knight from the hand. + shogi.Board[7, 8].Should().BeNull(); + var moveSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Knight, To = new Vector2(7, 8) }); + shogi.PrintStateAsAscii(); + moveSuccess.Should().BeFalse(); + shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance); + shogi.Board[7, 8].Should().BeNull(); + + // Assert + //var pawnDropSuccess = shogi.Move(new Move) + + // Assert + } + + [TestMethod] + public void PreventInvalidDrop_Check() + { + + } + [TestMethod] public void Check() { @@ -316,16 +394,10 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState // Act - P1 Pawn wins by checkmate. var moveSuccess = shogi.Move(new Move { From = new Vector2(4, 6), To = new Vector2(4, 7) }); - Console.WriteLine("Checkmate"); - shogi.PrintStateAsAscii(); - shogi.Move(new Move { From = new Vector2(4, 8), To = new Vector2(4, 7) }); - shogi.PrintStateAsAscii(); - - // Assert + // Assert - checkmate moveSuccess.Should().BeTrue(); shogi.IsCheckmate.Should().BeTrue(); - } } } diff --git a/PathFinding/PathFinding.csproj b/PathFinding/PathFinding.csproj index f208d30..423f013 100644 --- a/PathFinding/PathFinding.csproj +++ b/PathFinding/PathFinding.csproj @@ -2,6 +2,7 @@ net5.0 + true