using System.Numerics; using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("Shogi.Domain.UnitTests")] namespace Shogi.Domain { internal class StandardRules { /// Guaranteed to be non-null. /// public delegate void Callback(Piece collider, Vector2 position); 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; } } }); } /// /// Move a piece from a board tile to another board tile. /// /// The position of the piece being moved expressed in board notation. /// The target position expressed in board notation. /// A describing the success or failure of the simulation. public MoveResult Move(string fromNotation, string toNotation, bool isPromotion = false) { var from = ShogiBoardState.FromBoardNotation(fromNotation); var to = ShogiBoardState.FromBoardNotation(toNotation); var fromPiece = board[from]; if (fromPiece == null) { return new MoveResult(false, $"Tile [{fromNotation}] 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 (ShogiIsPathable(from, 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) { if (board.WhoseTurn == WhichPlayer.Player1 && (to.Y > 5 || from.Y > 5)) { fromPiece.Promote(); } else if (board.WhoseTurn == WhichPlayer.Player2 && (to.Y < 3 || from.Y < 3)) { fromPiece.Promote(); } } board[to] = fromPiece; board[from] = null; if (fromPiece.WhichPiece == WhichPiece.King) { if (fromPiece.Owner == WhichPlayer.Player1) { player1KingPosition = from; } else if (fromPiece.Owner == WhichPlayer.Player2) { player2KingPosition = from; } } //MoveHistory.Add(move); return new MoveResult(true); } /// /// Move a piece from the hand to the board. /// /// /// The target position expressed in board notation. /// A describing the success or failure of the simulation. public MoveResult Move(WhichPiece pieceInHand, string toNotation) { var to = ShogiBoardState.FromBoardNotation(toNotation); var index = board.Hand.FindIndex(p => p.WhichPiece == pieceInHand); if (index == -1) { return new MoveResult(false, $"{pieceInHand} does not exist in the hand."); } if (board[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 ((board.WhoseTurn == WhichPlayer.Player1 && to.Y > 6) || (board.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 ((board.WhoseTurn == WhichPlayer.Player1 && to.Y == 8) || (board.WhoseTurn == WhichPlayer.Player2 && to.Y == 0)) { return new MoveResult(false, $"{pieceInHand} has no valid moves after placed."); } break; } } // Mutate the board. board[to] = board.Hand[index]; board.Hand.RemoveAt(index); //MoveHistory.Add(move); return new MoveResult(true); } private bool ShogiIsPathable(Vector2 from, Vector2 to) { var piece = board[from]; if (piece == null) return false; var isObstructed = false; var isPathable = PathTo(from, to, (other, position) => { if (other.Owner == piece.Owner) isObstructed = true; }); return !isObstructed && isPathable; } public bool EvaluateCheckAfterMove(WhichPiece pieceInHand, Vector2 to, WhichPlayer whichPlayer) { if (whichPlayer == board.InCheck) return true; // If we already know the player is in check, don't bother. var isCheck = false; var kingPosition = whichPlayer == WhichPlayer.Player1 ? player1KingPosition : player2KingPosition; // Check if the move put the king in check. if (PathTo(to, kingPosition)) return true; // 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; } public bool EvaluateCheckAfterMove(Vector2 from, Vector2 to, WhichPlayer whichPlayer) { if (whichPlayer == board.InCheck) return true; // If we already know the player is in check, don't bother. var isCheck = false; var kingPosition = whichPlayer == WhichPlayer.Player1 ? player1KingPosition : player2KingPosition; // Check if the move put the king in check. if (PathTo(to, kingPosition)) return true; // Get line equation from king through the now-unoccupied location. var direction = Vector2.Subtract(kingPosition, from); 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? LinePathTo(kingPosition, direction, (piece, position) => { if (piece.Owner != whichPlayer) { switch (piece.WhichPiece) { case WhichPiece.Rook: isCheck = true; break; case WhichPiece.Lance: if (!piece.IsPromoted) isCheck = true; break; } } }); } else if (slope == 1) { LinePathTo(kingPosition, direction, (piece, position) => { if (piece.Owner != whichPlayer && piece.WhichPiece == WhichPiece.Bishop) { isCheck = true; } }); } else if (slope == 0) { LinePathTo(kingPosition, direction, (piece, position) => { if (piece.Owner != whichPlayer && piece.WhichPiece == WhichPiece.Rook) { isCheck = true; } }); } return isCheck; } public bool EvaluateCheckmate() { if (!board.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 == board.InCheck) // ...owned by the player in check... { // ...evaluate if any move gets the player out of check. PathEvery(from, (other, position) => { var simulationBoard = new StandardRules(new ShogiBoardState(board)); var fromNotation = ShogiBoardState.ToBoardNotation(from); var toNotation = ShogiBoardState.ToBoardNotation(position); var simulationResult = simulationBoard.Move(fromNotation, toNotation, false); if (simulationResult.Success) { if (!EvaluateCheckAfterMove(from, position, board.InCheck.Value)) { isCheckmate = false; } } }); } // TODO: Assert that a player could not place a piece from their hand to avoid check. }); return isCheckmate; } /// /// Navigate the collection such that each "step" is always towards the destination, respecting the Paths available to the element at origin. /// /// The pathing element. /// The starting location. /// The destination. /// Do cool stuff here. /// True if the element reached the destination. public bool PathTo(Vector2 origin, Vector2 destination, Callback? callback = null) { if (destination.X > 8 || destination.Y > 8 || destination.X < 0 || destination.Y < 0) { return false; } var piece = board[origin]; if (piece == null) return false; var path = FindDirectionTowardsDestination(GetMoveSet(piece.WhichPiece).GetMoves(piece.IsUpsideDown), origin, destination); if (!IsPathable(origin, destination, path.Direction)) { // Assumption: if a single best-choice step towards the destination cannot happen, no pathing can happen. return false; } var shouldPath = true; var next = origin; while (shouldPath && next != destination) { next = Vector2.Add(next, path.Direction); var collider = board[next]; if (collider != null) { callback?.Invoke(collider, next); shouldPath = false; } else if (path.Distance == Distance.OneStep) { shouldPath = false; } } return next == destination; } public void PathEvery(Vector2 from, Callback callback) { var piece = board[from]; if (piece == null) { return; } foreach (var path in GetMoveSet(piece.WhichPiece).GetMoves(piece.IsUpsideDown)) { var shouldPath = true; var next = Vector2.Add(from, path.Direction); ; while (shouldPath && next.X < 8 && next.Y < 8 && next.X >= 0 && next.Y >= 0) { var collider = board[(int)next.Y, (int)next.X]; if (collider != null) { callback(collider, next); shouldPath = false; } if (path.Distance == Distance.OneStep) { shouldPath = false; } next = Vector2.Add(next, path.Direction); } } } public static bool IsPathable(Vector2 origin, Vector2 destination, Vector2 direction) { var next = Vector2.Add(origin, direction); if (Vector2.Distance(next, destination) >= Vector2.Distance(origin, destination)) return false; var slope = (destination.Y - origin.Y) / (destination.X - origin.X); if (float.IsInfinity(slope)) { return next.X == destination.X; } else { // b = -mx + y var yIntercept = -slope * origin.X + origin.Y; // y = mx + b return next.Y == slope * next.X + yIntercept; } } /// /// Path the line from origin to destination, ignoring any Paths defined by the element at origin. /// public void LinePathTo(Vector2 origin, Vector2 direction, Callback callback) { direction = Vector2.Normalize(direction); var next = Vector2.Add(origin, direction); while (next.X >= 0 && next.X < 8 && next.Y >= 0 && next.Y < 8) { var element = board[next]; if (element != null) callback(element, next); next = Vector2.Add(next, direction); } } public static Move FindDirectionTowardsDestination(ICollection paths, Vector2 origin, Vector2 destination) => paths.Aggregate((a, b) => { var distanceA = Vector2.Distance(destination, Vector2.Add(origin, a.Direction)); var distanceB = Vector2.Distance(destination, Vector2.Add(origin, b.Direction)); return distanceA < distanceB ? a : b; }); public static MoveSet GetMoveSet(WhichPiece whichPiece) { return whichPiece switch { WhichPiece.King => MoveSet.King, WhichPiece.GoldGeneral => MoveSet.GoldGeneral, WhichPiece.SilverGeneral => MoveSet.SilverGeneral, WhichPiece.Bishop => MoveSet.Bishop, WhichPiece.Rook => MoveSet.Rook, WhichPiece.Knight => MoveSet.Knight, WhichPiece.Lance => MoveSet.Lance, WhichPiece.Pawn => MoveSet.Pawn, _ => throw new ArgumentException($"{nameof(WhichPiece)} not recognized."), }; } } }