Finish logic around placing pieces from the hand.
This commit is contained in:
@@ -17,7 +17,6 @@ public class BoardState
|
|||||||
playerInCheck: null,
|
playerInCheck: null,
|
||||||
previousMove: new Move());
|
previousMove: new Move());
|
||||||
|
|
||||||
public delegate void ForEachDelegate(Piece element, Vector2 position);
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Key is position notation, such as "E4".
|
/// Key is position notation, such as "E4".
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -3,256 +3,261 @@ using BoardTile = System.Collections.Generic.KeyValuePair<System.Numerics.Vector
|
|||||||
|
|
||||||
namespace Shogi.Domain
|
namespace Shogi.Domain
|
||||||
{
|
{
|
||||||
internal class StandardRules
|
internal class StandardRules
|
||||||
{
|
{
|
||||||
private readonly BoardState boardState;
|
private readonly BoardState boardState;
|
||||||
|
|
||||||
internal StandardRules(BoardState board)
|
internal StandardRules(BoardState board)
|
||||||
{
|
{
|
||||||
boardState = board;
|
boardState = board;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Move a piece from a board tile to another board tile ignorant of check or check-mate.
|
/// Move a piece from a board tile to another board tile ignorant of check or check-mate.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fromNotation">The position of the piece being moved expressed in board notation.</param>
|
/// <param name="fromNotation">The position of the piece being moved expressed in board notation.</param>
|
||||||
/// <param name="toNotation">The target position expressed in board notation.</param>
|
/// <param name="toNotation">The target position expressed in board notation.</param>
|
||||||
/// <param name="isPromotionRequested">True if a promotion is expected as a result of this move.</param>
|
/// <param name="isPromotionRequested">True if a promotion is expected as a result of this move.</param>
|
||||||
/// <returns>A <see cref="MoveResult" /> describing the success or failure of the move.</returns>
|
/// <returns>A <see cref="MoveResult" /> describing the success or failure of the move.</returns>
|
||||||
internal MoveResult Move(string fromNotation, string toNotation, bool isPromotionRequested = false)
|
internal MoveResult Move(string fromNotation, string toNotation, bool isPromotionRequested = false)
|
||||||
{
|
{
|
||||||
var from = Notation.FromBoardNotation(fromNotation);
|
var from = Notation.FromBoardNotation(fromNotation);
|
||||||
var to = Notation.FromBoardNotation(toNotation);
|
var to = Notation.FromBoardNotation(toNotation);
|
||||||
var fromPiece = boardState[from];
|
var fromPiece = boardState[from];
|
||||||
if (fromPiece == null)
|
if (fromPiece == null)
|
||||||
{
|
{
|
||||||
return new MoveResult(false, $"Tile [{fromNotation}] is empty. There is no piece to move.");
|
return new MoveResult(false, $"Tile [{fromNotation}] is empty. There is no piece to move.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fromPiece.Owner != boardState.WhoseTurn)
|
if (fromPiece.Owner != boardState.WhoseTurn)
|
||||||
{
|
{
|
||||||
return new MoveResult(false, "Not allowed to move the opponents piece");
|
return new MoveResult(false, "Not allowed to move the opponents piece");
|
||||||
}
|
}
|
||||||
|
|
||||||
var path = fromPiece.GetPathFromStartToEnd(from, to);
|
var path = fromPiece.GetPathFromStartToEnd(from, to);
|
||||||
|
|
||||||
if (boardState.IsPathBlocked(path))
|
if (boardState.IsPathBlocked(path))
|
||||||
{
|
{
|
||||||
return new MoveResult(false, "Another piece obstructs the desired move.");
|
return new MoveResult(false, "Another piece obstructs the desired move.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (boardState[to] != null)
|
if (boardState[to] != null)
|
||||||
{
|
{
|
||||||
boardState.Capture(to);
|
boardState.Capture(to);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isPromotionRequested && (boardState.IsWithinPromotionZone(to) || boardState.IsWithinPromotionZone(from)))
|
if (isPromotionRequested && (boardState.IsWithinPromotionZone(to) || boardState.IsWithinPromotionZone(from)))
|
||||||
{
|
{
|
||||||
fromPiece.Promote();
|
fromPiece.Promote();
|
||||||
}
|
}
|
||||||
|
|
||||||
boardState[to] = fromPiece;
|
boardState[to] = fromPiece;
|
||||||
boardState[from] = null;
|
boardState[from] = null;
|
||||||
|
|
||||||
boardState.PreviousMove = new Move(from, to);
|
boardState.PreviousMove = new Move(from, to);
|
||||||
var otherPlayer = boardState.WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
var otherPlayer = boardState.WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
||||||
boardState.WhoseTurn = otherPlayer;
|
boardState.WhoseTurn = otherPlayer;
|
||||||
|
|
||||||
return new MoveResult(true);
|
return new MoveResult(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Move a piece from the hand to the board ignorant if check or check-mate.
|
/// Move a piece from the hand to the board ignorant if check or check-mate.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="pieceInHand"></param>
|
/// <param name="pieceInHand"></param>
|
||||||
/// <param name="to">The target position expressed in board notation.</param>
|
/// <param name="to">The target position expressed in board notation.</param>
|
||||||
/// <returns>A <see cref="MoveResult" /> describing the success or failure of the simulation.</returns>
|
/// <returns>A <see cref="MoveResult" /> describing the success or failure of the simulation.</returns>
|
||||||
internal MoveResult Move(WhichPiece pieceInHand, string toNotation)
|
internal MoveResult Move(WhichPiece pieceInHand, string toNotation)
|
||||||
{
|
{
|
||||||
var to = Notation.FromBoardNotation(toNotation);
|
var to = Notation.FromBoardNotation(toNotation);
|
||||||
var index = boardState.ActivePlayerHand.FindIndex(p => p.WhichPiece == pieceInHand);
|
var index = boardState.ActivePlayerHand.FindIndex(p => p.WhichPiece == pieceInHand);
|
||||||
if (index == -1)
|
if (index == -1)
|
||||||
{
|
{
|
||||||
return new MoveResult(false, $"{pieceInHand} does not exist in the hand.");
|
return new MoveResult(false, $"{pieceInHand} does not exist in the hand.");
|
||||||
}
|
}
|
||||||
if (boardState[to] != null)
|
if (boardState[to] != null)
|
||||||
{
|
{
|
||||||
return new MoveResult(false, $"Illegal move - attempting to capture while playing a piece from the hand.");
|
return new MoveResult(false, "Illegal move - attempting to capture while playing a piece from the hand.");
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (pieceInHand)
|
switch (pieceInHand)
|
||||||
{
|
{
|
||||||
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.
|
||||||
if ((boardState.WhoseTurn == WhichPlayer.Player1 && to.Y > 6)
|
if ((boardState.WhoseTurn == WhichPlayer.Player1 && to.Y > 6)
|
||||||
|| (boardState.WhoseTurn == WhichPlayer.Player2 && to.Y < 2))
|
|| (boardState.WhoseTurn == WhichPlayer.Player2 && to.Y < 2))
|
||||||
{
|
{
|
||||||
return new MoveResult(false, "Knight has no valid moves after placed.");
|
return new MoveResult(false, "Knight has no valid moves after placed.");
|
||||||
}
|
}
|
||||||
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.
|
||||||
if ((boardState.WhoseTurn == WhichPlayer.Player1 && to.Y == 8)
|
if ((boardState.WhoseTurn == WhichPlayer.Player1 && to.Y == 8)
|
||||||
|| (boardState.WhoseTurn == WhichPlayer.Player2 && to.Y == 0))
|
|| (boardState.WhoseTurn == WhichPlayer.Player2 && to.Y == 0))
|
||||||
{
|
{
|
||||||
return new MoveResult(false, $"{pieceInHand} has no valid moves after placed.");
|
return new MoveResult(false, $"{pieceInHand} has no valid moves after placed.");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mutate the board.
|
// Mutate the board.
|
||||||
boardState[to] = boardState.ActivePlayerHand[index];
|
boardState[to] = boardState.ActivePlayerHand[index];
|
||||||
boardState.ActivePlayerHand.RemoveAt(index);
|
boardState.ActivePlayerHand.RemoveAt(index);
|
||||||
//MoveHistory.Add(move);
|
boardState.PreviousMove = new Move(pieceInHand, to);
|
||||||
return new MoveResult(true);
|
return new MoveResult(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines if the last move put the player who moved in check.
|
/// Determines if the last move put the player who moved in check.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// This strategy recognizes that a "discover check" could only occur from a subset of pieces: Rook, Bishop, Lance.
|
/// This strategy recognizes that a "discover check" could only occur from a subset of pieces: Rook, Bishop, Lance.
|
||||||
/// In this way, only those pieces need to be considered when evaluating if a move placed the moving player in check.
|
/// In this way, only those pieces need to be considered when evaluating if a move placed the moving player in check.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
internal bool IsPlayerInCheckAfterMove()
|
internal bool DidPlayerPutThemselfInCheck()
|
||||||
{
|
{
|
||||||
var previousMovedPiece = boardState[boardState.PreviousMove.To];
|
if (boardState.PreviousMove.From == null)
|
||||||
if (previousMovedPiece == null) throw new ArgumentNullException(nameof(previousMovedPiece), $"No piece exists at position {boardState.PreviousMove.To}.");
|
{
|
||||||
var kingPosition = previousMovedPiece.Owner == WhichPlayer.Player1 ? boardState.Player1KingPosition : boardState.Player2KingPosition;
|
// You can't place yourself in check by placing a piece from your hand.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
var isCheck = false;
|
var previousMovedPiece = boardState[boardState.PreviousMove.To];
|
||||||
|
if (previousMovedPiece == null) throw new ArgumentNullException(nameof(previousMovedPiece), $"No piece exists at position {boardState.PreviousMove.To}.");
|
||||||
|
var kingPosition = previousMovedPiece.Owner == WhichPlayer.Player1 ? boardState.Player1KingPosition : boardState.Player2KingPosition;
|
||||||
|
|
||||||
// Get line equation from king through the now-unoccupied location.
|
|
||||||
var direction = Vector2.Subtract(kingPosition, boardState.PreviousMove.From);
|
|
||||||
var slope = Math.Abs(direction.Y / direction.X);
|
|
||||||
var path = BoardState.GetPathAlongDirectionFromStartToEdgeOfBoard(boardState.PreviousMove.From, Vector2.Normalize(direction));
|
|
||||||
var threat = boardState.QueryFirstPieceInPath(path);
|
|
||||||
if (threat == null || threat.Owner == previousMovedPiece.Owner) return false;
|
|
||||||
// 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))
|
|
||||||
{
|
|
||||||
isCheck = threat.WhichPiece switch
|
|
||||||
{
|
|
||||||
WhichPiece.Lance => !threat.IsPromoted,
|
|
||||||
WhichPiece.Rook => true,
|
|
||||||
_ => false
|
|
||||||
};
|
|
||||||
}
|
|
||||||
else if (slope == 1)
|
|
||||||
{
|
|
||||||
isCheck = threat.WhichPiece switch
|
|
||||||
{
|
|
||||||
WhichPiece.Bishop => true,
|
|
||||||
_ => false
|
|
||||||
};
|
|
||||||
}
|
|
||||||
else if (slope == 0)
|
|
||||||
{
|
|
||||||
isCheck = threat.WhichPiece switch
|
|
||||||
{
|
|
||||||
WhichPiece.Rook => true,
|
|
||||||
_ => false
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return isCheck;
|
var isDiscoverCheck = false;
|
||||||
}
|
// Get line equation from king through the now-unoccupied location.
|
||||||
|
var direction = Vector2.Subtract(kingPosition, boardState.PreviousMove.From.Value);
|
||||||
|
var slope = Math.Abs(direction.Y / direction.X);
|
||||||
|
var path = BoardState.GetPathAlongDirectionFromStartToEdgeOfBoard(boardState.PreviousMove.From.Value, Vector2.Normalize(direction));
|
||||||
|
var threat = boardState.QueryFirstPieceInPath(path);
|
||||||
|
if (threat == null || threat.Owner == previousMovedPiece.Owner) return false;
|
||||||
|
// 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))
|
||||||
|
{
|
||||||
|
isDiscoverCheck = threat.WhichPiece switch
|
||||||
|
{
|
||||||
|
WhichPiece.Lance => !threat.IsPromoted,
|
||||||
|
WhichPiece.Rook => true,
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else if (slope == 1)
|
||||||
|
{
|
||||||
|
isDiscoverCheck = threat.WhichPiece switch
|
||||||
|
{
|
||||||
|
WhichPiece.Bishop => true,
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else if (slope == 0)
|
||||||
|
{
|
||||||
|
isDiscoverCheck = threat.WhichPiece switch
|
||||||
|
{
|
||||||
|
WhichPiece.Rook => true,
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return isDiscoverCheck;
|
||||||
|
}
|
||||||
|
|
||||||
internal bool IsOpponentInCheckAfterMove() => IsOpposingKingThreatenedByPosition(boardState.PreviousMove.To);
|
internal bool IsOpponentInCheckAfterMove() => IsOpposingKingThreatenedByPosition(boardState.PreviousMove.To);
|
||||||
|
|
||||||
internal bool IsOpposingKingThreatenedByPosition(Vector2 position)
|
internal bool IsOpposingKingThreatenedByPosition(Vector2 position)
|
||||||
{
|
{
|
||||||
var previousMovedPiece = boardState[position];
|
var previousMovedPiece = boardState[position];
|
||||||
if (previousMovedPiece == null) return false;
|
if (previousMovedPiece == null) return false;
|
||||||
|
|
||||||
var kingPosition = previousMovedPiece.Owner == WhichPlayer.Player1 ? boardState.Player2KingPosition : boardState.Player1KingPosition;
|
var kingPosition = previousMovedPiece.Owner == WhichPlayer.Player1 ? boardState.Player2KingPosition : boardState.Player1KingPosition;
|
||||||
var path = previousMovedPiece.GetPathFromStartToEnd(position, kingPosition);
|
var path = previousMovedPiece.GetPathFromStartToEnd(position, kingPosition);
|
||||||
var threatenedPiece = boardState.QueryFirstPieceInPath(path);
|
var threatenedPiece = boardState.QueryFirstPieceInPath(path);
|
||||||
if (!path.Any() || threatenedPiece == null) return false;
|
if (!path.Any() || threatenedPiece == null) return false;
|
||||||
|
|
||||||
return threatenedPiece.WhichPiece == WhichPiece.King;
|
return threatenedPiece.WhichPiece == WhichPiece.King;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool IsOpponentInCheckMate()
|
internal bool IsOpponentInCheckMate()
|
||||||
{
|
{
|
||||||
// Assume checkmate, then try to disprove.
|
// Assume checkmate, then try to disprove.
|
||||||
if (!boardState.InCheck.HasValue) return false;
|
if (!boardState.InCheck.HasValue) return false;
|
||||||
// Get all pieces from opponent who threaten the king in question.
|
// Get all pieces from opponent who threaten the king in question.
|
||||||
var opponent = boardState.WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
var opponent = boardState.WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
||||||
var tilesOccupiedByOpponent = boardState.GetTilesOccupiedBy(opponent);
|
var tilesOccupiedByOpponent = boardState.GetTilesOccupiedBy(opponent);
|
||||||
var kingPosition = boardState.WhoseTurn == WhichPlayer.Player1
|
var kingPosition = boardState.WhoseTurn == WhichPlayer.Player1
|
||||||
? boardState.Player1KingPosition
|
? boardState.Player1KingPosition
|
||||||
: boardState.Player2KingPosition;
|
: boardState.Player2KingPosition;
|
||||||
var threats = tilesOccupiedByOpponent.Where(tile => PieceHasLineOfSight(tile, kingPosition)).ToList();
|
var threats = tilesOccupiedByOpponent.Where(tile => PieceHasLineOfSight(tile, kingPosition)).ToList();
|
||||||
if (threats.Count == 1)
|
if (threats.Count == 1)
|
||||||
{
|
{
|
||||||
/* If there is exactly one threat it is possible to block the check.
|
/* If there is exactly one threat it is possible to block the check.
|
||||||
* Foreach piece owned by whichPlayer
|
* Foreach piece owned by whichPlayer
|
||||||
* if piece can intercept check, return false;
|
* if piece can intercept check, return false;
|
||||||
*/
|
*/
|
||||||
var threat = threats.Single();
|
var threat = threats.Single();
|
||||||
var pathFromThreatToKing = threat.Value.GetPathFromStartToEnd(threat.Key, kingPosition);
|
var pathFromThreatToKing = threat.Value.GetPathFromStartToEnd(threat.Key, kingPosition);
|
||||||
var tilesThatCouldBlockTheThreat = boardState.GetTilesOccupiedBy(boardState.WhoseTurn);
|
var tilesThatCouldBlockTheThreat = boardState.GetTilesOccupiedBy(boardState.WhoseTurn);
|
||||||
foreach (var threatBlockingPosition in pathFromThreatToKing)
|
foreach (var threatBlockingPosition in pathFromThreatToKing)
|
||||||
{
|
{
|
||||||
var tilesThatDoBlockThreat = tilesThatCouldBlockTheThreat
|
var tilesThatDoBlockThreat = tilesThatCouldBlockTheThreat
|
||||||
.Where(tile => PieceHasLineOfSight(tile, threatBlockingPosition))
|
.Where(tile => PieceHasLineOfSight(tile, threatBlockingPosition))
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
if (tilesThatDoBlockThreat.Any())
|
if (tilesThatDoBlockThreat.Any())
|
||||||
{
|
{
|
||||||
return false; // Cannot be check-mate if a piece can intercept the threat.
|
return false; // Cannot be check-mate if a piece can intercept the threat.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* If no ability to block the check, maybe the king can evade check by moving.
|
* If no ability to block the check, maybe the king can evade check by moving.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
foreach (var maybeSafePosition in GetPossiblePositionsForKing(this.boardState.WhoseTurn))
|
foreach (var maybeSafePosition in GetPossiblePositionsForKing(this.boardState.WhoseTurn))
|
||||||
{
|
{
|
||||||
threats = tilesOccupiedByOpponent
|
threats = tilesOccupiedByOpponent
|
||||||
.Where(tile => PieceHasLineOfSight(tile, maybeSafePosition))
|
.Where(tile => PieceHasLineOfSight(tile, maybeSafePosition))
|
||||||
.ToList();
|
.ToList();
|
||||||
if (!threats.Any())
|
if (!threats.Any())
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IList<Vector2> GetPossiblePositionsForKing(WhichPlayer whichPlayer)
|
private IList<Vector2> GetPossiblePositionsForKing(WhichPlayer whichPlayer)
|
||||||
{
|
{
|
||||||
var kingPosition = whichPlayer == WhichPlayer.Player1
|
var kingPosition = whichPlayer == WhichPlayer.Player1
|
||||||
? boardState.Player1KingPosition
|
? boardState.Player1KingPosition
|
||||||
: boardState.Player2KingPosition;
|
: boardState.Player2KingPosition;
|
||||||
|
|
||||||
return King.KingPaths
|
return King.KingPaths
|
||||||
.Select(path => path.Direction + kingPosition)
|
.Select(path => path.Direction + kingPosition)
|
||||||
.Where(newPosition => newPosition.IsInsideBoardBoundary())
|
.Where(newPosition => newPosition.IsInsideBoardBoundary())
|
||||||
// Where tile at position is empty, meaning the king could move there.
|
// Where tile at position is empty, meaning the king could move there.
|
||||||
.Where(newPosition => boardState[newPosition] == null)
|
.Where(newPosition => boardState[newPosition] == null)
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool PieceHasLineOfSight(BoardTile tile, Vector2 lineOfSightTarget)
|
private bool PieceHasLineOfSight(BoardTile tile, Vector2 lineOfSightTarget)
|
||||||
{
|
{
|
||||||
var path = tile.Value.GetPathFromStartToEnd(tile.Key, lineOfSightTarget);
|
var path = tile.Value.GetPathFromStartToEnd(tile.Key, lineOfSightTarget);
|
||||||
return path
|
return path
|
||||||
.SkipLast(1)
|
.SkipLast(1)
|
||||||
.All(position => boardState[Notation.ToBoardNotation(position)] == null);
|
.All(position => boardState[Notation.ToBoardNotation(position)] == null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,20 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a single piece being moved by a player from <paramref name="From"/> to <paramref name="To"/>.
|
/// Represents a single piece being moved by a player from <paramref name="From"/> to <paramref name="To"/>.
|
||||||
/// </summary>
|
/// </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; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,12 +44,14 @@ public sealed class ShogiBoard
|
|||||||
throw new InvalidOperationException("Unable to move because you are still in check.");
|
throw new InvalidOperationException("Unable to move because you are still in check.");
|
||||||
}
|
}
|
||||||
|
|
||||||
var otherPlayer = BoardState.WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
if (simulation.DidPlayerPutThemselfInCheck())
|
||||||
if (simulation.IsPlayerInCheckAfterMove())
|
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException("Illegal move. This move places you in check.");
|
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);
|
_ = rules.Move(from, to, isPromotion);
|
||||||
if (rules.IsOpponentInCheckAfterMove())
|
if (rules.IsOpponentInCheckAfterMove())
|
||||||
{
|
{
|
||||||
@@ -113,31 +115,36 @@ public sealed class ShogiBoard
|
|||||||
throw new InvalidOperationException(moveResult.Reason);
|
throw new InvalidOperationException(moveResult.Reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
var otherPlayer = tempBoard.WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
// If already in check, assert the move that resulted in check no longer results in check.
|
||||||
if (BoardState.InCheck == BoardState.WhoseTurn)
|
if (BoardState.InCheck == BoardState.WhoseTurn
|
||||||
|
&& simulation.IsOpposingKingThreatenedByPosition(BoardState.PreviousMove.To))
|
||||||
{
|
{
|
||||||
//if (simulation.IsPlayerInCheckAfterMove(boardState.PreviousMoveTo, toVector, boardState.WhoseTurn))
|
throw new InvalidOperationException("Unable to drop piece becauase you are still in check.");
|
||||||
//{
|
|
||||||
// throw new InvalidOperationException("Illegal move. You're still in check!");
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var kingPosition = otherPlayer == WhichPlayer.Player1 ? tempBoard.Player1KingPosition : tempBoard.Player2KingPosition;
|
if (simulation.DidPlayerPutThemselfInCheck())
|
||||||
//if (simulation.IsPlayerInCheckAfterMove(toVector, kingPosition, otherPlayer))
|
{
|
||||||
//{
|
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);
|
var kingPosition = otherPlayer == WhichPlayer.Player1
|
||||||
//if (rules.IsPlayerInCheckAfterMove(fromVector, toVector, otherPlayer))
|
? tempBoard.Player1KingPosition
|
||||||
//{
|
: tempBoard.Player2KingPosition;
|
||||||
// board.InCheck = otherPlayer;
|
|
||||||
// board.IsCheckmate = rules.EvaluateCheckmate();
|
|
||||||
//}
|
|
||||||
//else
|
|
||||||
//{
|
|
||||||
// board.InCheck = null;
|
|
||||||
//}
|
|
||||||
BoardState.WhoseTurn = otherPlayer;
|
BoardState.WhoseTurn = otherPlayer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,7 +156,7 @@ public sealed class ShogiBoard
|
|||||||
{
|
{
|
||||||
var builder = new StringBuilder();
|
var builder = new StringBuilder();
|
||||||
builder.Append(" ");
|
builder.Append(" ");
|
||||||
builder.Append("Player 2(.)");
|
builder.Append("Player 2");
|
||||||
builder.AppendLine();
|
builder.AppendLine();
|
||||||
for (var rank = 8; rank >= 0; rank--)
|
for (var rank = 8; rank >= 0; rank--)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Shogi.Domain.ValueObjects;
|
using Shogi.Domain.ValueObjects;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Shogi.Domain.UnitTests
|
namespace Shogi.Domain.UnitTests
|
||||||
{
|
{
|
||||||
@@ -299,15 +300,16 @@ namespace Shogi.Domain.UnitTests
|
|||||||
shogi.Move("A7", "A6", false);
|
shogi.Move("A7", "A6", false);
|
||||||
// P1 drop Bishop, place P2 in check
|
// P1 drop Bishop, place P2 in check
|
||||||
shogi.Move(WhichPiece.Bishop, "G7");
|
shogi.Move(WhichPiece.Bishop, "G7");
|
||||||
|
|
||||||
shogi.BoardState.InCheck.Should().Be(WhichPlayer.Player2);
|
shogi.BoardState.InCheck.Should().Be(WhichPlayer.Player2);
|
||||||
shogi.BoardState.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
|
shogi.BoardState.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
|
||||||
shogi.BoardState["E5"].Should().BeNull();
|
shogi.BoardState["E5"].Should().BeNull();
|
||||||
|
|
||||||
// Act - P2 places a Bishop while in check.
|
// Act - P2 places a Bishop while in check.
|
||||||
shogi.Move(WhichPiece.Bishop, "E5");
|
var act = () => shogi.Move(WhichPiece.Bishop, "E5");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
|
using var scope = new AssertionScope();
|
||||||
|
act.Should().Throw<InvalidOperationException>();
|
||||||
shogi.BoardState["E5"].Should().BeNull();
|
shogi.BoardState["E5"].Should().BeNull();
|
||||||
shogi.BoardState.InCheck.Should().Be(WhichPlayer.Player2);
|
shogi.BoardState.InCheck.Should().Be(WhichPlayer.Player2);
|
||||||
shogi.BoardState.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
|
shogi.BoardState.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
|
||||||
@@ -326,25 +328,21 @@ namespace Shogi.Domain.UnitTests
|
|||||||
shogi.Move("B2", "H8", false);
|
shogi.Move("B2", "H8", false);
|
||||||
// P2 Pawn
|
// P2 Pawn
|
||||||
shogi.Move("G6", "G5", false);
|
shogi.Move("G6", "G5", false);
|
||||||
using (new AssertionScope())
|
shogi.BoardState.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
|
||||||
{
|
shogi.BoardState["I9"].Should().NotBeNull();
|
||||||
shogi.BoardState.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
|
shogi.BoardState["I9"]!.WhichPiece.Should().Be(WhichPiece.Lance);
|
||||||
shogi.BoardState["I9"].Should().NotBeNull();
|
shogi.BoardState["I9"]!.Owner.Should().Be(WhichPlayer.Player2);
|
||||||
shogi.BoardState["I9"]!.WhichPiece.Should().Be(WhichPiece.Lance);
|
|
||||||
shogi.BoardState["I9"]!.Owner.Should().Be(WhichPlayer.Player2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Act - P1 tries to place a piece where an opponent's piece resides.
|
// Act - P1 tries to place a piece where an opponent's piece resides.
|
||||||
shogi.Move(WhichPiece.Bishop, "I9");
|
var act = () => shogi.Move(WhichPiece.Bishop, "I9");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
using (new AssertionScope())
|
using var scope = new AssertionScope();
|
||||||
{
|
act.Should().Throw<InvalidOperationException>();
|
||||||
shogi.BoardState.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
|
shogi.BoardState.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
|
||||||
shogi.BoardState["I9"].Should().NotBeNull();
|
shogi.BoardState["I9"].Should().NotBeNull();
|
||||||
shogi.BoardState["I9"]!.WhichPiece.Should().Be(WhichPiece.Lance);
|
shogi.BoardState["I9"]!.WhichPiece.Should().Be(WhichPiece.Lance);
|
||||||
shogi.BoardState["I9"]!.Owner.Should().Be(WhichPlayer.Player2);
|
shogi.BoardState["I9"]!.Owner.Should().Be(WhichPlayer.Player2);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -446,6 +444,7 @@ namespace Shogi.Domain.UnitTests
|
|||||||
|
|
||||||
// Assert - checkmate
|
// Assert - checkmate
|
||||||
console.WriteLine(shogi.ToStringStateAsAscii());
|
console.WriteLine(shogi.ToStringStateAsAscii());
|
||||||
|
console.WriteLine(string.Join(",", shogi.BoardState.Player1Hand.Select(p => p.WhichPiece.ToString())));
|
||||||
board.IsCheckmate.Should().BeTrue();
|
board.IsCheckmate.Should().BeTrue();
|
||||||
board.InCheck.Should().Be(WhichPlayer.Player2);
|
board.InCheck.Should().Be(WhichPlayer.Player2);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user