yep
This commit is contained in:
@@ -95,6 +95,92 @@ public class BoardState
|
||||
set => this[Notation.ToBoardNotation(x, y)] = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move a piece from a board tile to another board tile ignorant of check or check-mate.
|
||||
/// If a piece is captured during the move, state will change to reflect that.
|
||||
/// If a piece should promote during the move, state will change to reflect that.
|
||||
/// </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);
|
||||
if (this[toNotation] != null)
|
||||
{
|
||||
Capture(to);
|
||||
}
|
||||
|
||||
var fromPiece = this[fromNotation]
|
||||
?? throw new InvalidOperationException($"No piece exists at position {fromNotation}.");
|
||||
if (isPromotionRequested &&
|
||||
(IsWithinPromotionZone(to) || IsWithinPromotionZone(from)))
|
||||
{
|
||||
fromPiece.Promote();
|
||||
}
|
||||
|
||||
this[toNotation] = fromPiece;
|
||||
this[fromNotation] = null;
|
||||
|
||||
PreviousMove = new Move(from, to);
|
||||
var otherPlayer = WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
||||
WhoseTurn = otherPlayer;
|
||||
|
||||
return new MoveResult(true);
|
||||
}
|
||||
|
||||
/// Move a piece from the hand to the board ignorant of 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 index = ActivePlayerHand.FindIndex(p => p.WhichPiece == pieceInHand);
|
||||
if (index == -1)
|
||||
{
|
||||
return new MoveResult(false, $"{pieceInHand} does not exist in the hand.");
|
||||
}
|
||||
if (this[toNotation] != null)
|
||||
{
|
||||
return new MoveResult(false, "Illegal move - attempting to capture while playing a piece from the hand.");
|
||||
}
|
||||
|
||||
var to = Notation.FromBoardNotation(toNotation);
|
||||
switch (pieceInHand)
|
||||
{
|
||||
case WhichPiece.Knight:
|
||||
{
|
||||
// Knight cannot be placed onto the farthest two ranks from the hand.
|
||||
if (WhoseTurn == WhichPlayer.Player1 && to.Y > 6
|
||||
|| 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 (WhoseTurn == WhichPlayer.Player1 && to.Y == 8
|
||||
|| WhoseTurn == WhichPlayer.Player2 && to.Y == 0)
|
||||
{
|
||||
return new MoveResult(false, $"{pieceInHand} has no valid moves after placed.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Mutate the board.
|
||||
this[toNotation] = ActivePlayerHand[index];
|
||||
ActivePlayerHand.RemoveAt(index);
|
||||
PreviousMove = new Move(pieceInHand, to);
|
||||
return new MoveResult(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the given path can be traversed without colliding into a piece.
|
||||
/// </summary>
|
||||
@@ -107,8 +193,6 @@ public class BoardState
|
||||
|
||||
internal bool IsWithinPromotionZone(Vector2 position)
|
||||
{
|
||||
|
||||
|
||||
// TODO: Move this promotion zone logic into the StandardRules class.
|
||||
return WhoseTurn == WhichPlayer.Player1 && position.Y > 5
|
||||
|| WhoseTurn == WhichPlayer.Player2 && position.Y < 3;
|
||||
@@ -127,9 +211,8 @@ public class BoardState
|
||||
|
||||
internal void Capture(Vector2 to)
|
||||
{
|
||||
var piece = this[to];
|
||||
if (piece == null) throw new InvalidOperationException("Cannot capture. Piece at position does not exist.");
|
||||
|
||||
var piece = this[to]
|
||||
?? throw new InvalidOperationException("Cannot capture. Piece at position does not exist.");
|
||||
piece.Capture(WhoseTurn);
|
||||
ActivePlayerHand.Add(piece);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user