yep
This commit is contained in:
@@ -4,6 +4,10 @@ namespace Shogi.Domain
|
||||
{
|
||||
internal class StandardRules
|
||||
{
|
||||
/// <param name="element">Guaranteed to be non-null.</param>
|
||||
/// <param name="position"></param>
|
||||
public delegate void Callback(Piece collider, Vector2 position);
|
||||
|
||||
private readonly ShogiBoardState board;
|
||||
private Vector2 player1KingPosition;
|
||||
private Vector2 player2KingPosition;
|
||||
@@ -38,18 +42,20 @@ namespace Shogi.Domain
|
||||
/// <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)
|
||||
public MoveResult Move(Vector2 from, Vector2 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)
|
||||
|
||||
if (IsPathable(from, to) == false)
|
||||
{
|
||||
return new MoveResult(false, $"Proposed move is not part of the move-set for piece {fromPiece.WhichPiece}.");
|
||||
}
|
||||
@@ -68,13 +74,11 @@ namespace Shogi.Domain
|
||||
//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))
|
||||
if (board.WhoseTurn == WhichPlayer.Player1 && (to.Y > 5 || from.Y > 5))
|
||||
{
|
||||
fromPiece.Promote();
|
||||
}
|
||||
else if (board.WhoseTurn == WhichPlayer.Player2 && (toVector.Y < 3 || fromVector.Y < 3))
|
||||
else if (board.WhoseTurn == WhichPlayer.Player2 && (to.Y < 3 || from.Y < 3))
|
||||
{
|
||||
fromPiece.Promote();
|
||||
}
|
||||
@@ -85,17 +89,15 @@ namespace Shogi.Domain
|
||||
{
|
||||
if (fromPiece.Owner == WhichPlayer.Player1)
|
||||
{
|
||||
player1King.X = move.To.X;
|
||||
player1King.Y = move.To.Y;
|
||||
player1KingPosition = from;
|
||||
}
|
||||
else if (fromPiece.Owner == WhichPlayer.Player2)
|
||||
{
|
||||
player2King.X = move.To.X;
|
||||
player2King.Y = move.To.Y;
|
||||
player2KingPosition = from;
|
||||
}
|
||||
}
|
||||
MoveHistory.Add(move);
|
||||
return true;
|
||||
//MoveHistory.Add(move);
|
||||
return new MoveResult(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -104,30 +106,27 @@ namespace Shogi.Domain
|
||||
/// <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)
|
||||
public MoveResult Move(WhichPiece pieceInHand, Vector2 to)
|
||||
{
|
||||
var index = Hand.FindIndex(p => p.WhichPiece == move.PieceFromHand);
|
||||
var index = board.Hand.FindIndex(p => p.WhichPiece == pieceInHand);
|
||||
if (index == -1)
|
||||
{
|
||||
Error = $"{move.PieceFromHand} does not exist in the hand.";
|
||||
return false;
|
||||
return new MoveResult(false, $"{pieceInHand} does not exist in the hand.");
|
||||
}
|
||||
if (Board[move.To] != null)
|
||||
if (board[to] != null)
|
||||
{
|
||||
Error = $"Illegal move - attempting to capture while playing a piece from the hand.";
|
||||
return false;
|
||||
return new MoveResult(false, $"Illegal move - attempting to capture while playing a piece from the hand.");
|
||||
}
|
||||
|
||||
switch (move.PieceFromHand!.Value)
|
||||
switch (pieceInHand)
|
||||
{
|
||||
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))
|
||||
if ((board.WhoseTurn == WhichPlayer.Player1 && to.Y > 6)
|
||||
|| (board.WhoseTurn == WhichPlayer.Player2 && to.Y < 2))
|
||||
{
|
||||
Error = $"Knight has no valid moves after placed.";
|
||||
return false;
|
||||
return new MoveResult(false, "Knight has no valid moves after placed.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -135,134 +134,251 @@ namespace Shogi.Domain
|
||||
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))
|
||||
if ((board.WhoseTurn == WhichPlayer.Player1 && to.Y == 8)
|
||||
|| (board.WhoseTurn == WhichPlayer.Player2 && to.Y == 0))
|
||||
{
|
||||
Error = $"{move.PieceFromHand} has no valid moves after placed.";
|
||||
return false;
|
||||
return new MoveResult(false, $"{pieceInHand} has no valid moves after placed.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Mutate the board.
|
||||
Board[move.To] = Hand[index];
|
||||
Hand.RemoveAt(index);
|
||||
MoveHistory.Add(move);
|
||||
|
||||
return true;
|
||||
board[to] = board.Hand[index];
|
||||
board.Hand.RemoveAt(index);
|
||||
//MoveHistory.Add(move);
|
||||
return new MoveResult(true);
|
||||
}
|
||||
|
||||
private bool IsPathable(Vector2 from, Vector2 to)
|
||||
{
|
||||
var piece = Board[from];
|
||||
var piece = board[from];
|
||||
if (piece == null) return false;
|
||||
|
||||
var isObstructed = false;
|
||||
var isPathable = pathFinder.PathTo(from, to, (other, position) =>
|
||||
var isPathable = PathTo(from, to, (other, position) =>
|
||||
{
|
||||
if (other.Owner == piece.Owner) isObstructed = true;
|
||||
});
|
||||
return !isObstructed && isPathable;
|
||||
}
|
||||
|
||||
private bool EvaluateCheckAfterMove(Move move, WhichPlayer WhichPerspective)
|
||||
public bool EvaluateCheckAfterMove(WhichPiece pieceInHand, Vector2 to, WhichPlayer whichPlayer)
|
||||
{
|
||||
if (WhichPerspective == InCheck) return true; // If we already know the player is in check, don't bother.
|
||||
if (whichPlayer == board.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;
|
||||
var kingPosition = whichPlayer == WhichPlayer.Player1 ? player1KingPosition : player2KingPosition;
|
||||
|
||||
// Check if the move put the king in check.
|
||||
if (pathFinder.PathTo(move.To, kingPosition)) return true;
|
||||
if (PathTo(to, kingPosition)) return true;
|
||||
|
||||
if (move.From.HasValue)
|
||||
// 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))
|
||||
{
|
||||
// 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?
|
||||
LinePathTo(kingPosition, direction, (piece, position) =>
|
||||
{
|
||||
// if slope of the move is also infinity...can skip this?
|
||||
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
|
||||
if (piece.Owner != whichPlayer)
|
||||
{
|
||||
if (piece.Owner != WhichPerspective)
|
||||
switch (piece.WhichPiece)
|
||||
{
|
||||
switch (piece.WhichPiece)
|
||||
{
|
||||
case WhichPiece.Rook:
|
||||
isCheck = true;
|
||||
break;
|
||||
case WhichPiece.Lance:
|
||||
if (!piece.IsPromoted) isCheck = true;
|
||||
break;
|
||||
}
|
||||
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
|
||||
else if (slope == 1)
|
||||
{
|
||||
// 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.
|
||||
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;
|
||||
}
|
||||
|
||||
private bool EvaluateCheckmate()
|
||||
public bool EvaluateCheckmate()
|
||||
{
|
||||
if (!InCheck.HasValue) return false;
|
||||
if (!board.InCheck.HasValue) return false;
|
||||
|
||||
// Assume true and try to disprove.
|
||||
var isCheckmate = true;
|
||||
Board.ForEachNotNull((piece, from) => // For each piece...
|
||||
board.ForEachNotNull((piece, from) => // For each piece...
|
||||
{
|
||||
// Short circuit
|
||||
if (!isCheckmate) return;
|
||||
|
||||
if (piece.Owner == InCheck) // ...owned by the player in check...
|
||||
if (piece.Owner == board.InCheck) // ...owned by the player in check...
|
||||
{
|
||||
// ...evaluate if any move gets the player out of check.
|
||||
pathFinder.PathEvery(from, (other, position) =>
|
||||
PathEvery(from, (other, position) =>
|
||||
{
|
||||
var simulationBoard = new Shogi(this);
|
||||
var moveToTry = new Move(from, position);
|
||||
var moveSuccess = simulationBoard.TryMove(moveToTry);
|
||||
if (moveSuccess)
|
||||
var simulationBoard = new StandardRules(new ShogiBoardState(board));
|
||||
var simulationResult = simulationBoard.Move(from, position, false);
|
||||
if (simulationResult.Success)
|
||||
{
|
||||
if (!EvaluateCheckAfterMove(moveToTry, InCheck.Value))
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Navigate the collection such that each "step" is always towards the destination, respecting the Paths available to the element at origin.
|
||||
/// </summary>
|
||||
/// <param name="element">The pathing element.</param>
|
||||
/// <param name="origin">The starting location.</param>
|
||||
/// <param name="destination">The destination.</param>
|
||||
/// <param name="callback">Do cool stuff here.</param>
|
||||
/// <returns>True if the element reached the destination.</returns>
|
||||
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))
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Path the line from origin to destination, ignoring any Paths defined by the element at origin.
|
||||
/// </summary>
|
||||
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<Move> 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."),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user