This commit is contained in:
2022-01-29 10:07:53 -06:00
parent 499e480851
commit f79a1312c7
4 changed files with 190 additions and 161 deletions

View File

@@ -43,16 +43,24 @@
public void Move(string from, string to, bool isPromotion)
{
var fromVector = ShogiBoardState.FromBoardNotation(from);
var toVector = ShogiBoardState.FromBoardNotation(to);
var moveResult = rules.Move(from, to, isPromotion);
var tempBoard = new ShogiBoardState(board);
var simulation = new StandardRules(tempBoard);
var moveResult = simulation.Move(from, to, isPromotion);
if (!moveResult.Success)
{
throw new InvalidOperationException(moveResult.Reason);
}
var fromVector = ShogiBoardState.FromBoardNotation(from);
var toVector = ShogiBoardState.FromBoardNotation(to);
var otherPlayer = board.WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
if (rules.EvaluateCheckAfterMove(fromVector, toVector, otherPlayer))
if (simulation.IsPlayerInCheckAfterMove(fromVector, toVector, board.WhoseTurn))
{
throw new InvalidOperationException("Illegal move. This move places you in check.");
}
rules.Move(from, to, isPromotion);
if (rules.IsPlayerInCheckAfterMove(fromVector, toVector, otherPlayer))
{
board.InCheck = otherPlayer;
board.IsCheckmate = rules.EvaluateCheckmate();
@@ -64,6 +72,10 @@
board.WhoseTurn = otherPlayer;
}
public void Move(WhichPiece pieceInHand, string to)
{
}
///// <summary>
///// Attempts a given move. Returns false if the move is illegal.
///// </summary>

View File

@@ -42,6 +42,9 @@ namespace Shogi.Domain
{
board[kvp.Key] = kvp.Value == null ? null : new Piece(kvp.Value);
}
WhoseTurn = other.WhoseTurn;
InCheck = other.InCheck;
IsCheckmate = other.IsCheckmate;
MoveHistory.AddRange(other.MoveHistory);
Player1Hand.AddRange(other.Player1Hand);
Player2Hand.AddRange(other.Player2Hand);

View File

@@ -39,11 +39,11 @@ namespace Shogi.Domain
}
/// <summary>
/// Move a piece from a board tile to another board tile.
/// 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>
/// <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 move.</returns>
public MoveResult Move(string fromNotation, string toNotation, bool isPromotion = false)
{
var from = ShogiBoardState.FromBoardNotation(fromNotation);
@@ -105,7 +105,7 @@ namespace Shogi.Domain
}
/// <summary>
/// Move a piece from the hand to the board.
/// 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>
@@ -184,7 +184,7 @@ namespace Shogi.Domain
return isCheck;
}
public bool EvaluateCheckAfterMove(Vector2 from, Vector2 to, WhichPlayer whichPlayer)
public bool IsPlayerInCheckAfterMove(Vector2 from, Vector2 to, WhichPlayer whichPlayer)
{
if (whichPlayer == board.InCheck) return true; // If we already know the player is in check, don't bother.
@@ -265,7 +265,7 @@ namespace Shogi.Domain
var simulationResult = simulationBoard.Move(fromNotation, toNotation, false);
if (simulationResult.Success)
{
if (!EvaluateCheckAfterMove(from, position, board.InCheck.Value))
if (!IsPlayerInCheckAfterMove(from, position, board.InCheck.Value))
{
isCheckmate = false;
}