UTests pass with Pathfinder2D
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using Gameboard.ShogiUI.BoardState.Pieces;
|
||||
using PathFinding;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
@@ -16,6 +18,7 @@ namespace Gameboard.ShogiUI.BoardState
|
||||
private ShogiBoard validationBoard;
|
||||
private Vector2 player1King;
|
||||
private Vector2 player2King;
|
||||
private PathFinder2D<Piece> pathFinder;
|
||||
public IReadOnlyDictionary<WhichPlayer, List<Piece>> Hands { get; }
|
||||
public Array2D<Piece> Board { get; }
|
||||
public List<Move> MoveHistory { get; }
|
||||
@@ -31,17 +34,51 @@ namespace Gameboard.ShogiUI.BoardState
|
||||
{ WhichPlayer.Player1, new List<Piece>()},
|
||||
{ WhichPlayer.Player2, new List<Piece>()},
|
||||
};
|
||||
pathFinder = new PathFinder2D<Piece>(Board);
|
||||
InitializeBoardState();
|
||||
player1King = new Vector2(4, 0);
|
||||
player2King = new Vector2(4, 8);
|
||||
}
|
||||
|
||||
public ShogiBoard(IList<Move> moves) : this()
|
||||
{
|
||||
for (var i = 0; i < moves.Count; i++)
|
||||
{
|
||||
if (!Move(moves[i]))
|
||||
{
|
||||
throw new InvalidOperationException($"Unable to construct ShogiBoard with the given move at index {i}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ShogiBoard(ShogiBoard toCopy)
|
||||
{
|
||||
Board = new Array2D<Piece>(9, 9);
|
||||
for (var x = 0; x < 9; x++)
|
||||
for (var y = 0; y < 9; y++)
|
||||
Board[x, y] = toCopy.Board[x, y]?.DeepClone();
|
||||
|
||||
pathFinder = new PathFinder2D<Piece>(Board);
|
||||
MoveHistory = new List<Move>(toCopy.MoveHistory);
|
||||
Hands = new Dictionary<WhichPlayer, List<Piece>>
|
||||
{
|
||||
{ WhichPlayer.Player1, new List<Piece>(toCopy.Hands[WhichPlayer.Player1]) },
|
||||
{ WhichPlayer.Player2, new List<Piece>(toCopy.Hands[WhichPlayer.Player2]) }
|
||||
};
|
||||
player1King = toCopy.player1King;
|
||||
player2King = toCopy.player2King;
|
||||
}
|
||||
|
||||
public bool Move(Move move)
|
||||
{
|
||||
|
||||
var otherPlayer = WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
||||
var moveSuccess = TryMove(move);
|
||||
|
||||
if (!moveSuccess) return false;
|
||||
if (!moveSuccess)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Evaluate check
|
||||
InCheck = EvaluateCheck(otherPlayer) ? otherPlayer : null;
|
||||
@@ -59,8 +96,9 @@ namespace Gameboard.ShogiUI.BoardState
|
||||
// Try making the move in a "throw away" board.
|
||||
if (validationBoard == null)
|
||||
{
|
||||
validationBoard = ConstructWithMoves(MoveHistory);
|
||||
validationBoard = new ShogiBoard(this);
|
||||
}
|
||||
|
||||
var isValid = move.PieceFromCaptured.HasValue
|
||||
? validationBoard.PlaceFromHand(move)
|
||||
: validationBoard.PlaceFromBoard(move);
|
||||
@@ -92,15 +130,15 @@ namespace Gameboard.ShogiUI.BoardState
|
||||
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...
|
||||
});
|
||||
//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 = ConstructWithMoves(MoveHistory);
|
||||
if (validationBoard == null) validationBoard = new ShogiBoard(this);
|
||||
var moveSuccess = validationBoard.TryMove(new Move { From = from, To = position });
|
||||
if (moveSuccess)
|
||||
{
|
||||
@@ -148,7 +186,7 @@ namespace Gameboard.ShogiUI.BoardState
|
||||
var fromPiece = Board[move.From.X, move.From.Y];
|
||||
if (fromPiece == null) return false; // Invalid move
|
||||
if (fromPiece.Owner != WhoseTurn) return false; // Invalid move; cannot move other players pieces.
|
||||
if (ValidateMoveAgainstMoveSet(move.From, move.To) == false) return false; // Invalid move; move not part of move-set.
|
||||
if (IsPathable(move.From, move.To, fromPiece) == false) return false; // Invalid move; move not part of move-set.
|
||||
|
||||
var captured = Board[move.To.X, move.To.Y];
|
||||
if (captured != null)
|
||||
@@ -188,6 +226,17 @@ namespace Gameboard.ShogiUI.BoardState
|
||||
MoveHistory.Add(move);
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool IsPathable(Vector2 from, Vector2 to, Piece piece)
|
||||
{
|
||||
var isObstructed = false;
|
||||
var isPathable = pathFinder.PathTo(piece, from, to, (other, position) =>
|
||||
{
|
||||
if (other.Owner == piece.Owner) isObstructed = true;
|
||||
});
|
||||
return !isObstructed && isPathable;
|
||||
}
|
||||
|
||||
public void PrintStateAsAscii()
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
@@ -228,219 +277,34 @@ namespace Gameboard.ShogiUI.BoardState
|
||||
/// </summary>
|
||||
private bool EvaluateCheck(WhichPlayer whichPlayer)
|
||||
{
|
||||
var kingPosition = whichPlayer == WhichPlayer.Player1 ? player1King : player2King;
|
||||
var destination = whichPlayer == WhichPlayer.Player1 ? player1King : player2King;
|
||||
var inCheck = false;
|
||||
// Iterate every board piece...
|
||||
Board.ForEachNotNull((piece, x, y) =>
|
||||
{
|
||||
var v = new Vector2(x, y);
|
||||
var origin = new Vector2(x, y);
|
||||
// ...that belongs to the opponent within range...
|
||||
if (piece.Owner != whichPlayer && (piece.IsRanged || Vector2.Distance(kingPosition, v) < 3))
|
||||
if (piece.Owner != whichPlayer && pathFinder.IsPathable(origin, destination, piece))
|
||||
{
|
||||
IterateMoveSet(new Vector2(x, y), (threatenedPiece, position) =>
|
||||
pathFinder.PathTo(piece, origin, destination, (threatenedPiece, position) =>
|
||||
{
|
||||
// ...and threatens the player's king.
|
||||
inCheck |=
|
||||
threatenedPiece?.WhichPiece == WhichPiece.King
|
||||
&& threatenedPiece?.Owner == whichPlayer;
|
||||
threatenedPiece.WhichPiece == WhichPiece.King
|
||||
&& threatenedPiece.Owner == whichPlayer;
|
||||
});
|
||||
}
|
||||
});
|
||||
return inCheck;
|
||||
}
|
||||
private bool ValidateMoveAgainstMoveSet(Vector2 from, Vector2 to)
|
||||
{
|
||||
var isValid = false;
|
||||
IterateMoveSet(from, (piece, position) =>
|
||||
{
|
||||
if (piece?.Owner != WhoseTurn && position == to)
|
||||
{
|
||||
isValid = true;
|
||||
}
|
||||
});
|
||||
|
||||
return isValid;
|
||||
}
|
||||
/// <summary>
|
||||
/// Iterate through the possible moves of a piece at a given position.
|
||||
/// </summary>
|
||||
private void IterateMoveSet(Vector2 from, MoveSetCallback callback)
|
||||
{
|
||||
// TODO: Make these are of the move To, so only possible moves towards the move To are iterated.
|
||||
// Maybe separate functions? Sometimes I need to iterate the whole move-set, sometimes I need to iterate only the move-set towards the move To.
|
||||
var piece = Board[from.X, from.Y];
|
||||
switch (piece?.WhichPiece)
|
||||
{
|
||||
case WhichPiece.King:
|
||||
IterateKingMoveSet(from, callback);
|
||||
break;
|
||||
case WhichPiece.GoldenGeneral:
|
||||
IterateGoldenGeneralMoveSet(from, callback);
|
||||
break;
|
||||
case WhichPiece.SilverGeneral:
|
||||
IterateSilverGeneralMoveSet(from, callback);
|
||||
break;
|
||||
case WhichPiece.Bishop:
|
||||
IterateBishopMoveSet(from, callback);
|
||||
break;
|
||||
case WhichPiece.Rook:
|
||||
IterateRookMoveSet(from, callback);
|
||||
break;
|
||||
case WhichPiece.Knight:
|
||||
IterateKnightMoveSet(from, callback);
|
||||
break;
|
||||
case WhichPiece.Lance:
|
||||
IterateLanceMoveSet(from, callback);
|
||||
break;
|
||||
case WhichPiece.Pawn:
|
||||
IteratePawnMoveSet(from, callback);
|
||||
break;
|
||||
}
|
||||
}
|
||||
private void IterateKingMoveSet(Vector2 from, MoveSetCallback callback)
|
||||
{
|
||||
var piece = Board[from.X, from.Y];
|
||||
var direction = new Direction(piece.Owner);
|
||||
BoardStep(from, direction.Up, callback);
|
||||
BoardStep(from, direction.UpLeft, callback);
|
||||
BoardStep(from, direction.UpRight, callback);
|
||||
BoardStep(from, direction.Down, callback);
|
||||
BoardStep(from, direction.DownLeft, callback);
|
||||
BoardStep(from, direction.DownRight, callback);
|
||||
BoardStep(from, direction.Left, callback);
|
||||
BoardStep(from, direction.Right, callback);
|
||||
}
|
||||
private void IterateGoldenGeneralMoveSet(Vector2 from, MoveSetCallback callback)
|
||||
{
|
||||
var piece = Board[from.X, from.Y];
|
||||
var direction = new Direction(piece.Owner);
|
||||
BoardStep(from, direction.Up, callback);
|
||||
BoardStep(from, direction.UpLeft, callback);
|
||||
BoardStep(from, direction.UpRight, callback);
|
||||
BoardStep(from, direction.Down, callback);
|
||||
BoardStep(from, direction.Left, callback);
|
||||
BoardStep(from, direction.Right, callback);
|
||||
}
|
||||
private void IterateSilverGeneralMoveSet(Vector2 from, MoveSetCallback callback)
|
||||
{
|
||||
var piece = Board[from.X, from.Y];
|
||||
var direction = new Direction(piece.Owner);
|
||||
if (piece.IsPromoted)
|
||||
{
|
||||
IterateGoldenGeneralMoveSet(from, callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
BoardStep(from, direction.Up, callback);
|
||||
BoardStep(from, direction.UpLeft, callback);
|
||||
BoardStep(from, direction.UpRight, callback);
|
||||
BoardStep(from, direction.DownLeft, callback);
|
||||
BoardStep(from, direction.DownRight, callback);
|
||||
}
|
||||
}
|
||||
private void IterateBishopMoveSet(Vector2 from, MoveSetCallback callback)
|
||||
{
|
||||
var piece = Board[from.X, from.Y];
|
||||
var direction = new Direction(piece.Owner);
|
||||
BoardWalk(from, direction.UpLeft, callback);
|
||||
BoardWalk(from, direction.UpRight, callback);
|
||||
BoardWalk(from, direction.DownLeft, callback);
|
||||
BoardWalk(from, direction.DownRight, callback);
|
||||
if (piece.IsPromoted)
|
||||
{
|
||||
BoardStep(from, direction.Up, callback);
|
||||
BoardStep(from, direction.Left, callback);
|
||||
BoardStep(from, direction.Right, callback);
|
||||
BoardStep(from, direction.Down, callback);
|
||||
}
|
||||
}
|
||||
private void IterateRookMoveSet(Vector2 from, MoveSetCallback callback)
|
||||
{
|
||||
var piece = Board[from.X, from.Y];
|
||||
var direction = new Direction(piece.Owner);
|
||||
BoardWalk(from, direction.Up, callback);
|
||||
BoardWalk(from, direction.Left, callback);
|
||||
BoardWalk(from, direction.Right, callback);
|
||||
BoardWalk(from, direction.Down, callback);
|
||||
if (piece.IsPromoted)
|
||||
{
|
||||
BoardStep(from, direction.UpLeft, callback);
|
||||
BoardStep(from, direction.UpRight, callback);
|
||||
BoardStep(from, direction.DownLeft, callback);
|
||||
BoardStep(from, direction.DownRight, callback);
|
||||
}
|
||||
}
|
||||
private void IterateKnightMoveSet(Vector2 from, MoveSetCallback callback)
|
||||
{
|
||||
var piece = Board[from.X, from.Y];
|
||||
if (piece.IsPromoted)
|
||||
{
|
||||
IterateGoldenGeneralMoveSet(from, callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
var direction = new Direction(piece.Owner);
|
||||
BoardStep(from, direction.KnightLeft, callback);
|
||||
BoardStep(from, direction.KnightRight, callback);
|
||||
}
|
||||
}
|
||||
private void IterateLanceMoveSet(Vector2 from, MoveSetCallback callback)
|
||||
{
|
||||
var piece = Board[from.X, from.Y];
|
||||
if (piece.IsPromoted)
|
||||
{
|
||||
IterateGoldenGeneralMoveSet(from, callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
var direction = new Direction(piece.Owner);
|
||||
BoardWalk(from, direction.Up, callback);
|
||||
}
|
||||
}
|
||||
private void IteratePawnMoveSet(Vector2 from, MoveSetCallback callback)
|
||||
{
|
||||
var piece = Board[from.X, from.Y];
|
||||
if (piece?.WhichPiece == WhichPiece.Pawn)
|
||||
{
|
||||
if (piece.IsPromoted)
|
||||
{
|
||||
IterateGoldenGeneralMoveSet(from, callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
var direction = new Direction(piece.Owner);
|
||||
BoardStep(from, direction.Up, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Useful for iterating the board for pieces that move many spaces.
|
||||
/// </summary>
|
||||
/// <param name="callback">A function that returns true if walking should continue.</param>
|
||||
private void BoardWalk(Vector2 from, Vector2 direction, MoveSetCallback callback)
|
||||
{
|
||||
var foundAnotherPiece = false;
|
||||
var to = Vector2.Add(from, direction);
|
||||
while (to.X >= 0 && to.X < 9 && to.Y >= 0 && to.Y < 9 && !foundAnotherPiece)
|
||||
{
|
||||
var piece = Board[to.X, to.Y];
|
||||
callback(piece, to);
|
||||
to = Vector2.Add(to, direction);
|
||||
foundAnotherPiece = piece != null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Useful for iterating the board for pieces that move only one space.
|
||||
/// </summary>
|
||||
private void BoardStep(Vector2 from, Vector2 direction, MoveSetCallback callback)
|
||||
{
|
||||
var to = Vector2.Add(from, direction);
|
||||
if (to.X >= 0 && to.X < 9 && to.Y >= 0 && to.Y < 9)
|
||||
{
|
||||
callback(Board[to.X, to.Y], to);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Initialize
|
||||
@@ -453,7 +317,7 @@ namespace Gameboard.ShogiUI.BoardState
|
||||
private void ResetFrontRow(WhichPlayer player)
|
||||
{
|
||||
int y = player == WhichPlayer.Player1 ? 2 : 6;
|
||||
for (int x = 0; x < 9; x++) Board[x, y] = new Piece(WhichPiece.Pawn, player);
|
||||
for (int x = 0; x < 9; x++) Board[x, y] = new Pawn(player);
|
||||
}
|
||||
private void ResetMiddleRow(WhichPlayer player)
|
||||
{
|
||||
@@ -464,28 +328,28 @@ namespace Gameboard.ShogiUI.BoardState
|
||||
Board[8, y] = null;
|
||||
if (player == WhichPlayer.Player1)
|
||||
{
|
||||
Board[1, y] = new Piece(WhichPiece.Bishop, player);
|
||||
Board[7, y] = new Piece(WhichPiece.Rook, player);
|
||||
Board[1, y] = new Bishop(player);
|
||||
Board[7, y] = new Rook(player);
|
||||
}
|
||||
else
|
||||
{
|
||||
Board[1, y] = new Piece(WhichPiece.Rook, player);
|
||||
Board[7, y] = new Piece(WhichPiece.Bishop, player);
|
||||
Board[1, y] = new Rook(player);
|
||||
Board[7, y] = new Bishop(player);
|
||||
}
|
||||
}
|
||||
private void ResetRearRow(WhichPlayer player)
|
||||
{
|
||||
int y = player == WhichPlayer.Player1 ? 0 : 8;
|
||||
|
||||
Board[0, y] = new Piece(WhichPiece.Lance, player);
|
||||
Board[1, y] = new Piece(WhichPiece.Knight, player);
|
||||
Board[2, y] = new Piece(WhichPiece.SilverGeneral, player);
|
||||
Board[3, y] = new Piece(WhichPiece.GoldenGeneral, player);
|
||||
Board[4, y] = new Piece(WhichPiece.King, player);
|
||||
Board[5, y] = new Piece(WhichPiece.GoldenGeneral, player);
|
||||
Board[6, y] = new Piece(WhichPiece.SilverGeneral, player);
|
||||
Board[7, y] = new Piece(WhichPiece.Knight, player);
|
||||
Board[8, y] = new Piece(WhichPiece.Lance, player);
|
||||
Board[0, y] = new Lance(player);
|
||||
Board[1, y] = new Knight(player);
|
||||
Board[2, y] = new SilverGeneral(player);
|
||||
Board[3, y] = new GoldenGeneral(player);
|
||||
Board[4, y] = new King(player);
|
||||
Board[5, y] = new GoldenGeneral(player);
|
||||
Board[6, y] = new SilverGeneral(player);
|
||||
Board[7, y] = new Knight(player);
|
||||
Board[8, y] = new Lance(player);
|
||||
}
|
||||
private void InitializeBoardState()
|
||||
{
|
||||
@@ -499,17 +363,17 @@ namespace Gameboard.ShogiUI.BoardState
|
||||
}
|
||||
#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;
|
||||
}
|
||||
//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;
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user