Organize domain project.

This commit is contained in:
2023-02-20 18:54:53 -06:00
parent c251770f58
commit faff4049d5
36 changed files with 603 additions and 599 deletions

View File

@@ -1,33 +1,32 @@
using Shogi.Contracts.Types;
using System.Collections.ObjectModel;
using System.Linq;
namespace Shogi.Api.Extensions;
public static class ContractsExtensions
{
public static WhichPlayer ToContract(this Domain.WhichPlayer player)
public static WhichPlayer ToContract(this Domain.ValueObjects.WhichPlayer player)
{
return player switch
{
Domain.WhichPlayer.Player1 => WhichPlayer.Player1,
Domain.WhichPlayer.Player2 => WhichPlayer.Player2,
Domain.ValueObjects.WhichPlayer.Player1 => WhichPlayer.Player1,
Domain.ValueObjects.WhichPlayer.Player2 => WhichPlayer.Player2,
_ => throw new NotImplementedException(),
};
}
public static WhichPiece ToContract(this Domain.WhichPiece piece)
public static WhichPiece ToContract(this Domain.ValueObjects.WhichPiece piece)
{
return piece switch
{
Domain.WhichPiece.King => WhichPiece.King,
Domain.WhichPiece.GoldGeneral => WhichPiece.GoldGeneral,
Domain.WhichPiece.SilverGeneral => WhichPiece.SilverGeneral,
Domain.WhichPiece.Bishop => WhichPiece.Bishop,
Domain.WhichPiece.Rook => WhichPiece.Rook,
Domain.WhichPiece.Knight => WhichPiece.Knight,
Domain.WhichPiece.Lance => WhichPiece.Lance,
Domain.WhichPiece.Pawn => WhichPiece.Pawn,
Domain.ValueObjects.WhichPiece.King => WhichPiece.King,
Domain.ValueObjects.WhichPiece.GoldGeneral => WhichPiece.GoldGeneral,
Domain.ValueObjects.WhichPiece.SilverGeneral => WhichPiece.SilverGeneral,
Domain.ValueObjects.WhichPiece.Bishop => WhichPiece.Bishop,
Domain.ValueObjects.WhichPiece.Rook => WhichPiece.Rook,
Domain.ValueObjects.WhichPiece.Knight => WhichPiece.Knight,
Domain.ValueObjects.WhichPiece.Lance => WhichPiece.Lance,
Domain.ValueObjects.WhichPiece.Pawn => WhichPiece.Pawn,
_ => throw new NotImplementedException(),
};
}
@@ -50,19 +49,19 @@ public static class ContractsExtensions
public static Dictionary<string, Piece?> ToContract(this ReadOnlyDictionary<string, Domain.ValueObjects.Piece?> boardState) =>
boardState.ToDictionary(kvp => kvp.Key, kvp => kvp.Value?.ToContract());
public static Domain.WhichPiece? ToDomain(this WhichPiece? piece) => piece.HasValue ? piece.Value.ToDomain() : null;
public static Domain.WhichPiece ToDomain(this WhichPiece piece)
public static Domain.ValueObjects.WhichPiece? ToDomain(this WhichPiece? piece) => piece.HasValue ? piece.Value.ToDomain() : null;
public static Domain.ValueObjects.WhichPiece ToDomain(this WhichPiece piece)
{
return piece switch
{
WhichPiece.King => Domain.WhichPiece.King,
WhichPiece.GoldGeneral => Domain.WhichPiece.GoldGeneral,
WhichPiece.SilverGeneral => Domain.WhichPiece.SilverGeneral,
WhichPiece.Bishop => Domain.WhichPiece.Bishop,
WhichPiece.Rook => Domain.WhichPiece.Rook,
WhichPiece.Knight => Domain.WhichPiece.Knight,
WhichPiece.Lance => Domain.WhichPiece.Lance,
WhichPiece.Pawn => Domain.WhichPiece.Pawn,
WhichPiece.King => Domain.ValueObjects.WhichPiece.King,
WhichPiece.GoldGeneral => Domain.ValueObjects.WhichPiece.GoldGeneral,
WhichPiece.SilverGeneral => Domain.ValueObjects.WhichPiece.SilverGeneral,
WhichPiece.Bishop => Domain.ValueObjects.WhichPiece.Bishop,
WhichPiece.Rook => Domain.ValueObjects.WhichPiece.Rook,
WhichPiece.Knight => Domain.ValueObjects.WhichPiece.Knight,
WhichPiece.Lance => Domain.ValueObjects.WhichPiece.Lance,
WhichPiece.Pawn => Domain.ValueObjects.WhichPiece.Pawn,
_ => throw new NotImplementedException(),
};
}

View File

@@ -1,6 +1,6 @@
using Shogi.Contracts.Types;
using DomainWhichPiece = Shogi.Domain.WhichPiece;
using DomainWhichPlayer = Shogi.Domain.WhichPlayer;
using DomainWhichPiece = Shogi.Domain.ValueObjects.WhichPiece;
using DomainWhichPlayer = Shogi.Domain.ValueObjects.WhichPlayer;
using Piece = Shogi.Contracts.Types.Piece;
namespace Shogi.Api.Managers

View File

@@ -1,4 +1,4 @@
using Shogi.Domain;
using Shogi.Domain.ValueObjects;
namespace Shogi.Api.Repositories.Dto;

View File

@@ -1,252 +0,0 @@
using Shogi.Domain.ValueObjects;
using System.Collections.ObjectModel;
using BoardTile = System.Collections.Generic.KeyValuePair<System.Numerics.Vector2, Shogi.Domain.ValueObjects.Piece>;
namespace Shogi.Domain;
public class BoardState
{
/// <summary>
/// Board state before any moves have been made, using standard setup and rules.
/// </summary>
public static BoardState StandardStarting => new(
state: BuildStandardStartingBoardState(),
player1Hand: new(),
player2Hand: new(),
whoseTurn: WhichPlayer.Player1,
playerInCheck: null,
previousMove: new Move());
/// <summary>
/// Key is position notation, such as "E4".
/// </summary>
private readonly Dictionary<string, Piece?> board;
public BoardState(
Dictionary<string, Piece?> state,
List<Piece> player1Hand,
List<Piece> player2Hand,
WhichPlayer whoseTurn,
WhichPlayer? playerInCheck,
Move previousMove)
{
board = state;
Player1Hand = player1Hand;
Player2Hand = player2Hand;
PreviousMove = previousMove;
WhoseTurn = whoseTurn;
InCheck = playerInCheck;
}
/// <summary>
/// Copy constructor.
/// </summary>
public BoardState(BoardState other)
{
board = new(81);
foreach (var kvp in other.board)
{
var piece = kvp.Value;
board[kvp.Key] = piece == null ? null : Piece.Create(piece.WhichPiece, piece.Owner, piece.IsPromoted);
}
WhoseTurn = other.WhoseTurn;
InCheck = other.InCheck;
IsCheckmate = other.IsCheckmate;
PreviousMove = other.PreviousMove;
Player1Hand = new(other.Player1Hand);
Player2Hand = new(other.Player2Hand);
}
public ReadOnlyDictionary<string, Piece?> State => new(board);
public List<Piece> ActivePlayerHand => WhoseTurn == WhichPlayer.Player1 ? Player1Hand : Player2Hand;
public Vector2 Player1KingPosition => Notation.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 => Notation.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 Move PreviousMove { get; set; }
public WhichPlayer WhoseTurn { get; set; }
public WhichPlayer? InCheck { get; set; }
public bool IsCheckmate { get; set; }
public Piece? this[string notation]
{
// TODO: Validate "notation" here and throw an exception if invalid.
get => board[notation];
set => board[notation] = value;
}
public Piece? this[Vector2 vector]
{
get => this[Notation.ToBoardNotation(vector)];
set => this[Notation.ToBoardNotation(vector)] = value;
}
public Piece? this[int x, int y]
{
get => this[Notation.ToBoardNotation(x, y)];
set => this[Notation.ToBoardNotation(x, y)] = value;
}
/// <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;
}
internal bool IsWithinPromotionZone(Vector2 position)
{
// TODO: Move this promotion zone logic into the StandardRules class.
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 List<BoardTile> GetTilesOccupiedBy(WhichPlayer whichPlayer) => board
.Where(kvp => kvp.Value?.Owner == whichPlayer)
.Select(kvp => new BoardTile(Notation.FromBoardNotation(kvp.Key), kvp.Value!))
.ToList();
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? QueryFirstPieceInPath(IEnumerable<Vector2> path)
{
foreach (var step in path)
{
if (this[step] != null) return this[step];
}
return null;
}
private static Dictionary<string, Piece?> BuildStandardStartingBoardState()
{
return new Dictionary<string, Piece?>(81)
{
["A1"] = new Lance(WhichPlayer.Player1),
["B1"] = new Knight(WhichPlayer.Player1),
["C1"] = new SilverGeneral(WhichPlayer.Player1),
["D1"] = new GoldGeneral(WhichPlayer.Player1),
["E1"] = new King(WhichPlayer.Player1),
["F1"] = new GoldGeneral(WhichPlayer.Player1),
["G1"] = new SilverGeneral(WhichPlayer.Player1),
["H1"] = new Knight(WhichPlayer.Player1),
["I1"] = new Lance(WhichPlayer.Player1),
["A2"] = null,
["B2"] = new Bishop(WhichPlayer.Player1),
["C2"] = null,
["D2"] = null,
["E2"] = null,
["F2"] = null,
["G2"] = null,
["H2"] = new Rook(WhichPlayer.Player1),
["I2"] = null,
["A3"] = new Pawn(WhichPlayer.Player1),
["B3"] = new Pawn(WhichPlayer.Player1),
["C3"] = new Pawn(WhichPlayer.Player1),
["D3"] = new Pawn(WhichPlayer.Player1),
["E3"] = new Pawn(WhichPlayer.Player1),
["F3"] = new Pawn(WhichPlayer.Player1),
["G3"] = new Pawn(WhichPlayer.Player1),
["H3"] = new Pawn(WhichPlayer.Player1),
["I3"] = new Pawn(WhichPlayer.Player1),
["A4"] = null,
["B4"] = null,
["C4"] = null,
["D4"] = null,
["E4"] = null,
["F4"] = null,
["G4"] = null,
["H4"] = null,
["I4"] = null,
["A5"] = null,
["B5"] = null,
["C5"] = null,
["D5"] = null,
["E5"] = null,
["F5"] = null,
["G5"] = null,
["H5"] = null,
["I5"] = null,
["A6"] = null,
["B6"] = null,
["C6"] = null,
["D6"] = null,
["E6"] = null,
["F6"] = null,
["G6"] = null,
["H6"] = null,
["I6"] = null,
["A7"] = new Pawn(WhichPlayer.Player2),
["B7"] = new Pawn(WhichPlayer.Player2),
["C7"] = new Pawn(WhichPlayer.Player2),
["D7"] = new Pawn(WhichPlayer.Player2),
["E7"] = new Pawn(WhichPlayer.Player2),
["F7"] = new Pawn(WhichPlayer.Player2),
["G7"] = new Pawn(WhichPlayer.Player2),
["H7"] = new Pawn(WhichPlayer.Player2),
["I7"] = new Pawn(WhichPlayer.Player2),
["A8"] = null,
["B8"] = new Rook(WhichPlayer.Player2),
["C8"] = null,
["D8"] = null,
["E8"] = null,
["F8"] = null,
["G8"] = null,
["H8"] = new Bishop(WhichPlayer.Player2),
["I8"] = null,
["A9"] = new Lance(WhichPlayer.Player2),
["B9"] = new Knight(WhichPlayer.Player2),
["C9"] = new SilverGeneral(WhichPlayer.Player2),
["D9"] = new GoldGeneral(WhichPlayer.Player2),
["E9"] = new King(WhichPlayer.Player2),
["F9"] = new GoldGeneral(WhichPlayer.Player2),
["G9"] = new SilverGeneral(WhichPlayer.Player2),
["H9"] = new Knight(WhichPlayer.Player2),
["I9"] = new Lance(WhichPlayer.Player2)
};
}
}

View File

@@ -1,19 +0,0 @@
using Shogi.Domain.ValueObjects;
namespace Shogi.Domain
{
internal static class DomainExtensions
{
public static bool IsKing(this Piece self) => self.WhichPiece == WhichPiece.King;
public static bool IsBetween(this float self, float min, float max)
{
return self >= min && self <= max;
}
public static bool IsInsideBoardBoundary(this Vector2 self)
{
return self.X.IsBetween(0, 8) && self.Y.IsBetween(0, 8);
}
}
}

View File

@@ -1,14 +0,0 @@
namespace Shogi.Domain
{
public class MoveResult
{
public bool Success { get; }
public string Reason { get; }
public MoveResult(bool isSuccess, string reason = "")
{
Success = isSuccess;
Reason = reason;
}
}
}

View File

@@ -1,33 +0,0 @@
using System.Text.RegularExpressions;
namespace Shogi.Domain
{
public static class Notation
{
private static readonly string BoardNotationRegex = @"(?<file>[A-I])(?<rank>[1-9])";
private static readonly char A = 'A';
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)
{
if (Regex.IsMatch(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);
}
throw new ArgumentException($"Board notation not recognized. Notation given: {notation}");
}
}
}

View File

@@ -1,22 +0,0 @@
using System.Numerics;
namespace Shogi.Domain.Pathing
{
/// <summary>
/// Directions are relative to the perspective of Player 1.
/// Up points towards player 1. Down points towards player 2.
/// </summary>
public static class Direction
{
public static readonly Vector2 Up = new(0, 1);
public static readonly Vector2 Down = new(0, -1);
public static readonly Vector2 Left = new(-1, 0);
public static readonly Vector2 Right = new(1, 0);
public static readonly Vector2 UpLeft = new(-1, 1);
public static readonly Vector2 UpRight = new(1, 1);
public static readonly Vector2 DownLeft = new(-1, -1);
public static readonly Vector2 DownRight = new(1, -1);
public static readonly Vector2 KnightLeft = new(-1, 2);
public static readonly Vector2 KnightRight = new(1, 2);
}
}

View File

@@ -1,8 +0,0 @@
namespace Shogi.Domain.Pathing
{
public enum Distance
{
OneStep,
MultiStep
}
}

View File

@@ -1,41 +0,0 @@
using System.Diagnostics;
namespace Shogi.Domain.Pathing
{
[DebuggerDisplay("{Direction} - {Distance}")]
public class Path
{
public Vector2 Direction { get; }
public Distance Distance { get; }
public Path(Vector2 direction, Distance distance = Distance.OneStep)
{
Direction = direction;
Distance = distance;
}
public Path Invert() => new(Vector2.Negate(Direction), Distance);
}
public static class PathExtensions
{
public static Path GetNearestPath(this IEnumerable<Path> paths, Vector2 start, Vector2 end)
{
if (!paths.DefaultIfEmpty().Any())
{
throw new ArgumentException("No paths to get nearest path from.");
}
var shortestPath = paths.First();
foreach (var path in paths.Skip(1))
{
var distance = Vector2.Distance(start + path.Direction, end);
var shortestDistance = Vector2.Distance(start + shortestPath.Direction, end);
if (distance < shortestDistance)
{
shortestPath = path;
}
}
return shortestPath;
}
}
}

View File

@@ -1,4 +1,4 @@
using Shogi.Domain.Pathing;
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
using System.Collections.ObjectModel;
namespace Shogi.Domain.ValueObjects

View File

@@ -0,0 +1,252 @@
using System.Collections.ObjectModel;
using Shogi.Domain.YetToBeAssimilatedIntoDDD;
using BoardTile = System.Collections.Generic.KeyValuePair<System.Numerics.Vector2, Shogi.Domain.ValueObjects.Piece>;
namespace Shogi.Domain.ValueObjects;
public class BoardState
{
/// <summary>
/// Board state before any moves have been made, using standard setup and rules.
/// </summary>
public static BoardState StandardStarting => new(
state: BuildStandardStartingBoardState(),
player1Hand: new(),
player2Hand: new(),
whoseTurn: WhichPlayer.Player1,
playerInCheck: null,
previousMove: new Move());
/// <summary>
/// Key is position notation, such as "E4".
/// </summary>
private readonly Dictionary<string, Piece?> board;
public BoardState(
Dictionary<string, Piece?> state,
List<Piece> player1Hand,
List<Piece> player2Hand,
WhichPlayer whoseTurn,
WhichPlayer? playerInCheck,
Move previousMove)
{
board = state;
Player1Hand = player1Hand;
Player2Hand = player2Hand;
PreviousMove = previousMove;
WhoseTurn = whoseTurn;
InCheck = playerInCheck;
}
/// <summary>
/// Copy constructor.
/// </summary>
public BoardState(BoardState other)
{
board = new(81);
foreach (var kvp in other.board)
{
var piece = kvp.Value;
board[kvp.Key] = piece == null ? null : Piece.Create(piece.WhichPiece, piece.Owner, piece.IsPromoted);
}
WhoseTurn = other.WhoseTurn;
InCheck = other.InCheck;
IsCheckmate = other.IsCheckmate;
PreviousMove = other.PreviousMove;
Player1Hand = new(other.Player1Hand);
Player2Hand = new(other.Player2Hand);
}
public ReadOnlyDictionary<string, Piece?> State => new(board);
public List<Piece> ActivePlayerHand => WhoseTurn == WhichPlayer.Player1 ? Player1Hand : Player2Hand;
public Vector2 Player1KingPosition => Notation.FromBoardNotation(board.Where(kvp => kvp.Value != null).Single(kvp =>
{
var piece = kvp.Value;
return piece!.IsKing() && piece!.Owner == WhichPlayer.Player1;
}).Key);
public Vector2 Player2KingPosition => Notation.FromBoardNotation(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 Move PreviousMove { get; set; }
public WhichPlayer WhoseTurn { get; set; }
public WhichPlayer? InCheck { get; set; }
public bool IsCheckmate { get; set; }
public Piece? this[string notation]
{
// TODO: Validate "notation" here and throw an exception if invalid.
get => board[notation];
set => board[notation] = value;
}
public Piece? this[Vector2 vector]
{
get => this[Notation.ToBoardNotation(vector)];
set => this[Notation.ToBoardNotation(vector)] = value;
}
public Piece? this[int x, int y]
{
get => this[Notation.ToBoardNotation(x, y)];
set => this[Notation.ToBoardNotation(x, y)] = value;
}
/// <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;
}
internal bool IsWithinPromotionZone(Vector2 position)
{
// TODO: Move this promotion zone logic into the StandardRules class.
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 List<BoardTile> GetTilesOccupiedBy(WhichPlayer whichPlayer) => board
.Where(kvp => kvp.Value?.Owner == whichPlayer)
.Select(kvp => new BoardTile(Notation.FromBoardNotation(kvp.Key), kvp.Value!))
.ToList();
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? QueryFirstPieceInPath(IEnumerable<Vector2> path)
{
foreach (var step in path)
{
if (this[step] != null) return this[step];
}
return null;
}
private static Dictionary<string, Piece?> BuildStandardStartingBoardState()
{
return new Dictionary<string, Piece?>(81)
{
["A1"] = new Lance(WhichPlayer.Player1),
["B1"] = new Knight(WhichPlayer.Player1),
["C1"] = new SilverGeneral(WhichPlayer.Player1),
["D1"] = new GoldGeneral(WhichPlayer.Player1),
["E1"] = new King(WhichPlayer.Player1),
["F1"] = new GoldGeneral(WhichPlayer.Player1),
["G1"] = new SilverGeneral(WhichPlayer.Player1),
["H1"] = new Knight(WhichPlayer.Player1),
["I1"] = new Lance(WhichPlayer.Player1),
["A2"] = null,
["B2"] = new Bishop(WhichPlayer.Player1),
["C2"] = null,
["D2"] = null,
["E2"] = null,
["F2"] = null,
["G2"] = null,
["H2"] = new Rook(WhichPlayer.Player1),
["I2"] = null,
["A3"] = new Pawn(WhichPlayer.Player1),
["B3"] = new Pawn(WhichPlayer.Player1),
["C3"] = new Pawn(WhichPlayer.Player1),
["D3"] = new Pawn(WhichPlayer.Player1),
["E3"] = new Pawn(WhichPlayer.Player1),
["F3"] = new Pawn(WhichPlayer.Player1),
["G3"] = new Pawn(WhichPlayer.Player1),
["H3"] = new Pawn(WhichPlayer.Player1),
["I3"] = new Pawn(WhichPlayer.Player1),
["A4"] = null,
["B4"] = null,
["C4"] = null,
["D4"] = null,
["E4"] = null,
["F4"] = null,
["G4"] = null,
["H4"] = null,
["I4"] = null,
["A5"] = null,
["B5"] = null,
["C5"] = null,
["D5"] = null,
["E5"] = null,
["F5"] = null,
["G5"] = null,
["H5"] = null,
["I5"] = null,
["A6"] = null,
["B6"] = null,
["C6"] = null,
["D6"] = null,
["E6"] = null,
["F6"] = null,
["G6"] = null,
["H6"] = null,
["I6"] = null,
["A7"] = new Pawn(WhichPlayer.Player2),
["B7"] = new Pawn(WhichPlayer.Player2),
["C7"] = new Pawn(WhichPlayer.Player2),
["D7"] = new Pawn(WhichPlayer.Player2),
["E7"] = new Pawn(WhichPlayer.Player2),
["F7"] = new Pawn(WhichPlayer.Player2),
["G7"] = new Pawn(WhichPlayer.Player2),
["H7"] = new Pawn(WhichPlayer.Player2),
["I7"] = new Pawn(WhichPlayer.Player2),
["A8"] = null,
["B8"] = new Rook(WhichPlayer.Player2),
["C8"] = null,
["D8"] = null,
["E8"] = null,
["F8"] = null,
["G8"] = null,
["H8"] = new Bishop(WhichPlayer.Player2),
["I8"] = null,
["A9"] = new Lance(WhichPlayer.Player2),
["B9"] = new Knight(WhichPlayer.Player2),
["C9"] = new SilverGeneral(WhichPlayer.Player2),
["D9"] = new GoldGeneral(WhichPlayer.Player2),
["E9"] = new King(WhichPlayer.Player2),
["F9"] = new GoldGeneral(WhichPlayer.Player2),
["G9"] = new SilverGeneral(WhichPlayer.Player2),
["H9"] = new Knight(WhichPlayer.Player2),
["I9"] = new Lance(WhichPlayer.Player2)
};
}
}

View File

@@ -1,4 +1,4 @@
using Shogi.Domain.Pathing;
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
using System.Collections.ObjectModel;
namespace Shogi.Domain.ValueObjects

View File

@@ -1,4 +1,4 @@
using Shogi.Domain.Pathing;
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
using System.Collections.ObjectModel;
namespace Shogi.Domain.ValueObjects

View File

@@ -1,4 +1,4 @@
using Shogi.Domain.Pathing;
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
using System.Collections.ObjectModel;
namespace Shogi.Domain.ValueObjects

View File

@@ -1,4 +1,4 @@
using Shogi.Domain.Pathing;
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
using System.Collections.ObjectModel;
namespace Shogi.Domain.ValueObjects

View File

@@ -0,0 +1,14 @@
namespace Shogi.Domain.ValueObjects
{
public class MoveResult
{
public bool Success { get; }
public string Reason { get; }
public MoveResult(bool isSuccess, string reason = "")
{
Success = isSuccess;
Reason = reason;
}
}
}

View File

@@ -1,4 +1,4 @@
using Shogi.Domain.Pathing;
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
using System.Collections.ObjectModel;
namespace Shogi.Domain.ValueObjects

View File

@@ -1,4 +1,4 @@
using Shogi.Domain.Pathing;
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
using System.Diagnostics;
namespace Shogi.Domain.ValueObjects

View File

@@ -1,4 +1,4 @@
using Shogi.Domain.Pathing;
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
using System.Collections.ObjectModel;
namespace Shogi.Domain.ValueObjects;

View File

@@ -1,4 +1,5 @@
using System.Text;
using Shogi.Domain.YetToBeAssimilatedIntoDDD;
namespace Shogi.Domain.ValueObjects;

View File

@@ -1,4 +1,4 @@
using Shogi.Domain.Pathing;
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
using System.Collections.ObjectModel;
namespace Shogi.Domain.ValueObjects

View File

@@ -1,7 +1,7 @@
using Shogi.Domain.ValueObjects;
using Shogi.Domain.YetToBeAssimilatedIntoDDD;
using BoardTile = System.Collections.Generic.KeyValuePair<System.Numerics.Vector2, Shogi.Domain.ValueObjects.Piece>;
namespace Shogi.Domain
namespace Shogi.Domain.ValueObjects
{
internal class StandardRules
{
@@ -84,8 +84,8 @@ namespace Shogi.Domain
case WhichPiece.Knight:
{
// Knight cannot be placed onto the farthest two ranks from the hand.
if ((boardState.WhoseTurn == WhichPlayer.Player1 && to.Y > 6)
|| (boardState.WhoseTurn == WhichPlayer.Player2 && to.Y < 2))
if (boardState.WhoseTurn == WhichPlayer.Player1 && to.Y > 6
|| boardState.WhoseTurn == WhichPlayer.Player2 && to.Y < 2)
{
return new MoveResult(false, "Knight has no valid moves after placed.");
}
@@ -95,8 +95,8 @@ namespace Shogi.Domain
case WhichPiece.Pawn:
{
// Lance and Pawn cannot be placed onto the farthest rank from the hand.
if ((boardState.WhoseTurn == WhichPlayer.Player1 && to.Y == 8)
|| (boardState.WhoseTurn == WhichPlayer.Player2 && to.Y == 0))
if (boardState.WhoseTurn == WhichPlayer.Player1 && to.Y == 8
|| boardState.WhoseTurn == WhichPlayer.Player2 && to.Y == 0)
{
return new MoveResult(false, $"{pieceInHand} has no valid moves after placed.");
}
@@ -222,7 +222,7 @@ namespace Shogi.Domain
* If no ability to block the check, maybe the king can evade check by moving.
*/
foreach (var maybeSafePosition in GetPossiblePositionsForKing(this.boardState.WhoseTurn))
foreach (var maybeSafePosition in GetPossiblePositionsForKing(boardState.WhoseTurn))
{
threats = tilesOccupiedByOpponent
.Where(tile => PieceHasLineOfSight(tile, maybeSafePosition))

View File

@@ -0,0 +1,14 @@
namespace Shogi.Domain.ValueObjects
{
public enum WhichPiece
{
King,
GoldGeneral,
SilverGeneral,
Bishop,
Rook,
Knight,
Lance,
Pawn
}
}

View File

@@ -0,0 +1,8 @@
namespace Shogi.Domain.ValueObjects
{
public enum WhichPlayer
{
Player1,
Player2
}
}

View File

@@ -1,14 +0,0 @@
namespace Shogi.Domain
{
public enum WhichPiece
{
King,
GoldGeneral,
SilverGeneral,
Bishop,
Rook,
Knight,
Lance,
Pawn
}
}

View File

@@ -1,8 +0,0 @@
namespace Shogi.Domain
{
public enum WhichPlayer
{
Player1,
Player2
}
}

View File

@@ -0,0 +1,19 @@
using Shogi.Domain.ValueObjects;
namespace Shogi.Domain.YetToBeAssimilatedIntoDDD
{
internal static class DomainExtensions
{
public static bool IsKing(this Piece self) => self.WhichPiece == WhichPiece.King;
public static bool IsBetween(this float self, float min, float max)
{
return self >= min && self <= max;
}
public static bool IsInsideBoardBoundary(this Vector2 self)
{
return self.X.IsBetween(0, 8) && self.Y.IsBetween(0, 8);
}
}
}

View File

@@ -0,0 +1,33 @@
using System.Text.RegularExpressions;
namespace Shogi.Domain.YetToBeAssimilatedIntoDDD
{
public static class Notation
{
private static readonly string BoardNotationRegex = @"(?<file>[A-I])(?<rank>[1-9])";
private static readonly char A = 'A';
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)
{
if (Regex.IsMatch(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);
}
throw new ArgumentException($"Board notation not recognized. Notation given: {notation}");
}
}
}

View File

@@ -0,0 +1,22 @@
using System.Numerics;
namespace Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing
{
/// <summary>
/// Directions are relative to the perspective of Player 1.
/// Up points towards player 1. Down points towards player 2.
/// </summary>
public static class Direction
{
public static readonly Vector2 Up = new(0, 1);
public static readonly Vector2 Down = new(0, -1);
public static readonly Vector2 Left = new(-1, 0);
public static readonly Vector2 Right = new(1, 0);
public static readonly Vector2 UpLeft = new(-1, 1);
public static readonly Vector2 UpRight = new(1, 1);
public static readonly Vector2 DownLeft = new(-1, -1);
public static readonly Vector2 DownRight = new(1, -1);
public static readonly Vector2 KnightLeft = new(-1, 2);
public static readonly Vector2 KnightRight = new(1, 2);
}
}

View File

@@ -0,0 +1,8 @@
namespace Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing
{
public enum Distance
{
OneStep,
MultiStep
}
}

View File

@@ -0,0 +1,41 @@
using System.Diagnostics;
namespace Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing
{
[DebuggerDisplay("{Direction} - {Distance}")]
public class Path
{
public Vector2 Direction { get; }
public Distance Distance { get; }
public Path(Vector2 direction, Distance distance = Distance.OneStep)
{
Direction = direction;
Distance = distance;
}
public Path Invert() => new(Vector2.Negate(Direction), Distance);
}
public static class PathExtensions
{
public static Path GetNearestPath(this IEnumerable<Path> paths, Vector2 start, Vector2 end)
{
if (!paths.DefaultIfEmpty().Any())
{
throw new ArgumentException("No paths to get nearest path from.");
}
var shortestPath = paths.First();
foreach (var path in paths.Skip(1))
{
var distance = Vector2.Distance(start + path.Direction, end);
var shortestDistance = Vector2.Distance(start + shortestPath.Direction, end);
if (distance < shortestDistance)
{
shortestPath = path;
}
}
return shortestPath;
}
}
}

View File

@@ -0,0 +1,4 @@
# Shogi.Domain
### TODO:
* There are enough non-DDD classes around navigating the standard 9x9 Shogi board that probably a new value object or entity is merited. See classes within Pathing folder as well as Notation.cs.

View File

@@ -1,4 +1,5 @@
using System.Numerics;
using Shogi.Domain.YetToBeAssimilatedIntoDDD;
namespace Shogi.Domain.UnitTests
{

View File

@@ -1,11 +1,11 @@
using Shogi.Domain.Pathing;
using Shogi.Domain.ValueObjects;
using Shogi.Domain.ValueObjects;
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
using System.Numerics;
namespace Shogi.Domain.UnitTests
namespace Shogi.Domain.UnitTests;
public class RookShould
{
public class RookShould
{
public class MoveSet
{
private readonly Rook rook1;
@@ -82,12 +82,10 @@ namespace Shogi.Domain.UnitTests
}
private readonly Rook rookPlayer1;
private readonly Rook rookPlayer2;
public RookShould()
{
this.rookPlayer1 = new Rook(WhichPlayer.Player1);
this.rookPlayer2 = new Rook(WhichPlayer.Player2);
}
[Fact]
@@ -221,5 +219,4 @@ namespace Shogi.Domain.UnitTests
steps.Should().HaveCount(1);
steps.Should().Contain(new Vector2(1, 1));
}
}
}

View File

@@ -1,4 +1,6 @@
namespace Shogi.Domain.UnitTests;
using Shogi.Domain.ValueObjects;
namespace Shogi.Domain.UnitTests;
public class ShogiBoardStateShould
{