it fricken works!

This commit is contained in:
2021-03-02 19:08:10 -06:00
parent 3da187be17
commit 2e976c01e9
6 changed files with 41 additions and 32 deletions

View File

@@ -15,10 +15,10 @@ namespace Gameboard.ShogiUI.BoardState
public class ShogiBoard
{
private delegate void MoveSetCallback(Piece piece, Vector2 position);
private ShogiBoard validationBoard;
private readonly PathFinder2D<Piece> pathFinder;
public ShogiBoard validationBoard;
private Vector2 player1King;
private Vector2 player2King;
private PathFinder2D<Piece> pathFinder;
public IReadOnlyDictionary<WhichPlayer, List<Piece>> Hands { get; }
public Array2D<Piece> Board { get; }
public List<Move> MoveHistory { get; }
@@ -44,15 +44,11 @@ namespace Gameboard.ShogiUI.BoardState
{
for (var i = 0; i < moves.Count; i++)
{
if (!TryMove(moves[i]))
if (!Move(moves[i]))
{
throw new InvalidOperationException($"Unable to construct ShogiBoard with the given move at index {i}.");
}
}
if (EvaluateCheckAfterMove(WhoseTurn))
{
InCheck = WhoseTurn;
}
}
private ShogiBoard(ShogiBoard toCopy)
@@ -75,7 +71,7 @@ namespace Gameboard.ShogiUI.BoardState
public bool Move(Move move)
{
var otherPlayer = WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
var moveSuccess = TryMove(move);
if (!moveSuccess)
@@ -84,13 +80,10 @@ namespace Gameboard.ShogiUI.BoardState
}
// Evaluate check
if (EvaluateCheckAfterMove(WhoseTurn))
if (EvaluateCheckAfterMove(move, otherPlayer))
{
InCheck = WhoseTurn;
if (InCheck.HasValue)
{
//IsCheckmate = EvaluateCheckmate();
}
InCheck = otherPlayer;
IsCheckmate = EvaluateCheckmate();
}
return true;
}
@@ -114,11 +107,14 @@ namespace Gameboard.ShogiUI.BoardState
validationBoard = null;
return false;
}
// Assert that this move does not put the moving player in check.
if (validationBoard.EvaluateCheckAfterMove(WhoseTurn))
// If already in check, assert the move that resulted in check no longer results in check.
if (InCheck == WhoseTurn)
{
// Sneakily using this.WhoseTurn instead of validationBoard.WhoseTurn;
return false;
if (validationBoard.EvaluateCheckAfterMove(MoveHistory[^1], WhoseTurn))
{
// Sneakily using this.WhoseTurn instead of validationBoard.WhoseTurn;
return false;
}
}
// The move is valid and legal; update board state.
@@ -251,13 +247,11 @@ namespace Gameboard.ShogiUI.BoardState
Console.WriteLine(builder.ToString());
}
#region Rules Validation
private bool EvaluateCheckAfterMove(WhichPlayer whichPlayer)
private bool EvaluateCheckAfterMove(Move move, WhichPlayer whichPlayer)
{
var isCheck = false;
var kingPosition = whichPlayer == WhichPlayer.Player1 ? player1King : player2King;
// Get last move.
var move = MoveHistory[^1];
// Check if the move put the king in check.
if (pathFinder.PathTo(move.To, kingPosition)) return true;
@@ -327,10 +321,15 @@ namespace Gameboard.ShogiUI.BoardState
pathFinder.PathEvery(from, (other, position) =>
{
if (validationBoard == null) validationBoard = new ShogiBoard(this);
var moveSuccess = validationBoard.TryMove(new Move { From = from, To = position });
var moveToTry = new Move { From = from, To = position };
var moveSuccess = validationBoard.TryMove(moveToTry);
if (moveSuccess)
{
isCheckmate = false;
validationBoard = null;
if (!EvaluateCheckAfterMove(moveToTry, InCheck.Value))
{
isCheckmate = false;
}
}
});
}