@@ -1,4 +1,4 @@
|
||||
using System.Numerics;
|
||||
using Shogi.Domain.Pieces;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Shogi.Domain
|
||||
@@ -7,7 +7,7 @@ namespace Shogi.Domain
|
||||
// Then validation can occur when assigning a piece to a position.
|
||||
public class ShogiBoardState
|
||||
{
|
||||
private static readonly string BoardNotationRegex = @"(?<file>[a-iA-I])(?<rank>[1-9])";
|
||||
private static readonly string BoardNotationRegex = @"(?<file>[A-I])(?<rank>[1-9])";
|
||||
private static readonly char A = 'A';
|
||||
public delegate void ForEachDelegate(Piece element, Vector2 position);
|
||||
/// <summary>
|
||||
@@ -15,21 +15,33 @@ namespace Shogi.Domain
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, Piece?> board;
|
||||
|
||||
public List<Piece> Hand => WhoseTurn == WhichPlayer.Player1 ? Player1Hand : Player2Hand;
|
||||
public List<Piece> ActivePlayerHand => WhoseTurn == WhichPlayer.Player1 ? Player1Hand : Player2Hand;
|
||||
/// <summary>
|
||||
/// "Active Player" means the player whose turn it is.
|
||||
/// </summary>
|
||||
public Vector2 ActivePlayerKingPosition => WhoseTurn == WhichPlayer.Player1 ? Player1KingPosition : Player2KingPosition;
|
||||
/// <summary>
|
||||
/// "Opposing Player" means the player whose turn it isn't.
|
||||
/// </summary>
|
||||
public Vector2 OpposingPlayerKingPosition => WhoseTurn == WhichPlayer.Player1 ? Player2KingPosition : Player1KingPosition;
|
||||
public Vector2 Player1KingPosition { get; set; }
|
||||
public Vector2 Player2KingPosition { get; set; }
|
||||
public List<Piece> Player1Hand { get; }
|
||||
public List<Piece> Player2Hand { get; }
|
||||
public List<Move> MoveHistory { get; }
|
||||
public Vector2 PreviousMoveFrom { get; private set; }
|
||||
public Vector2 PreviousMoveTo { get; private set; }
|
||||
public WhichPlayer WhoseTurn { get; set; }
|
||||
public WhichPlayer? InCheck { get; set; }
|
||||
public bool IsCheckmate { get; set; }
|
||||
|
||||
public ShogiBoardState()
|
||||
{
|
||||
board = new Dictionary<string, Piece?>(81);
|
||||
board = new Dictionary<string, Piece?>(81, StringComparer.OrdinalIgnoreCase);
|
||||
InitializeBoardState();
|
||||
Player1Hand = new List<Piece>();
|
||||
Player2Hand = new List<Piece>();
|
||||
MoveHistory = new List<Move>();
|
||||
PreviousMoveTo = Vector2.Zero;
|
||||
CacheKingPositions();
|
||||
}
|
||||
|
||||
|
||||
@@ -40,21 +52,39 @@ namespace Shogi.Domain
|
||||
{
|
||||
foreach (var kvp in other.board)
|
||||
{
|
||||
board[kvp.Key] = kvp.Value == null ? null : new Piece(kvp.Value);
|
||||
// Replace copy constructor with static factory method in Piece.cs
|
||||
board[kvp.Key] = kvp.Value == null ? null : Piece.Create(kvp.Value);
|
||||
}
|
||||
WhoseTurn = other.WhoseTurn;
|
||||
InCheck = other.InCheck;
|
||||
IsCheckmate = other.IsCheckmate;
|
||||
MoveHistory.AddRange(other.MoveHistory);
|
||||
PreviousMoveTo = other.PreviousMoveTo;
|
||||
Player1Hand.AddRange(other.Player1Hand);
|
||||
Player2Hand.AddRange(other.Player2Hand);
|
||||
Player1KingPosition = other.Player1KingPosition;
|
||||
Player2KingPosition = other.Player2KingPosition;
|
||||
}
|
||||
|
||||
public Piece? this[string notation]
|
||||
{
|
||||
// TODO: Validate "notation" here and throw an exception if invalid.
|
||||
get => board[notation.ToUpper()];
|
||||
set => board[notation.ToUpper()] = value;
|
||||
get => board[notation];
|
||||
set
|
||||
{
|
||||
if (value?.WhichPiece == WhichPiece.King)
|
||||
{
|
||||
if (value.Owner == WhichPlayer.Player1)
|
||||
{
|
||||
// TODO: This FromBoardNotation() is a waste if the Vector2 indexer was called. :(
|
||||
Player1KingPosition = FromBoardNotation(notation);
|
||||
}
|
||||
else if (value.Owner == WhichPlayer.Player2)
|
||||
{
|
||||
Player2KingPosition = FromBoardNotation(notation);
|
||||
}
|
||||
}
|
||||
board[notation] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public Piece? this[Vector2 vector]
|
||||
@@ -69,6 +99,22 @@ namespace Shogi.Domain
|
||||
set => this[ToBoardNotation(x, y)] = value;
|
||||
}
|
||||
|
||||
internal void RememberAsMostRecentMove(Vector2 from, Vector2 to)
|
||||
{
|
||||
PreviousMoveFrom = from;
|
||||
PreviousMoveTo = to;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the given path can be traversed without colliding into a piece.
|
||||
/// </summary>
|
||||
public bool IsPathBlocked(IEnumerable<Vector2> path)
|
||||
{
|
||||
return !path.Any()
|
||||
|| path.SkipLast(1).Any(position => this[position] != null)
|
||||
|| this[path.Last()]?.Owner == WhoseTurn;
|
||||
}
|
||||
|
||||
public void ForEachNotNull(ForEachDelegate callback)
|
||||
{
|
||||
for (var x = 0; x < 9; x++)
|
||||
@@ -83,11 +129,57 @@ namespace Shogi.Domain
|
||||
}
|
||||
}
|
||||
|
||||
internal bool IsWithinPromotionZone(Vector2 position)
|
||||
{
|
||||
return (WhoseTurn == WhichPlayer.Player1 && position.Y > 5)
|
||||
|| (WhoseTurn == WhichPlayer.Player2 && position.Y < 3);
|
||||
}
|
||||
|
||||
internal static bool IsWithinBoardBoundary(Vector2 position)
|
||||
{
|
||||
return position.X <= 8 && position.X >= 0
|
||||
&& position.Y <= 8 && position.Y >= 0;
|
||||
}
|
||||
|
||||
internal IEnumerable<BoardTile> GetTilesOccupiedBy(WhichPlayer whichPlayer) => board
|
||||
.Where(kvp => kvp.Value?.Owner == whichPlayer)
|
||||
.Select(kvp => new BoardTile(kvp.Value!, FromBoardNotation(kvp.Key)));
|
||||
|
||||
internal void Capture(Vector2 to)
|
||||
{
|
||||
var piece = this[to];
|
||||
if (piece == null) throw new InvalidOperationException("Cannot capture. Piece at position does not exist.");
|
||||
|
||||
piece.Capture(WhoseTurn);
|
||||
ActivePlayerHand.Add(piece);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Does not include the start position.
|
||||
/// </summary>
|
||||
internal static IEnumerable<Vector2> GetPathAlongDirectionFromStartToEdgeOfBoard(Vector2 start, Vector2 direction)
|
||||
{
|
||||
var next = start;
|
||||
while (IsWithinBoardBoundary(next + direction))
|
||||
{
|
||||
next += direction;
|
||||
yield return next;
|
||||
}
|
||||
}
|
||||
|
||||
internal Piece? GetFirstPieceAlongPath(IEnumerable<Vector2> path)
|
||||
{
|
||||
foreach (var step in path)
|
||||
{
|
||||
if (this[step] != null) return this[step];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static string ToBoardNotation(Vector2 vector)
|
||||
{
|
||||
return ToBoardNotation((int)vector.X, (int)vector.Y);
|
||||
}
|
||||
public static string ToBoardNotation(int x, int y)
|
||||
private static string ToBoardNotation(int x, int y)
|
||||
{
|
||||
var file = (char)(x + A);
|
||||
var rank = y + 1;
|
||||
@@ -95,10 +187,9 @@ namespace Shogi.Domain
|
||||
}
|
||||
public static Vector2 FromBoardNotation(string notation)
|
||||
{
|
||||
notation = notation.ToUpper();
|
||||
if (Regex.IsMatch(notation, BoardNotationRegex))
|
||||
{
|
||||
var match = Regex.Match(notation, BoardNotationRegex);
|
||||
var match = Regex.Match(notation, BoardNotationRegex, RegexOptions.IgnoreCase);
|
||||
char file = match.Groups["file"].Value[0];
|
||||
int rank = int.Parse(match.Groups["rank"].Value);
|
||||
return new Vector2(file - A, rank - 1);
|
||||
@@ -106,37 +197,55 @@ namespace Shogi.Domain
|
||||
throw new ArgumentException($"Board notation not recognized. Notation given: {notation}");
|
||||
}
|
||||
|
||||
private void CacheKingPositions()
|
||||
{
|
||||
ForEachNotNull((tile, position) =>
|
||||
{
|
||||
if (tile.WhichPiece == WhichPiece.King)
|
||||
{
|
||||
if (tile.Owner == WhichPlayer.Player1)
|
||||
{
|
||||
Player1KingPosition = position;
|
||||
}
|
||||
else if (tile.Owner == WhichPlayer.Player2)
|
||||
{
|
||||
Player2KingPosition = position;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void InitializeBoardState()
|
||||
{
|
||||
this["A1"] = new Piece(WhichPiece.Lance, WhichPlayer.Player1);
|
||||
this["B1"] = new Piece(WhichPiece.Knight, WhichPlayer.Player1);
|
||||
this["C1"] = new Piece(WhichPiece.SilverGeneral, WhichPlayer.Player1);
|
||||
this["D1"] = new Piece(WhichPiece.GoldGeneral, WhichPlayer.Player1);
|
||||
this["E1"] = new Piece(WhichPiece.King, WhichPlayer.Player1);
|
||||
this["F1"] = new Piece(WhichPiece.GoldGeneral, WhichPlayer.Player1);
|
||||
this["G1"] = new Piece(WhichPiece.SilverGeneral, WhichPlayer.Player1);
|
||||
this["H1"] = new Piece(WhichPiece.Knight, WhichPlayer.Player1);
|
||||
this["I1"] = new Piece(WhichPiece.Lance, WhichPlayer.Player1);
|
||||
this["A1"] = new Lance(WhichPlayer.Player1);
|
||||
this["B1"] = new Knight(WhichPlayer.Player1);
|
||||
this["C1"] = new SilverGeneral(WhichPlayer.Player1);
|
||||
this["D1"] = new GoldGeneral(WhichPlayer.Player1);
|
||||
this["E1"] = new King(WhichPlayer.Player1);
|
||||
this["F1"] = new GoldGeneral(WhichPlayer.Player1);
|
||||
this["G1"] = new SilverGeneral(WhichPlayer.Player1);
|
||||
this["H1"] = new Knight(WhichPlayer.Player1);
|
||||
this["I1"] = new Lance(WhichPlayer.Player1);
|
||||
|
||||
this["A2"] = null;
|
||||
this["B2"] = new Piece(WhichPiece.Bishop, WhichPlayer.Player1);
|
||||
this["B2"] = new Bishop(WhichPlayer.Player1);
|
||||
this["C2"] = null;
|
||||
this["D2"] = null;
|
||||
this["E2"] = null;
|
||||
this["F2"] = null;
|
||||
this["G2"] = null;
|
||||
this["H2"] = new Piece(WhichPiece.Rook, WhichPlayer.Player1);
|
||||
this["H2"] = new Rook(WhichPlayer.Player1);
|
||||
this["I2"] = null;
|
||||
|
||||
this["A3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
|
||||
this["B3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
|
||||
this["C3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
|
||||
this["D3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
|
||||
this["E3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
|
||||
this["F3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
|
||||
this["G3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
|
||||
this["H3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
|
||||
this["I3"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player1);
|
||||
this["A3"] = new Pawn(WhichPlayer.Player1);
|
||||
this["B3"] = new Pawn(WhichPlayer.Player1);
|
||||
this["C3"] = new Pawn(WhichPlayer.Player1);
|
||||
this["D3"] = new Pawn(WhichPlayer.Player1);
|
||||
this["E3"] = new Pawn(WhichPlayer.Player1);
|
||||
this["F3"] = new Pawn(WhichPlayer.Player1);
|
||||
this["G3"] = new Pawn(WhichPlayer.Player1);
|
||||
this["H3"] = new Pawn(WhichPlayer.Player1);
|
||||
this["I3"] = new Pawn(WhichPlayer.Player1);
|
||||
|
||||
this["A4"] = null;
|
||||
this["B4"] = null;
|
||||
@@ -168,35 +277,35 @@ namespace Shogi.Domain
|
||||
this["H6"] = null;
|
||||
this["I6"] = null;
|
||||
|
||||
this["A7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
|
||||
this["B7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
|
||||
this["C7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
|
||||
this["D7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
|
||||
this["E7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
|
||||
this["F7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
|
||||
this["G7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
|
||||
this["H7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
|
||||
this["I7"] = new Piece(WhichPiece.Pawn, WhichPlayer.Player2);
|
||||
this["A7"] = new Pawn(WhichPlayer.Player2);
|
||||
this["B7"] = new Pawn(WhichPlayer.Player2);
|
||||
this["C7"] = new Pawn(WhichPlayer.Player2);
|
||||
this["D7"] = new Pawn(WhichPlayer.Player2);
|
||||
this["E7"] = new Pawn(WhichPlayer.Player2);
|
||||
this["F7"] = new Pawn(WhichPlayer.Player2);
|
||||
this["G7"] = new Pawn(WhichPlayer.Player2);
|
||||
this["H7"] = new Pawn(WhichPlayer.Player2);
|
||||
this["I7"] = new Pawn(WhichPlayer.Player2);
|
||||
|
||||
this["A8"] = null;
|
||||
this["B8"] = new Piece(WhichPiece.Rook, WhichPlayer.Player2);
|
||||
this["B8"] = new Rook(WhichPlayer.Player2);
|
||||
this["C8"] = null;
|
||||
this["D8"] = null;
|
||||
this["E8"] = null;
|
||||
this["F8"] = null;
|
||||
this["G8"] = null;
|
||||
this["H8"] = new Piece(WhichPiece.Bishop, WhichPlayer.Player2);
|
||||
this["H8"] = new Bishop(WhichPlayer.Player2);
|
||||
this["I8"] = null;
|
||||
|
||||
this["A9"] = new Piece(WhichPiece.Lance, WhichPlayer.Player2);
|
||||
this["B9"] = new Piece(WhichPiece.Knight, WhichPlayer.Player2);
|
||||
this["C9"] = new Piece(WhichPiece.SilverGeneral, WhichPlayer.Player2);
|
||||
this["D9"] = new Piece(WhichPiece.GoldGeneral, WhichPlayer.Player2);
|
||||
this["E9"] = new Piece(WhichPiece.King, WhichPlayer.Player2);
|
||||
this["F9"] = new Piece(WhichPiece.GoldGeneral, WhichPlayer.Player2);
|
||||
this["G9"] = new Piece(WhichPiece.SilverGeneral, WhichPlayer.Player2);
|
||||
this["H9"] = new Piece(WhichPiece.Knight, WhichPlayer.Player2);
|
||||
this["I9"] = new Piece(WhichPiece.Lance, WhichPlayer.Player2);
|
||||
this["A9"] = new Lance(WhichPlayer.Player2);
|
||||
this["B9"] = new Knight(WhichPlayer.Player2);
|
||||
this["C9"] = new SilverGeneral(WhichPlayer.Player2);
|
||||
this["D9"] = new GoldGeneral(WhichPlayer.Player2);
|
||||
this["E9"] = new King(WhichPlayer.Player2);
|
||||
this["F9"] = new GoldGeneral(WhichPlayer.Player2);
|
||||
this["G9"] = new SilverGeneral(WhichPlayer.Player2);
|
||||
this["H9"] = new Knight(WhichPlayer.Player2);
|
||||
this["I9"] = new Lance(WhichPlayer.Player2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user