Files
Shogi/Shogi.Domain/StandardRules.cs

269 lines
7.3 KiB
C#

using System.Numerics;
namespace Shogi.Domain
{
internal class StandardRules
{
private readonly ShogiBoardState board;
private Vector2 player1KingPosition;
private Vector2 player2KingPosition;
public StandardRules(ShogiBoardState board)
{
this.board = board;
CacheKingPositions();
}
private void CacheKingPositions()
{
this.board.ForEachNotNull((tile, position) =>
{
if (tile.WhichPiece == WhichPiece.King)
{
if (tile.Owner == WhichPlayer.Player1)
{
player1KingPosition = position;
}
else if (tile.Owner == WhichPlayer.Player2)
{
player2KingPosition = position;
}
}
});
}
/// <summary>
/// Move a piece from a board tile to another board tile.
/// </summary>
/// <param name="from">The position of the piece being moved 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>
public MoveResult Move(string from, string to, bool isPromotion = false)
{
var fromPiece = board[from];
if (fromPiece == null)
{
return new MoveResult(false, $"Tile [{from}] is empty. There is no piece to move.");
}
if (fromPiece.Owner != board.WhoseTurn)
{
return new MoveResult(false, "Not allowed to move the opponents piece");
}
if (IsPathable(move.From.Value, move.To) == false)
{
return new MoveResult(false, $"Proposed move is not part of the move-set for piece {fromPiece.WhichPiece}.");
}
var captured = board[to];
if (captured != null)
{
if (captured.Owner == board.WhoseTurn)
{
return new MoveResult(false, "Capturing your own piece is not allowed.");
}
captured.Capture();
board.Hand.Add(captured);
}
//Mutate the board.
if (isPromotion)
{
var fromVector = ShogiBoardState.FromBoardNotation(from);
var toVector = ShogiBoardState.FromBoardNotation(to);
if (board.WhoseTurn == WhichPlayer.Player1 && (toVector.Y > 5 || fromVector.Y > 5))
{
fromPiece.Promote();
}
else if (board.WhoseTurn == WhichPlayer.Player2 && (toVector.Y < 3 || fromVector.Y < 3))
{
fromPiece.Promote();
}
}
board[to] = fromPiece;
board[from] = null;
if (fromPiece.WhichPiece == WhichPiece.King)
{
if (fromPiece.Owner == WhichPlayer.Player1)
{
player1King.X = move.To.X;
player1King.Y = move.To.Y;
}
else if (fromPiece.Owner == WhichPlayer.Player2)
{
player2King.X = move.To.X;
player2King.Y = move.To.Y;
}
}
MoveHistory.Add(move);
return true;
}
/// <summary>
/// Move a piece from the hand to the board.
/// </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>
public void Move(WhichPiece pieceInHand, string to)
{
var index = Hand.FindIndex(p => p.WhichPiece == move.PieceFromHand);
if (index == -1)
{
Error = $"{move.PieceFromHand} does not exist in the hand.";
return false;
}
if (Board[move.To] != null)
{
Error = $"Illegal move - attempting to capture while playing a piece from the hand.";
return false;
}
switch (move.PieceFromHand!.Value)
{
case WhichPiece.Knight:
{
// Knight cannot be placed onto the farthest two ranks from the hand.
if ((WhoseTurn == WhichPlayer.Player1 && move.To.Y > 6)
|| (WhoseTurn == WhichPlayer.Player2 && move.To.Y < 2))
{
Error = $"Knight has no valid moves after placed.";
return false;
}
break;
}
case WhichPiece.Lance:
case WhichPiece.Pawn:
{
// Lance and Pawn cannot be placed onto the farthest rank from the hand.
if ((WhoseTurn == WhichPlayer.Player1 && move.To.Y == 8)
|| (WhoseTurn == WhichPlayer.Player2 && move.To.Y == 0))
{
Error = $"{move.PieceFromHand} has no valid moves after placed.";
return false;
}
break;
}
}
// Mutate the board.
Board[move.To] = Hand[index];
Hand.RemoveAt(index);
MoveHistory.Add(move);
return true;
}
private bool IsPathable(Vector2 from, Vector2 to)
{
var piece = Board[from];
if (piece == null) return false;
var isObstructed = false;
var isPathable = pathFinder.PathTo(from, to, (other, position) =>
{
if (other.Owner == piece.Owner) isObstructed = true;
});
return !isObstructed && isPathable;
}
private bool EvaluateCheckAfterMove(Move move, WhichPlayer WhichPerspective)
{
if (WhichPerspective == InCheck) return true; // If we already know the player is in check, don't bother.
var isCheck = false;
var kingPosition = WhichPerspective == WhichPlayer.Player1 ? player1King : player2King;
// Check if the move put the king in check.
if (pathFinder.PathTo(move.To, kingPosition)) return true;
if (move.From.HasValue)
{
// Get line equation from king through the now-unoccupied location.
var direction = Vector2.Subtract(kingPosition, move.From!.Value);
var slope = Math.Abs(direction.Y / direction.X);
// 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))
{
// if slope of the move is also infinity...can skip this?
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
{
if (piece.Owner != WhichPerspective)
{
switch (piece.WhichPiece)
{
case WhichPiece.Rook:
isCheck = true;
break;
case WhichPiece.Lance:
if (!piece.IsPromoted) isCheck = true;
break;
}
}
});
}
else if (slope == 1)
{
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
{
if (piece.Owner != WhichPerspective && piece.WhichPiece == WhichPiece.Bishop)
{
isCheck = true;
}
});
}
else if (slope == 0)
{
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
{
if (piece.Owner != WhichPerspective && piece.WhichPiece == WhichPiece.Rook)
{
isCheck = true;
}
});
}
}
else
{
// TODO: Check for illegal move from hand. It is illegal to place from the hand such that you check-mate your opponent.
// Go read the shogi rules to be sure this is true.
}
return isCheck;
}
private bool EvaluateCheckmate()
{
if (!InCheck.HasValue) return false;
// Assume true and try to disprove.
var isCheckmate = true;
Board.ForEachNotNull((piece, from) => // For each piece...
{
// Short circuit
if (!isCheckmate) return;
if (piece.Owner == InCheck) // ...owned by the player in check...
{
// ...evaluate if any move gets the player out of check.
pathFinder.PathEvery(from, (other, position) =>
{
var simulationBoard = new Shogi(this);
var moveToTry = new Move(from, position);
var moveSuccess = simulationBoard.TryMove(moveToTry);
if (moveSuccess)
{
if (!EvaluateCheckAfterMove(moveToTry, InCheck.Value))
{
isCheckmate = false;
}
}
});
}
});
return isCheckmate;
}
}
}