Split Shogi into ShogiBoardState and StandardRules
This commit is contained in:
199
Shogi.Domain/ShogiBoardState.cs
Normal file
199
Shogi.Domain/ShogiBoardState.cs
Normal file
@@ -0,0 +1,199 @@
|
||||
using System.Numerics;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Shogi.Domain
|
||||
{
|
||||
// TODO: Avoid extending dictionary. Use composition instead.
|
||||
// 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 char A = 'A';
|
||||
public delegate void ForEachDelegate(Piece element, Vector2 position);
|
||||
/// <summary>
|
||||
/// Key is position notation, such as "E4".
|
||||
/// </summary>
|
||||
private Dictionary<string, Piece?> board;
|
||||
|
||||
public List<Piece> Hand => WhoseTurn == WhichPlayer.Player1 ? Player1Hand : Player2Hand;
|
||||
public List<Piece> Player1Hand { get; }
|
||||
public List<Piece> Player2Hand { get; }
|
||||
public List<Move> MoveHistory { get; }
|
||||
public WhichPlayer WhoseTurn => MoveHistory.Count % 2 == 0 ? WhichPlayer.Player1 : WhichPlayer.Player2;
|
||||
public WhichPlayer? InCheck { get; private set; }
|
||||
public bool IsCheckmate { get; private set; }
|
||||
|
||||
public ShogiBoardState()
|
||||
{
|
||||
board = new Dictionary<string, Piece?>(81);
|
||||
InitializeBoardState();
|
||||
Player1Hand = new List<Piece>();
|
||||
Player2Hand = new List<Piece>();
|
||||
MoveHistory = new List<Move>();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Copy constructor.
|
||||
/// </summary>
|
||||
public ShogiBoardState(ShogiBoardState other) : this()
|
||||
{
|
||||
foreach (var kvp in other.board)
|
||||
{
|
||||
board[kvp.Key] = kvp.Value == null ? null : new Piece(kvp.Value);
|
||||
}
|
||||
MoveHistory.AddRange(other.MoveHistory);
|
||||
Player1Hand.AddRange(other.Player1Hand);
|
||||
Player2Hand.AddRange(other.Player2Hand);
|
||||
}
|
||||
|
||||
public Piece? this[string notation]
|
||||
{
|
||||
// TODO: Validate "notation" here and throw an exception if invalid.
|
||||
get => board[notation.ToUpper()];
|
||||
set => board[notation.ToUpper()] = value;
|
||||
}
|
||||
|
||||
public Piece? this[Vector2 vector]
|
||||
{
|
||||
get => this[ToBoardNotation(vector)];
|
||||
set => this[ToBoardNotation(vector)] = value;
|
||||
}
|
||||
|
||||
public Piece? this[int x, int y]
|
||||
{
|
||||
get => this[ToBoardNotation(x, y)];
|
||||
set => this[ToBoardNotation(x, y)] = value;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static string ToBoardNotation(Vector2 vector)
|
||||
{
|
||||
return ToBoardNotation((int)vector.X, (int)vector.Y);
|
||||
}
|
||||
public static string ToBoardNotation(int x, int y)
|
||||
{
|
||||
var file = (char)(x + A);
|
||||
var rank = y + 1;
|
||||
return $"{file}{rank}";
|
||||
}
|
||||
public static Vector2 FromBoardNotation(string notation)
|
||||
{
|
||||
notation = notation.ToUpper();
|
||||
if (Regex.IsMatch(notation, BoardNotationRegex))
|
||||
{
|
||||
var match = Regex.Match(notation, BoardNotationRegex);
|
||||
char file = match.Groups["file"].Value[0];
|
||||
int rank = int.Parse(match.Groups["rank"].Value);
|
||||
return new Vector2(file - A, rank - 1);
|
||||
}
|
||||
throw new ArgumentException($"Board notation not recognized. Notation given: {notation}");
|
||||
}
|
||||
|
||||
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["A2"] = null;
|
||||
this["B2"] = new Piece(WhichPiece.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["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["A4"] = null;
|
||||
this["B4"] = null;
|
||||
this["C4"] = null;
|
||||
this["D4"] = null;
|
||||
this["E4"] = null;
|
||||
this["F4"] = null;
|
||||
this["G4"] = null;
|
||||
this["H4"] = null;
|
||||
this["I4"] = null;
|
||||
|
||||
this["A5"] = null;
|
||||
this["B5"] = null;
|
||||
this["C5"] = null;
|
||||
this["D5"] = null;
|
||||
this["E5"] = null;
|
||||
this["F5"] = null;
|
||||
this["G5"] = null;
|
||||
this["H5"] = null;
|
||||
this["I5"] = null;
|
||||
|
||||
this["A6"] = null;
|
||||
this["B6"] = null;
|
||||
this["C6"] = null;
|
||||
this["D6"] = null;
|
||||
this["E6"] = null;
|
||||
this["F6"] = null;
|
||||
this["G6"] = null;
|
||||
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["A8"] = null;
|
||||
this["B8"] = new Piece(WhichPiece.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["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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user