yep
This commit is contained in:
@@ -44,11 +44,15 @@ namespace Gameboard.ShogiUI.BoardState
|
|||||||
{
|
{
|
||||||
for (var i = 0; i < moves.Count; i++)
|
for (var i = 0; i < moves.Count; i++)
|
||||||
{
|
{
|
||||||
if (!Move(moves[i]))
|
if (!TryMove(moves[i]))
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Unable to construct ShogiBoard with the given move at index {i}.");
|
throw new InvalidOperationException($"Unable to construct ShogiBoard with the given move at index {i}.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (EvaluateCheckAfterMove(WhoseTurn))
|
||||||
|
{
|
||||||
|
InCheck = WhoseTurn;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ShogiBoard(ShogiBoard toCopy)
|
private ShogiBoard(ShogiBoard toCopy)
|
||||||
@@ -72,7 +76,6 @@ namespace Gameboard.ShogiUI.BoardState
|
|||||||
public bool Move(Move move)
|
public bool Move(Move move)
|
||||||
{
|
{
|
||||||
|
|
||||||
var otherPlayer = WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
|
||||||
var moveSuccess = TryMove(move);
|
var moveSuccess = TryMove(move);
|
||||||
|
|
||||||
if (!moveSuccess)
|
if (!moveSuccess)
|
||||||
@@ -81,11 +84,14 @@ namespace Gameboard.ShogiUI.BoardState
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Evaluate check
|
// Evaluate check
|
||||||
InCheck = EvaluateCheck(otherPlayer) ? otherPlayer : null;
|
if (EvaluateCheckAfterMove(WhoseTurn))
|
||||||
|
{
|
||||||
|
InCheck = WhoseTurn;
|
||||||
if (InCheck.HasValue)
|
if (InCheck.HasValue)
|
||||||
{
|
{
|
||||||
//IsCheckmate = EvaluateCheckmate();
|
//IsCheckmate = EvaluateCheckmate();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -109,47 +115,17 @@ namespace Gameboard.ShogiUI.BoardState
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Assert that this move does not put the moving player in check.
|
// Assert that this move does not put the moving player in check.
|
||||||
if (validationBoard.EvaluateCheck(WhoseTurn)) return false;
|
if (validationBoard.EvaluateCheckAfterMove(WhoseTurn))
|
||||||
|
{
|
||||||
|
// Sneakily using this.WhoseTurn instead of validationBoard.WhoseTurn;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// The move is valid and legal; update board state.
|
// The move is valid and legal; update board state.
|
||||||
if (move.PieceFromCaptured.HasValue) PlaceFromHand(move);
|
if (move.PieceFromCaptured.HasValue) PlaceFromHand(move);
|
||||||
else PlaceFromBoard(move);
|
else PlaceFromBoard(move);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
private bool EvaluateCheckmate()
|
|
||||||
{
|
|
||||||
if (!InCheck.HasValue) return false;
|
|
||||||
|
|
||||||
// Assume true and try to disprove.
|
|
||||||
var isCheckmate = true;
|
|
||||||
Board.ForEachNotNull((piece, x, y) => // For each piece...
|
|
||||||
{
|
|
||||||
if (!isCheckmate) return; // Short circuit
|
|
||||||
|
|
||||||
var from = new Vector2(x, y);
|
|
||||||
if (piece.Owner == InCheck) // Owned by the player in check...
|
|
||||||
{
|
|
||||||
var positionsToCheck = new List<Vector2>(10);
|
|
||||||
//IterateMoveSet(from, (innerPiece, position) =>
|
|
||||||
//{
|
|
||||||
// if (innerPiece?.Owner != InCheck) positionsToCheck.Add(position); // Find possible moves...
|
|
||||||
//});
|
|
||||||
|
|
||||||
// And evaluate if any move gets the player out of check.
|
|
||||||
foreach (var position in positionsToCheck)
|
|
||||||
{
|
|
||||||
if (validationBoard == null) validationBoard = new ShogiBoard(this);
|
|
||||||
var moveSuccess = validationBoard.TryMove(new Move { From = from, To = position });
|
|
||||||
if (moveSuccess)
|
|
||||||
{
|
|
||||||
isCheckmate &= validationBoard.EvaluateCheck(InCheck.Value);
|
|
||||||
validationBoard = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return isCheckmate;
|
|
||||||
}
|
|
||||||
/// <returns>True if the move was successful.</returns>
|
/// <returns>True if the move was successful.</returns>
|
||||||
private bool PlaceFromHand(Move move)
|
private bool PlaceFromHand(Move move)
|
||||||
{
|
{
|
||||||
@@ -186,7 +162,7 @@ namespace Gameboard.ShogiUI.BoardState
|
|||||||
var fromPiece = Board[move.From.X, move.From.Y];
|
var fromPiece = Board[move.From.X, move.From.Y];
|
||||||
if (fromPiece == null) return false; // Invalid move
|
if (fromPiece == null) return false; // Invalid move
|
||||||
if (fromPiece.Owner != WhoseTurn) return false; // Invalid move; cannot move other players pieces.
|
if (fromPiece.Owner != WhoseTurn) return false; // Invalid move; cannot move other players pieces.
|
||||||
if (IsPathable(move.From, move.To, fromPiece) == false) return false; // Invalid move; move not part of move-set.
|
if (IsPathable(move.From, move.To) == false) return false; // Invalid move; move not part of move-set.
|
||||||
|
|
||||||
var captured = Board[move.To.X, move.To.Y];
|
var captured = Board[move.To.X, move.To.Y];
|
||||||
if (captured != null)
|
if (captured != null)
|
||||||
@@ -227,10 +203,13 @@ namespace Gameboard.ShogiUI.BoardState
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsPathable(Vector2 from, Vector2 to, Piece piece)
|
private bool IsPathable(Vector2 from, Vector2 to)
|
||||||
{
|
{
|
||||||
|
var piece = Board[from.X, from.Y];
|
||||||
|
if (piece == null) return false;
|
||||||
|
|
||||||
var isObstructed = false;
|
var isObstructed = false;
|
||||||
var isPathable = pathFinder.PathTo(piece, from, to, (other, position) =>
|
var isPathable = pathFinder.PathTo(from, to, (other, position) =>
|
||||||
{
|
{
|
||||||
if (other.Owner == piece.Owner) isObstructed = true;
|
if (other.Owner == piece.Owner) isObstructed = true;
|
||||||
});
|
});
|
||||||
@@ -272,39 +251,92 @@ namespace Gameboard.ShogiUI.BoardState
|
|||||||
Console.WriteLine(builder.ToString());
|
Console.WriteLine(builder.ToString());
|
||||||
}
|
}
|
||||||
#region Rules Validation
|
#region Rules Validation
|
||||||
/// <summary>
|
private bool EvaluateCheckAfterMove(WhichPlayer whichPlayer)
|
||||||
/// Evaluate if a player is in check given the current board state.
|
|
||||||
/// </summary>
|
|
||||||
private bool EvaluateCheck(WhichPlayer whichPlayer)
|
|
||||||
{
|
{
|
||||||
var destination = whichPlayer == WhichPlayer.Player1 ? player1King : player2King;
|
var isCheck = false;
|
||||||
var inCheck = false;
|
var kingPosition = whichPlayer == WhichPlayer.Player1 ? player1King : player2King;
|
||||||
// Iterate every board piece...
|
|
||||||
Board.ForEachNotNull((piece, x, y) =>
|
|
||||||
{
|
|
||||||
var origin = new Vector2(x, y);
|
|
||||||
// ...that belongs to the opponent within range...
|
|
||||||
if (piece.Owner != whichPlayer && pathFinder.IsPathable(origin, destination, piece))
|
|
||||||
{
|
|
||||||
pathFinder.PathTo(piece, origin, destination, (threatenedPiece, position) =>
|
|
||||||
{
|
|
||||||
// ...and threatens the player's king.
|
|
||||||
inCheck |=
|
|
||||||
threatenedPiece.WhichPiece == WhichPiece.King
|
|
||||||
&& threatenedPiece.Owner == whichPlayer;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return inCheck;
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Iterate through the possible moves of a piece at a given position.
|
|
||||||
/// </summary>
|
|
||||||
|
|
||||||
/// <summary>
|
// Get last move.
|
||||||
/// Useful for iterating the board for pieces that move many spaces.
|
var move = MoveHistory[^1];
|
||||||
/// </summary>
|
// Check if the move put the king in check.
|
||||||
/// <param name="callback">A function that returns true if walking should continue.</param>
|
if (pathFinder.PathTo(move.To, kingPosition)) return true;
|
||||||
|
|
||||||
|
// Get line equation from king through the now-unoccupied location.
|
||||||
|
var direction = Vector2.Subtract(kingPosition, move.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?
|
||||||
|
pathFinder.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)
|
||||||
|
{
|
||||||
|
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
|
||||||
|
{
|
||||||
|
if (piece.Owner != whichPlayer && piece.WhichPiece == WhichPiece.Bishop)
|
||||||
|
{
|
||||||
|
isCheck = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if (slope == 0)
|
||||||
|
{
|
||||||
|
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
|
||||||
|
{
|
||||||
|
if (piece.Owner != whichPlayer && piece.WhichPiece == WhichPiece.Rook)
|
||||||
|
{
|
||||||
|
isCheck = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return isCheck;
|
||||||
|
}
|
||||||
|
private bool EvaluateCheckmate()
|
||||||
|
{
|
||||||
|
if (!InCheck.HasValue) return false;
|
||||||
|
|
||||||
|
// Assume true and try to disprove.
|
||||||
|
var isCheckmate = true;
|
||||||
|
Board.ForEachNotNull((piece, x, y) => // For each piece...
|
||||||
|
{
|
||||||
|
// Short circuit
|
||||||
|
if (!isCheckmate) return;
|
||||||
|
|
||||||
|
var from = new Vector2(x, y);
|
||||||
|
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) =>
|
||||||
|
{
|
||||||
|
if (validationBoard == null) validationBoard = new ShogiBoard(this);
|
||||||
|
var moveSuccess = validationBoard.TryMove(new Move { From = from, To = position });
|
||||||
|
if (moveSuccess)
|
||||||
|
{
|
||||||
|
isCheckmate = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return isCheckmate;
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Initialize
|
#region Initialize
|
||||||
@@ -362,18 +394,5 @@ namespace Gameboard.ShogiUI.BoardState
|
|||||||
ResetRearRow(WhichPlayer.Player2);
|
ResetRearRow(WhichPlayer.Player2);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
//public static ShogiBoard ConstructWithMoves(IList<Move> moves)
|
|
||||||
//{
|
|
||||||
// var s = new ShogiBoard();
|
|
||||||
// for (var i = 0; i < moves.Count; i++)
|
|
||||||
// {
|
|
||||||
// if (!s.Move(moves[i]))
|
|
||||||
// {
|
|
||||||
// throw new InvalidOperationException($"Unable to construct ShogiBoard with the given move at index {i}.");
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return s;
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -177,7 +177,6 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
|
|||||||
// P1 Bishop puts P2 in check
|
// P1 Bishop puts P2 in check
|
||||||
new Move { From = new Vector2(1, 1), To = new Vector2(6, 6) }
|
new Move { From = new Vector2(1, 1), To = new Vector2(6, 6) }
|
||||||
};
|
};
|
||||||
//var shogi = new ShogiBoard(moves);
|
|
||||||
var shogi = new ShogiBoard(moves);
|
var shogi = new ShogiBoard(moves);
|
||||||
|
|
||||||
// Prerequisit
|
// Prerequisit
|
||||||
|
|||||||
@@ -24,14 +24,14 @@ namespace Gameboard.ShogiUI.UnitTests.PathFinding
|
|||||||
var result = finder.IsPathable(
|
var result = finder.IsPathable(
|
||||||
new Vector2(2, 2),
|
new Vector2(2, 2),
|
||||||
new Vector2(7, 7),
|
new Vector2(7, 7),
|
||||||
new Vector2(3, 3)
|
new Vector2(1, 1)
|
||||||
);
|
);
|
||||||
result.Should().BeTrue();
|
result.Should().BeTrue();
|
||||||
|
|
||||||
result = finder.IsPathable(
|
result = finder.IsPathable(
|
||||||
new Vector2(2, 2),
|
new Vector2(2, 2),
|
||||||
new Vector2(7, 7),
|
new Vector2(7, 7),
|
||||||
new Vector2(2, 2)
|
new Vector2(0, 0)
|
||||||
);
|
);
|
||||||
result.Should().BeFalse();
|
result.Should().BeFalse();
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ namespace PathFinding
|
|||||||
{
|
{
|
||||||
public interface IPlanarCollection<T> : IEnumerable<T>
|
public interface IPlanarCollection<T> : IEnumerable<T>
|
||||||
{
|
{
|
||||||
T this[int x, int y] { get; set; }
|
T this[float x, float y] { get; set; }
|
||||||
int GetLength(int dimension);
|
int GetLength(int dimension);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
|
|
||||||
@@ -24,46 +25,50 @@ namespace PathFinding
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Navigate the collection such that each "step" is always towards the destination.
|
/// Navigate the collection such that each "step" is always towards the destination, respecting the Paths available to the element at origin.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="element">The pathing element.</param>
|
/// <param name="element">The pathing element.</param>
|
||||||
/// <param name="origin">The starting location.</param>
|
/// <param name="origin">The starting location.</param>
|
||||||
/// <param name="destination">The destination.</param>
|
/// <param name="destination">The destination.</param>
|
||||||
/// <param name="callback">Do cool stuff here.</param>
|
/// <param name="callback">Do cool stuff here.</param>
|
||||||
/// <returns>True if the element reached the destination.</returns>
|
/// <returns>True if the element reached the destination.</returns>
|
||||||
public bool PathTo(T element, Vector2 origin, Vector2 destination, Callback callback)
|
public bool PathTo(Vector2 origin, Vector2 destination, Callback callback = null)
|
||||||
{
|
{
|
||||||
if (destination.X > width - 1 || destination.Y > height - 1 || destination.X < 0 || destination.Y < 0)
|
if (destination.X > width - 1 || destination.Y > height - 1 || destination.X < 0 || destination.Y < 0)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
var element = collection[origin.X, origin.Y];
|
||||||
var path = FindDirectionTowardsDestination(element.GetPaths(), origin, destination);
|
var path = FindDirectionTowardsDestination(element.GetPaths(), origin, destination);
|
||||||
var next = Vector2.Add(origin, path.Direction);
|
|
||||||
|
|
||||||
if (!IsPathable(origin, destination, next))
|
if (!IsPathable(origin, destination, path.Direction))
|
||||||
{
|
{
|
||||||
// Assumption: if a single best-choice step towards the destination cannot happen, no pathing can happen.
|
// Assumption: if a single best-choice step towards the destination cannot happen, no pathing can happen.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var shouldPath = true;
|
var shouldPath = true;
|
||||||
while (shouldPath)
|
var next = origin;
|
||||||
|
while (shouldPath && next != destination)
|
||||||
{
|
{
|
||||||
|
next = Vector2.Add(next, path.Direction);
|
||||||
var collider = collection[(int)next.X, (int)next.Y];
|
var collider = collection[(int)next.X, (int)next.Y];
|
||||||
if (collider != null) callback(collider, next);
|
if (collider != null)
|
||||||
if (next == destination) return true;
|
{
|
||||||
if (path.Distance == Distance.OneStep)
|
callback?.Invoke(collider, next);
|
||||||
|
shouldPath = false;
|
||||||
|
}
|
||||||
|
else if (path.Distance == Distance.OneStep)
|
||||||
{
|
{
|
||||||
shouldPath = false;
|
shouldPath = false;
|
||||||
}
|
}
|
||||||
next = Vector2.Add(next, path.Direction);
|
|
||||||
}
|
}
|
||||||
return true;
|
return next == destination;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PathEvery(IPlanarElement element, Vector2 from, Callback callback)
|
public void PathEvery(Vector2 from, Callback callback)
|
||||||
{
|
{
|
||||||
|
var element = collection[from.X, from.Y];
|
||||||
foreach (var path in element.GetPaths())
|
foreach (var path in element.GetPaths())
|
||||||
{
|
{
|
||||||
var shouldPath = true;
|
var shouldPath = true;
|
||||||
@@ -84,6 +89,22 @@ namespace PathFinding
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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 < width && next.Y >= 0 && next.Y < height)
|
||||||
|
{
|
||||||
|
var element = collection[next.X, next.Y];
|
||||||
|
if (element != null) callback(element, next);
|
||||||
|
next = Vector2.Add(next, direction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Path FindDirectionTowardsDestination(ICollection<Path> paths, Vector2 origin, Vector2 destination) =>
|
public Path FindDirectionTowardsDestination(ICollection<Path> paths, Vector2 origin, Vector2 destination) =>
|
||||||
paths.Aggregate((a, b) => Vector2.Distance(destination, Vector2.Add(origin, a.Direction)) < Vector2.Distance(destination, Vector2.Add(origin, b.Direction)) ? a : b);
|
paths.Aggregate((a, b) => Vector2.Distance(destination, Vector2.Add(origin, a.Direction)) < Vector2.Distance(destination, Vector2.Add(origin, b.Direction)) ? a : b);
|
||||||
|
|
||||||
@@ -91,20 +112,26 @@ namespace PathFinding
|
|||||||
public bool IsPathable(Vector2 origin, Vector2 destination, T element)
|
public bool IsPathable(Vector2 origin, Vector2 destination, T element)
|
||||||
{
|
{
|
||||||
var path = FindDirectionTowardsDestination(element.GetPaths(), origin, destination);
|
var path = FindDirectionTowardsDestination(element.GetPaths(), origin, destination);
|
||||||
var next = Vector2.Add(origin, path.Direction);
|
return IsPathable(origin, destination, path.Direction);
|
||||||
return IsPathable(origin, destination, next);
|
|
||||||
}
|
}
|
||||||
public bool IsPathable(Vector2 origin, Vector2 destination, Vector2 next)
|
public bool IsPathable(Vector2 origin, Vector2 destination, Vector2 direction)
|
||||||
{
|
{
|
||||||
if (Vector2.Distance(next, destination) < Vector2.Distance(origin, destination))
|
direction = Vector2.Normalize(direction);
|
||||||
{
|
var next = Vector2.Add(origin, direction);
|
||||||
// y = mx + b
|
if (Vector2.Distance(next, destination) >= Vector2.Distance(origin, destination)) return false;
|
||||||
// b = -mx + y
|
|
||||||
var slope = (destination.Y - origin.Y) / (destination.X - origin.X);
|
var slope = (destination.Y - origin.Y) / (destination.X - origin.X);
|
||||||
var yIntercept = -(slope * origin.X) + origin.Y;
|
if (float.IsInfinity(slope))
|
||||||
return float.IsInfinity(slope) || next.Y == slope * next.X + yIntercept;
|
{
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user