This commit is contained in:
2021-03-01 20:55:37 -06:00
parent f55716d2ec
commit 3da187be17
5 changed files with 154 additions and 109 deletions

View File

@@ -44,11 +44,15 @@ namespace Gameboard.ShogiUI.BoardState
{
for (var i = 0; i < moves.Count; i++)
{
if (!Move(moves[i]))
if (!TryMove(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)
@@ -72,7 +76,6 @@ namespace Gameboard.ShogiUI.BoardState
public bool Move(Move move)
{
var otherPlayer = WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
var moveSuccess = TryMove(move);
if (!moveSuccess)
@@ -81,10 +84,13 @@ namespace Gameboard.ShogiUI.BoardState
}
// Evaluate check
InCheck = EvaluateCheck(otherPlayer) ? otherPlayer : null;
if (InCheck.HasValue)
if (EvaluateCheckAfterMove(WhoseTurn))
{
//IsCheckmate = EvaluateCheckmate();
InCheck = WhoseTurn;
if (InCheck.HasValue)
{
//IsCheckmate = EvaluateCheckmate();
}
}
return true;
}
@@ -109,47 +115,17 @@ namespace Gameboard.ShogiUI.BoardState
return false;
}
// Assert that this move does not put the moving player in check.
if (validationBoard.EvaluateCheck(WhoseTurn)) return false;
if (validationBoard.EvaluateCheckAfterMove(WhoseTurn))
{
// Sneakily using this.WhoseTurn instead of validationBoard.WhoseTurn;
return false;
}
// The move is valid and legal; update board state.
if (move.PieceFromCaptured.HasValue) PlaceFromHand(move);
else PlaceFromBoard(move);
return true;
}
private bool EvaluateCheckmate()
{
if (!InCheck.HasValue) return false;
// Assume true and try to disprove.
var isCheckmate = true;
Board.ForEachNotNull((piece, x, y) => // For each piece...
{
if (!isCheckmate) return; // Short circuit
var from = new Vector2(x, y);
if (piece.Owner == InCheck) // Owned by the player in check...
{
var positionsToCheck = new List<Vector2>(10);
//IterateMoveSet(from, (innerPiece, position) =>
//{
// if (innerPiece?.Owner != InCheck) positionsToCheck.Add(position); // Find possible moves...
//});
// And evaluate if any move gets the player out of check.
foreach (var position in positionsToCheck)
{
if (validationBoard == null) validationBoard = new ShogiBoard(this);
var moveSuccess = validationBoard.TryMove(new Move { From = from, To = position });
if (moveSuccess)
{
isCheckmate &= validationBoard.EvaluateCheck(InCheck.Value);
validationBoard = null;
}
}
}
});
return isCheckmate;
}
/// <returns>True if the move was successful.</returns>
private bool PlaceFromHand(Move move)
{
@@ -186,7 +162,7 @@ namespace Gameboard.ShogiUI.BoardState
var fromPiece = Board[move.From.X, move.From.Y];
if (fromPiece == null) return false; // Invalid move
if (fromPiece.Owner != WhoseTurn) return false; // Invalid move; cannot move other players pieces.
if (IsPathable(move.From, move.To, fromPiece) == false) return false; // Invalid move; move not part of move-set.
if (IsPathable(move.From, move.To) == false) return false; // Invalid move; move not part of move-set.
var captured = Board[move.To.X, move.To.Y];
if (captured != null)
@@ -227,10 +203,13 @@ namespace Gameboard.ShogiUI.BoardState
return true;
}
private bool IsPathable(Vector2 from, Vector2 to, Piece piece)
private bool IsPathable(Vector2 from, Vector2 to)
{
var piece = Board[from.X, from.Y];
if (piece == null) return false;
var isObstructed = false;
var isPathable = pathFinder.PathTo(piece, from, to, (other, position) =>
var isPathable = pathFinder.PathTo(from, to, (other, position) =>
{
if (other.Owner == piece.Owner) isObstructed = true;
});
@@ -272,39 +251,92 @@ namespace Gameboard.ShogiUI.BoardState
Console.WriteLine(builder.ToString());
}
#region Rules Validation
/// <summary>
/// Evaluate if a player is in check given the current board state.
/// </summary>
private bool EvaluateCheck(WhichPlayer whichPlayer)
private bool EvaluateCheckAfterMove(WhichPlayer whichPlayer)
{
var destination = whichPlayer == WhichPlayer.Player1 ? player1King : player2King;
var inCheck = false;
// Iterate every board piece...
Board.ForEachNotNull((piece, x, y) =>
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;
// Get line equation from king through the now-unoccupied location.
var direction = Vector2.Subtract(kingPosition, move.From);
var slope = Math.Abs(direction.Y / direction.X);
// If absolute slope is 45°, look for a bishop along the line.
// If absolute slope is 0° or 90°, look for a rook along the line.
// if absolute slope is 0°, look for lance along the line.
if (float.IsInfinity(slope))
{
var origin = new Vector2(x, y);
// ...that belongs to the opponent within range...
if (piece.Owner != whichPlayer && pathFinder.IsPathable(origin, destination, piece))
// if slope of the move is also infinity...can skip this?
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
{
pathFinder.PathTo(piece, origin, destination, (threatenedPiece, position) =>
if (piece.Owner != whichPlayer)
{
// ...and threatens the player's king.
inCheck |=
threatenedPiece.WhichPiece == WhichPiece.King
&& threatenedPiece.Owner == whichPlayer;
});
switch (piece.WhichPiece)
{
case WhichPiece.Rook:
isCheck = true;
break;
case WhichPiece.Lance:
if (!piece.IsPromoted) isCheck = true;
break;
}
}
});
}
else if (slope == 1)
{
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
{
if (piece.Owner != whichPlayer && piece.WhichPiece == WhichPiece.Bishop)
{
isCheck = true;
}
});
}
else if (slope == 0)
{
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
{
if (piece.Owner != whichPlayer && piece.WhichPiece == WhichPiece.Rook)
{
isCheck = true;
}
});
}
return isCheck;
}
private bool EvaluateCheckmate()
{
if (!InCheck.HasValue) return false;
// Assume true and try to disprove.
var isCheckmate = true;
Board.ForEachNotNull((piece, x, y) => // For each piece...
{
// Short circuit
if (!isCheckmate) return;
var from = new Vector2(x, y);
if (piece.Owner == InCheck) // ...owned by the player in check...
{
// ...evaluate if any move gets the player out of check.
pathFinder.PathEvery(from, (other, position) =>
{
if (validationBoard == null) validationBoard = new ShogiBoard(this);
var moveSuccess = validationBoard.TryMove(new Move { From = from, To = position });
if (moveSuccess)
{
isCheckmate = false;
}
});
}
});
return inCheck;
return isCheckmate;
}
/// <summary>
/// Iterate through the possible moves of a piece at a given position.
/// </summary>
/// <summary>
/// Useful for iterating the board for pieces that move many spaces.
/// </summary>
/// <param name="callback">A function that returns true if walking should continue.</param>
#endregion
#region Initialize
@@ -362,18 +394,5 @@ namespace Gameboard.ShogiUI.BoardState
ResetRearRow(WhichPlayer.Player2);
}
#endregion
//public static ShogiBoard ConstructWithMoves(IList<Move> moves)
//{
// var s = new ShogiBoard();
// for (var i = 0; i < moves.Count; i++)
// {
// if (!s.Move(moves[i]))
// {
// throw new InvalidOperationException($"Unable to construct ShogiBoard with the given move at index {i}.");
// }
// }
// return s;
//}
}
}