This commit is contained in:
2021-03-03 07:33:12 -06:00
parent 2e976c01e9
commit e64f75e3cc
7 changed files with 90 additions and 12 deletions

View File

@@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
</PropertyGroup> </PropertyGroup>

View File

@@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -16,7 +16,7 @@ namespace Gameboard.ShogiUI.BoardState
{ {
private delegate void MoveSetCallback(Piece piece, Vector2 position); private delegate void MoveSetCallback(Piece piece, Vector2 position);
private readonly PathFinder2D<Piece> pathFinder; private readonly PathFinder2D<Piece> pathFinder;
public ShogiBoard validationBoard; private ShogiBoard validationBoard;
private Vector2 player1King; private Vector2 player1King;
private Vector2 player2King; private Vector2 player2King;
public IReadOnlyDictionary<WhichPlayer, List<Piece>> Hands { get; } public IReadOnlyDictionary<WhichPlayer, List<Piece>> Hands { get; }
@@ -46,6 +46,7 @@ namespace Gameboard.ShogiUI.BoardState
{ {
if (!Move(moves[i])) 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}."); 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: case WhichPiece.Knight:
// Knight cannot be placed onto the farthest two ranks from the hand. // 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; break;
case WhichPiece.Lance: case WhichPiece.Lance:
case WhichPiece.Pawn: case WhichPiece.Pawn:
// Lance and Pawn cannot be placed onto the farthest rank from the hand. // 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; break;
} }
if (WhoseTurn == WhichPlayer.Player1 && 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; if (WhoseTurn == WhichPlayer.Player2 && move.To.Y < minimumY) return false;
// Mutate the board. // Mutate the board.
Board[move.To.X, move.To.Y] = Hands[WhoseTurn][index]; Board[move.To.X, move.To.Y] = Hands[WhoseTurn][index];

View File

@@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -81,6 +81,24 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
shogi.Board[0, 3].WhichPiece.Should().Be(WhichPiece.Pawn); 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] [TestMethod]
public void PreventInvalidMoves_MoveToCurrentPosition() public void PreventInvalidMoves_MoveToCurrentPosition()
{ {
@@ -193,6 +211,66 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
shogi.Board[8, 7].Should().BeNull(); 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] [TestMethod]
public void Check() public void Check()
{ {
@@ -316,16 +394,10 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
// Act - P1 Pawn wins by checkmate. // Act - P1 Pawn wins by checkmate.
var moveSuccess = shogi.Move(new Move { From = new Vector2(4, 6), To = new Vector2(4, 7) }); 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) }); // Assert - checkmate
shogi.PrintStateAsAscii();
// Assert
moveSuccess.Should().BeTrue(); moveSuccess.Should().BeTrue();
shogi.IsCheckmate.Should().BeTrue(); shogi.IsCheckmate.Should().BeTrue();
} }
} }
} }

View File

@@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup> </PropertyGroup>
</Project> </Project>