This commit is contained in:
2024-10-11 11:10:38 -05:00
parent 81dd267290
commit f75553a0ad
14 changed files with 616 additions and 514 deletions

View File

@@ -12,105 +12,6 @@ namespace Shogi.Domain.ValueObjects
boardState = board;
}
/// <summary>
/// Move a piece from a board tile to another board tile ignorant of check or check-mate.
/// </summary>
/// <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="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>
internal MoveResult Move(string fromNotation, string toNotation, bool isPromotionRequested = false)
{
var from = Notation.FromBoardNotation(fromNotation);
var to = Notation.FromBoardNotation(toNotation);
var fromPiece = boardState[from];
if (fromPiece == null)
{
return new MoveResult(false, $"Tile [{fromNotation}] is empty. There is no piece to move.");
}
if (fromPiece.Owner != boardState.WhoseTurn)
{
return new MoveResult(false, "Not allowed to move the opponents piece");
}
var path = fromPiece.GetPathFromStartToEnd(from, to);
if (boardState.IsPathBlocked(path))
{
return new MoveResult(false, "Another piece obstructs the desired move.");
}
if (boardState[to] != null)
{
boardState.Capture(to);
}
if (isPromotionRequested && (boardState.IsWithinPromotionZone(to) || boardState.IsWithinPromotionZone(from)))
{
fromPiece.Promote();
}
boardState[to] = fromPiece;
boardState[from] = null;
boardState.PreviousMove = new Move(from, to);
var otherPlayer = boardState.WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
boardState.WhoseTurn = otherPlayer;
return new MoveResult(true);
}
/// Move a piece from the hand to the board ignorant if check or check-mate.
/// </summary>
/// <param name="pieceInHand"></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>
internal MoveResult Move(WhichPiece pieceInHand, string toNotation)
{
var to = Notation.FromBoardNotation(toNotation);
var index = boardState.ActivePlayerHand.FindIndex(p => p.WhichPiece == pieceInHand);
if (index == -1)
{
return new MoveResult(false, $"{pieceInHand} does not exist in the hand.");
}
if (boardState[to] != null)
{
return new MoveResult(false, "Illegal move - attempting to capture while playing a piece from the hand.");
}
switch (pieceInHand)
{
case WhichPiece.Knight:
{
// Knight cannot be placed onto the farthest two ranks from the hand.
if (boardState.WhoseTurn == WhichPlayer.Player1 && to.Y > 6
|| boardState.WhoseTurn == WhichPlayer.Player2 && to.Y < 2)
{
return new MoveResult(false, "Knight has no valid moves after placed.");
}
break;
}
case WhichPiece.Lance:
case WhichPiece.Pawn:
{
// Lance and Pawn cannot be placed onto the farthest rank from the hand.
if (boardState.WhoseTurn == WhichPlayer.Player1 && to.Y == 8
|| boardState.WhoseTurn == WhichPlayer.Player2 && to.Y == 0)
{
return new MoveResult(false, $"{pieceInHand} has no valid moves after placed.");
}
break;
}
}
// Mutate the board.
boardState[to] = boardState.ActivePlayerHand[index];
boardState.ActivePlayerHand.RemoveAt(index);
boardState.PreviousMove = new Move(pieceInHand, to);
return new MoveResult(true);
}
/// <summary>
/// Determines if the last move put the player who moved in check.
/// </summary>
@@ -238,14 +139,16 @@ namespace Shogi.Domain.ValueObjects
return true;
}
private IList<Vector2> GetPossiblePositionsForKing(WhichPlayer whichPlayer)
private List<Vector2> GetPossiblePositionsForKing(WhichPlayer whichPlayer)
{
var kingPosition = whichPlayer == WhichPlayer.Player1
? boardState.Player1KingPosition
: boardState.Player2KingPosition;
return King.KingPaths
.Select(path => path.Direction + kingPosition)
var paths = boardState[kingPosition]!.MoveSet;
return paths
.Select(path => path.NormalizedDirection + kingPosition)
// Because the king could be on the edge of the board, where some of its paths do not make sense.
.Where(newPosition => newPosition.IsInsideBoardBoundary())
// Where tile at position is empty, meaning the king could move there.
.Where(newPosition => boardState[newPosition] == null)