yep
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Text;
|
||||
using Shogi.Domain.Other;
|
||||
using Shogi.Domain.YetToBeAssimilatedIntoDDD;
|
||||
|
||||
namespace Shogi.Domain.ValueObjects;
|
||||
@@ -10,234 +11,257 @@ namespace Shogi.Domain.ValueObjects;
|
||||
/// </summary>
|
||||
public sealed class ShogiBoard
|
||||
{
|
||||
private readonly StandardRules rules;
|
||||
private readonly StandardRules rules;
|
||||
|
||||
public ShogiBoard(BoardState initialState)
|
||||
{
|
||||
BoardState = initialState;
|
||||
rules = new StandardRules(BoardState);
|
||||
}
|
||||
public ShogiBoard(BoardState initialState)
|
||||
{
|
||||
BoardState = initialState;
|
||||
rules = new StandardRules(BoardState);
|
||||
}
|
||||
|
||||
public BoardState BoardState { get; }
|
||||
public BoardState BoardState { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Move a piece from a board position to another board position, potentially capturing an opponents piece. Respects all rules of the game.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The strategy involves simulating a move on a throw-away board state that can be used to
|
||||
/// validate legal vs illegal moves without having to worry about reverting board state.
|
||||
/// </remarks>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
public void Move(string from, string to, bool isPromotion)
|
||||
{
|
||||
var simulationState = new BoardState(BoardState);
|
||||
var simulation = new StandardRules(simulationState);
|
||||
var moveResult = simulation.Move(from, to, isPromotion);
|
||||
if (!moveResult.Success)
|
||||
{
|
||||
throw new InvalidOperationException(moveResult.Reason);
|
||||
}
|
||||
/// <summary>
|
||||
/// Move a piece from a board position to another board position, potentially capturing an opponents piece. Respects all rules of the game.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The strategy involves simulating a move on a throw-away board state that can be used to
|
||||
/// validate legal vs illegal moves without having to worry about reverting board state.
|
||||
/// </remarks>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
public void Move(string from, string to, bool isPromotion = false)
|
||||
{
|
||||
// Validate the move
|
||||
var rulesState = new Piece?[9, 9];
|
||||
for (int x = 0; x < 9; x++)
|
||||
for (int y = 0; y < 9; y++)
|
||||
{
|
||||
rulesState[x, y] = this.BoardState[x, y];
|
||||
}
|
||||
|
||||
// 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))
|
||||
{
|
||||
throw new InvalidOperationException("Unable to move because you are still in check.");
|
||||
}
|
||||
var rules = new BoardRules<Piece>(rulesState);
|
||||
var validationResult = rules.ValidateMove(Notation.FromBoardNotation(from), Notation.FromBoardNotation(to), isPromotion);
|
||||
if (validationResult.IsError)
|
||||
{
|
||||
throw new InvalidOperationException(validationResult.ResultMessage);
|
||||
}
|
||||
|
||||
if (simulation.DidPlayerPutThemselfInCheck())
|
||||
{
|
||||
throw new InvalidOperationException("Illegal move. This move places you in check.");
|
||||
}
|
||||
// Move is valid, but is it legal?
|
||||
// Check for correct player's turn.
|
||||
if (BoardState.WhoseTurn != BoardState[from]!.Owner)
|
||||
{
|
||||
throw new InvalidOperationException("Not allowed to move the opponent's pieces.");
|
||||
}
|
||||
|
||||
var otherPlayer = BoardState.WhoseTurn == WhichPlayer.Player1
|
||||
? WhichPlayer.Player2
|
||||
: WhichPlayer.Player1;
|
||||
_ = rules.Move(from, to, isPromotion);
|
||||
if (rules.IsOpponentInCheckAfterMove())
|
||||
{
|
||||
BoardState.InCheck = otherPlayer;
|
||||
if (rules.IsOpponentInCheckMate())
|
||||
{
|
||||
BoardState.IsCheckmate = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
BoardState.InCheck = null;
|
||||
}
|
||||
BoardState.WhoseTurn = otherPlayer;
|
||||
}
|
||||
// Simulate the move on a throw -away state and look for "check" and "check-mate".
|
||||
var simulationState = new BoardState(BoardState);
|
||||
var moveResult = simulationState.Move(from, to, isPromotion);
|
||||
if (!moveResult.Success)
|
||||
{
|
||||
throw new InvalidOperationException(moveResult.Reason);
|
||||
}
|
||||
|
||||
public void Move(WhichPiece pieceInHand, string to)
|
||||
{
|
||||
var index = BoardState.ActivePlayerHand.FindIndex(p => p.WhichPiece == pieceInHand);
|
||||
if (index == -1)
|
||||
{
|
||||
throw new InvalidOperationException($"{pieceInHand} does not exist in the hand.");
|
||||
}
|
||||
var simulation = new StandardRules(simulationState);
|
||||
// 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))
|
||||
{
|
||||
throw new InvalidOperationException("Unable to move because you are still in check.");
|
||||
}
|
||||
|
||||
if (BoardState[to] != null)
|
||||
{
|
||||
throw new InvalidOperationException("Illegal placement of piece from the hand. Destination is not empty.");
|
||||
}
|
||||
if (simulation.DidPlayerPutThemselfInCheck())
|
||||
{
|
||||
throw new InvalidOperationException("Illegal move. This move places you in check.");
|
||||
}
|
||||
|
||||
var toVector = Notation.FromBoardNotation(to);
|
||||
switch (pieceInHand)
|
||||
{
|
||||
case WhichPiece.Knight:
|
||||
{
|
||||
// Knight cannot be placed onto the farthest two ranks from the hand.
|
||||
if (BoardState.WhoseTurn == WhichPlayer.Player1 && toVector.Y > 6
|
||||
|| BoardState.WhoseTurn == WhichPlayer.Player2 && toVector.Y < 2)
|
||||
{
|
||||
throw new InvalidOperationException("Illegal move. Knight has no valid moves after placement.");
|
||||
}
|
||||
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 && toVector.Y == 8
|
||||
|| BoardState.WhoseTurn == WhichPlayer.Player2 && toVector.Y == 0)
|
||||
{
|
||||
throw new InvalidOperationException($"Illegal move. {pieceInHand} has no valid moves after placement.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
var otherPlayer = BoardState.WhoseTurn == WhichPlayer.Player1
|
||||
? WhichPlayer.Player2
|
||||
: WhichPlayer.Player1;
|
||||
_ = BoardState.Move(from, to, isPromotion); // "Rules" should not be doing any data changes. "State" should do that.
|
||||
if (rules.IsOpponentInCheckAfterMove())
|
||||
{
|
||||
BoardState.InCheck = otherPlayer;
|
||||
if (rules.IsOpponentInCheckMate())
|
||||
{
|
||||
BoardState.IsCheckmate = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
BoardState.InCheck = null;
|
||||
}
|
||||
BoardState.WhoseTurn = otherPlayer;
|
||||
}
|
||||
|
||||
var tempBoard = new BoardState(BoardState);
|
||||
var simulation = new StandardRules(tempBoard);
|
||||
var moveResult = simulation.Move(pieceInHand, to);
|
||||
if (!moveResult.Success)
|
||||
{
|
||||
throw new InvalidOperationException(moveResult.Reason);
|
||||
}
|
||||
public void Move(WhichPiece pieceInHand, string to)
|
||||
{
|
||||
var index = BoardState.ActivePlayerHand.FindIndex(p => p.WhichPiece == pieceInHand);
|
||||
if (index == -1)
|
||||
{
|
||||
throw new InvalidOperationException($"{pieceInHand} does not exist in the hand.");
|
||||
}
|
||||
|
||||
// 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))
|
||||
{
|
||||
throw new InvalidOperationException("Unable to drop piece becauase you are still in check.");
|
||||
}
|
||||
if (BoardState[to] != null)
|
||||
{
|
||||
throw new InvalidOperationException("Illegal placement of piece from the hand. Destination is not empty.");
|
||||
}
|
||||
|
||||
if (simulation.DidPlayerPutThemselfInCheck())
|
||||
{
|
||||
throw new InvalidOperationException("Illegal move. This move places you in check.");
|
||||
}
|
||||
var toVector = Notation.FromBoardNotation(to);
|
||||
switch (pieceInHand)
|
||||
{
|
||||
case WhichPiece.Knight:
|
||||
{
|
||||
// Knight cannot be placed onto the farthest two ranks from the hand.
|
||||
if (BoardState.WhoseTurn == WhichPlayer.Player1 && toVector.Y > 6
|
||||
|| BoardState.WhoseTurn == WhichPlayer.Player2 && toVector.Y < 2)
|
||||
{
|
||||
throw new InvalidOperationException("Illegal move. Knight has no valid moves after placement.");
|
||||
}
|
||||
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 && toVector.Y == 8
|
||||
|| BoardState.WhoseTurn == WhichPlayer.Player2 && toVector.Y == 0)
|
||||
{
|
||||
throw new InvalidOperationException($"Illegal move. {pieceInHand} has no valid moves after placement.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
var tempBoard = new BoardState(BoardState);
|
||||
var simulation = new StandardRules(tempBoard);
|
||||
var moveResult = simulation.Move(pieceInHand, to);
|
||||
if (!moveResult.Success)
|
||||
{
|
||||
throw new InvalidOperationException(moveResult.Reason);
|
||||
}
|
||||
|
||||
var kingPosition = otherPlayer == WhichPlayer.Player1
|
||||
? tempBoard.Player1KingPosition
|
||||
: tempBoard.Player2KingPosition;
|
||||
BoardState.WhoseTurn = otherPlayer;
|
||||
}
|
||||
// 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))
|
||||
{
|
||||
throw new InvalidOperationException("Unable to drop piece becauase you are still in check.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prints a ASCII representation of the board for debugging board state.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string ToStringStateAsAscii()
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
builder.Append(" ");
|
||||
builder.Append("Player 2");
|
||||
builder.AppendLine();
|
||||
for (var rank = 8; rank >= 0; rank--)
|
||||
{
|
||||
// Horizontal line
|
||||
builder.Append(" - ");
|
||||
for (var file = 0; file < 8; file++) builder.Append("- - ");
|
||||
builder.Append("- -");
|
||||
if (simulation.DidPlayerPutThemselfInCheck())
|
||||
{
|
||||
throw new InvalidOperationException("Illegal move. This move places you in check.");
|
||||
}
|
||||
|
||||
// Print Rank ruler.
|
||||
builder.AppendLine();
|
||||
builder.Append($"{rank + 1} ");
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
// Print pieces.
|
||||
builder.Append(" |");
|
||||
for (var x = 0; x < 9; x++)
|
||||
{
|
||||
var piece = BoardState[x, rank];
|
||||
if (piece == null)
|
||||
{
|
||||
builder.Append(" ");
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.AppendFormat("{0}", ToAscii(piece));
|
||||
}
|
||||
builder.Append('|');
|
||||
}
|
||||
builder.AppendLine();
|
||||
}
|
||||
var kingPosition = otherPlayer == WhichPlayer.Player1
|
||||
? tempBoard.Player1KingPosition
|
||||
: tempBoard.Player2KingPosition;
|
||||
BoardState.WhoseTurn = otherPlayer;
|
||||
}
|
||||
|
||||
// Horizontal line
|
||||
builder.Append(" - ");
|
||||
for (var x = 0; x < 8; x++) builder.Append("- - ");
|
||||
builder.Append("- -");
|
||||
builder.AppendLine();
|
||||
builder.Append(" ");
|
||||
builder.Append("Player 1");
|
||||
/// <summary>
|
||||
/// Prints a ASCII representation of the board for debugging board state.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string ToStringStateAsAscii()
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
builder.Append(" ");
|
||||
builder.Append("Player 2");
|
||||
builder.AppendLine();
|
||||
for (var rank = 8; rank >= 0; rank--)
|
||||
{
|
||||
// Horizontal line
|
||||
builder.Append(" - ");
|
||||
for (var file = 0; file < 8; file++) builder.Append("- - ");
|
||||
builder.Append("- -");
|
||||
|
||||
builder.AppendLine();
|
||||
builder.AppendLine();
|
||||
// Print File ruler.
|
||||
builder.Append(" ");
|
||||
builder.Append(" A B C D E F G H I ");
|
||||
// Print Rank ruler.
|
||||
builder.AppendLine();
|
||||
builder.Append($"{rank + 1} ");
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
// Print pieces.
|
||||
builder.Append(" |");
|
||||
for (var x = 0; x < 9; x++)
|
||||
{
|
||||
var piece = BoardState[x, rank];
|
||||
if (piece == null)
|
||||
{
|
||||
builder.Append(" ");
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.AppendFormat("{0}", ToAscii(piece));
|
||||
}
|
||||
builder.Append('|');
|
||||
}
|
||||
builder.AppendLine();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="piece"></param>
|
||||
/// <returns>
|
||||
/// A string with three characters.
|
||||
/// The first character indicates promotion status.
|
||||
/// The second character indicates piece.
|
||||
/// The third character indicates ownership.
|
||||
/// </returns>
|
||||
private static string ToAscii(Piece piece)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
if (piece.IsPromoted) builder.Append('^');
|
||||
else builder.Append(' ');
|
||||
// Horizontal line
|
||||
builder.Append(" - ");
|
||||
for (var x = 0; x < 8; x++) builder.Append("- - ");
|
||||
builder.Append("- -");
|
||||
builder.AppendLine();
|
||||
builder.Append(" ");
|
||||
builder.Append("Player 1");
|
||||
|
||||
var name = piece.WhichPiece switch
|
||||
{
|
||||
WhichPiece.King => "K",
|
||||
WhichPiece.GoldGeneral => "G",
|
||||
WhichPiece.SilverGeneral => "S",
|
||||
WhichPiece.Bishop => "B",
|
||||
WhichPiece.Rook => "R",
|
||||
WhichPiece.Knight => "k",
|
||||
WhichPiece.Lance => "L",
|
||||
WhichPiece.Pawn => "P",
|
||||
_ => throw new ArgumentException($"Unknown value for {nameof(WhichPiece)}."),
|
||||
};
|
||||
builder.Append(name);
|
||||
builder.AppendLine();
|
||||
builder.AppendLine();
|
||||
// Print File ruler.
|
||||
builder.Append(" ");
|
||||
builder.Append(" A B C D E F G H I ");
|
||||
|
||||
if (piece.Owner == WhichPlayer.Player2) builder.Append('.');
|
||||
else builder.Append(' ');
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="piece"></param>
|
||||
/// <returns>
|
||||
/// A string with three characters.
|
||||
/// The first character indicates promotion status.
|
||||
/// The second character indicates piece.
|
||||
/// The third character indicates ownership.
|
||||
/// </returns>
|
||||
private static string ToAscii(Piece piece)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
if (piece.IsPromoted) builder.Append('^');
|
||||
else builder.Append(' ');
|
||||
|
||||
var name = piece.WhichPiece switch
|
||||
{
|
||||
WhichPiece.King => "K",
|
||||
WhichPiece.GoldGeneral => "G",
|
||||
WhichPiece.SilverGeneral => "S",
|
||||
WhichPiece.Bishop => "B",
|
||||
WhichPiece.Rook => "R",
|
||||
WhichPiece.Knight => "k",
|
||||
WhichPiece.Lance => "L",
|
||||
WhichPiece.Pawn => "P",
|
||||
_ => throw new ArgumentException($"Unknown value for {nameof(WhichPiece)}."),
|
||||
};
|
||||
builder.Append(name);
|
||||
|
||||
if (piece.Owner == WhichPlayer.Player2) builder.Append('.');
|
||||
else builder.Append(' ');
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user