Finish logic around placing pieces from the hand.

This commit is contained in:
2023-02-03 18:22:05 -06:00
parent dbdaaf8b30
commit 6a600bf2f7
5 changed files with 289 additions and 265 deletions

View File

@@ -3,6 +3,20 @@
/// <summary>
/// Represents a single piece being moved by a player from <paramref name="From"/> to <paramref name="To"/>.
/// </summary>
public record struct Move(Vector2 From, Vector2 To)
public readonly record struct Move
{
public Move(Vector2 from, Vector2 to)
{
From = from;
To = to;
}
public Move(WhichPiece pieceFromHand, Vector2 to)
{
PieceFromHand = pieceFromHand;
To = to;
}
public Vector2? From { get; }
public Vector2 To { get; }
public WhichPiece PieceFromHand { get; }
}

View File

@@ -44,12 +44,14 @@ public sealed class ShogiBoard
throw new InvalidOperationException("Unable to move because you are still in check.");
}
var otherPlayer = BoardState.WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
if (simulation.IsPlayerInCheckAfterMove())
if (simulation.DidPlayerPutThemselfInCheck())
{
throw new InvalidOperationException("Illegal move. This move places you in check.");
}
var otherPlayer = BoardState.WhoseTurn == WhichPlayer.Player1
? WhichPlayer.Player2
: WhichPlayer.Player1;
_ = rules.Move(from, to, isPromotion);
if (rules.IsOpponentInCheckAfterMove())
{
@@ -113,31 +115,36 @@ public sealed class ShogiBoard
throw new InvalidOperationException(moveResult.Reason);
}
var otherPlayer = tempBoard.WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
if (BoardState.InCheck == BoardState.WhoseTurn)
// If already in check, assert the move that resulted in check no longer results in check.
if (BoardState.InCheck == BoardState.WhoseTurn
&& simulation.IsOpposingKingThreatenedByPosition(BoardState.PreviousMove.To))
{
//if (simulation.IsPlayerInCheckAfterMove(boardState.PreviousMoveTo, toVector, boardState.WhoseTurn))
//{
// throw new InvalidOperationException("Illegal move. You're still in check!");
//}
throw new InvalidOperationException("Unable to drop piece becauase you are still in check.");
}
var kingPosition = otherPlayer == WhichPlayer.Player1 ? tempBoard.Player1KingPosition : tempBoard.Player2KingPosition;
//if (simulation.IsPlayerInCheckAfterMove(toVector, kingPosition, otherPlayer))
//{
if (simulation.DidPlayerPutThemselfInCheck())
{
throw new InvalidOperationException("Illegal move. This move places you in check.");
}
//}
// Update the non-simulation board.
var otherPlayer = tempBoard.WhoseTurn == WhichPlayer.Player1
? WhichPlayer.Player2
: WhichPlayer.Player1;
_ = rules.Move(pieceInHand, to);
if (rules.IsOpponentInCheckAfterMove())
{
BoardState.InCheck = otherPlayer;
// A pawn, placed from the hand, cannot be the cause of checkmate.
if (rules.IsOpponentInCheckMate() && pieceInHand != WhichPiece.Pawn)
{
BoardState.IsCheckmate = true;
}
}
//rules.Move(from, to, isPromotion);
//if (rules.IsPlayerInCheckAfterMove(fromVector, toVector, otherPlayer))
//{
// board.InCheck = otherPlayer;
// board.IsCheckmate = rules.EvaluateCheckmate();
//}
//else
//{
// board.InCheck = null;
//}
var kingPosition = otherPlayer == WhichPlayer.Player1
? tempBoard.Player1KingPosition
: tempBoard.Player2KingPosition;
BoardState.WhoseTurn = otherPlayer;
}
@@ -149,7 +156,7 @@ public sealed class ShogiBoard
{
var builder = new StringBuilder();
builder.Append(" ");
builder.Append("Player 2(.)");
builder.Append("Player 2");
builder.AppendLine();
for (var rank = 8; rank >= 0; rank--)
{