61 lines
2.0 KiB
C#
61 lines
2.0 KiB
C#
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
|
|
using System.Drawing;
|
|
|
|
namespace Shogi.Domain.Other;
|
|
|
|
public class Rules<TPiece> where TPiece : Enum
|
|
{
|
|
private Vector2 boardSize;
|
|
private TPiece theKing;
|
|
private Dictionary<TPiece, ICollection<Path>> piecePaths;
|
|
|
|
/// <summary>
|
|
/// Begin a new set of rules. If any rules already exist, this method will erase them.
|
|
/// </summary>
|
|
/// <param name="boardSize">The size of the game board in tiles. For examples, Chess is 8x8 and Shogi is 9x9.</param>
|
|
/// <param name="king">The piece that represents the King for each player or the piece that, when lost, results in losing the game.</param>
|
|
public Rules<TPiece> CreateNewRules(Vector2 boardSize, TPiece king)
|
|
{
|
|
this.boardSize = boardSize;
|
|
theKing = king;
|
|
piecePaths = [];
|
|
return this;
|
|
}
|
|
|
|
public Rules<TPiece> RegisterPieceWithRules(PieceRulesRegistration<TPiece> pieceToRegister)
|
|
{
|
|
if (piecePaths.ContainsKey(pieceToRegister.WhichPiece))
|
|
{
|
|
throw new ArgumentException("This type of piece has already been registered.", nameof(pieceToRegister));
|
|
}
|
|
|
|
piecePaths.Add(pieceToRegister.WhichPiece, pieceToRegister.MoveSet);
|
|
return this;
|
|
}
|
|
|
|
public int ValidateMove(Vector2 start, Vector2 end, TPiece[][] board)
|
|
{
|
|
if (board.GetLength(0) != boardSize.X || board.GetLength(1) != boardSize.Y)
|
|
{
|
|
throw new ArgumentException($"2D array dimensions must match boardSize given during {nameof(CreateNewRules)} method.", nameof(board));
|
|
}
|
|
if (start - start != Vector2.Zero)
|
|
{
|
|
throw new ArgumentException("Negative values not allowed.", nameof(start));
|
|
}
|
|
if (end - end != Vector2.Zero)
|
|
{
|
|
throw new ArgumentException("Negative values not allowed.", nameof(end));
|
|
}
|
|
if (start.X >= boardSize.X || start.Y >= boardSize.Y)
|
|
{
|
|
throw new ArgumentException("Start position must be within the given boardSize.", nameof(start));
|
|
}
|
|
if (end.X >= boardSize.X || end.Y >= boardSize.Y)
|
|
{
|
|
throw new ArgumentException("End position must be within the given boardSize.", nameof(end));
|
|
}
|
|
|
|
|
|
}
|
|
} |