checkpoint

This commit is contained in:
2022-06-07 20:29:04 -05:00
parent b3e0d154bd
commit 3a9a627e0d
8 changed files with 105 additions and 166 deletions

View File

@@ -1,5 +1,6 @@
using Shogi.Domain.Pieces;
using System.Text.RegularExpressions;
using BoardTile = System.Collections.Generic.KeyValuePair<System.Numerics.Vector2, Shogi.Domain.Pieces.Piece>;
namespace Shogi.Domain
{
@@ -15,25 +16,6 @@ namespace Shogi.Domain
/// </summary>
private readonly Dictionary<string, Piece?> board;
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 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, StringComparer.OrdinalIgnoreCase);
@@ -41,9 +23,26 @@ namespace Shogi.Domain
Player1Hand = new List<Piece>();
Player2Hand = new List<Piece>();
PreviousMoveTo = Vector2.Zero;
CacheKingPositions();
}
public List<Piece> ActivePlayerHand => WhoseTurn == WhichPlayer.Player1 ? Player1Hand : Player2Hand;
public Vector2 Player1KingPosition => FromBoardNotation(this.board.Where(kvp => kvp.Value != null).Single(kvp =>
{
var piece = kvp.Value;
return piece!.IsKing() && piece!.Owner == WhichPlayer.Player1;
}).Key);
public Vector2 Player2KingPosition => FromBoardNotation(this.board.Where(kvp => kvp.Value != null).Single(kvp =>
{
var piece = kvp.Value;
return piece!.IsKing() && piece!.Owner == WhichPlayer.Player2;
}).Key);
public List<Piece> Player1Hand { get; }
public List<Piece> Player2Hand { 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; }
/// <summary>
/// Copy constructor.
@@ -61,30 +60,13 @@ namespace Shogi.Domain
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];
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;
}
set => board[notation] = value;
}
public Piece? this[Vector2 vector]
@@ -112,21 +94,7 @@ namespace Shogi.Domain
{
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++)
{
for (var y = 0; y < 9; y++)
{
var position = new Vector2(x, y);
var elem = this[position];
if (elem != null)
callback(elem, position);
}
}
|| this[path.Last()]?.Owner == WhoseTurn;
}
internal bool IsWithinPromotionZone(Vector2 position)
@@ -141,9 +109,10 @@ namespace Shogi.Domain
&& position.Y <= 8 && position.Y >= 0;
}
internal IEnumerable<BoardTile> GetTilesOccupiedBy(WhichPlayer whichPlayer) => board
internal List<BoardTile> GetTilesOccupiedBy(WhichPlayer whichPlayer) => board
.Where(kvp => kvp.Value?.Owner == whichPlayer)
.Select(kvp => new BoardTile(kvp.Value!, FromBoardNotation(kvp.Key)));
.Select(kvp => new BoardTile(FromBoardNotation(kvp.Key), kvp.Value!))
.ToList();
internal void Capture(Vector2 to)
{
@@ -167,7 +136,7 @@ namespace Shogi.Domain
}
}
internal Piece? GetFirstPieceAlongPath(IEnumerable<Vector2> path)
internal Piece? QueryFirstPieceInPath(IEnumerable<Vector2> path)
{
foreach (var step in path)
{
@@ -197,24 +166,6 @@ 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 Lance(WhichPlayer.Player1);