Split Shogi into ShogiBoardState and StandardRules
This commit is contained in:
53
Shogi.Domain/Move.cs
Normal file
53
Shogi.Domain/Move.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.Diagnostics;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Shogi.Domain
|
||||
{
|
||||
[DebuggerDisplay("{From} - {To}")]
|
||||
public class Move
|
||||
{
|
||||
public Vector2? From { get; } // TODO: Use string notation
|
||||
public bool IsPromotion { get; }
|
||||
public WhichPiece? PieceFromHand { get; }
|
||||
public Vector2 To { get; }
|
||||
|
||||
public Move(Vector2 from, Vector2 to, bool isPromotion = false)
|
||||
{
|
||||
From = from;
|
||||
To = to;
|
||||
IsPromotion = isPromotion;
|
||||
}
|
||||
public Move(WhichPiece pieceFromHand, Vector2 to)
|
||||
{
|
||||
PieceFromHand = pieceFromHand;
|
||||
To = to;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor to represent moving a piece on the Board to another position on the Board.
|
||||
/// </summary>
|
||||
/// <param name="fromNotation">Position the piece is being moved from.</param>
|
||||
/// <param name="toNotation">Position the piece is being moved to.</param>
|
||||
/// <param name="isPromotion">If the moving piece should be promoted.</param>
|
||||
public Move(string fromNotation, string toNotation, bool isPromotion = false)
|
||||
{
|
||||
//From = NotationHelper.FromBoardNotation(fromNotation);
|
||||
//To = NotationHelper.FromBoardNotation(toNotation);
|
||||
//IsPromotion = isPromotion;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor to represent moving a piece from the Hand to the Board.
|
||||
/// </summary>
|
||||
/// <param name="pieceFromHand">The piece being moved from the Hand to the Board.</param>
|
||||
/// <param name="toNotation">Position the piece is being moved to.</param>
|
||||
/// <param name="isPromotion">If the moving piece should be promoted.</param>
|
||||
public Move(WhichPiece pieceFromHand, string toNotation, bool isPromotion = false)
|
||||
{
|
||||
//From = null;
|
||||
//PieceFromHand = pieceFromHand;
|
||||
//To = NotationHelper.FromBoardNotation(toNotation);
|
||||
//IsPromotion = isPromotion;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Shogi.Domain/MoveResult.cs
Normal file
14
Shogi.Domain/MoveResult.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Shogi.Domain
|
||||
{
|
||||
public class MoveResult
|
||||
{
|
||||
public bool Success { get; }
|
||||
public string Reason { get; }
|
||||
|
||||
public MoveResult(bool isSuccess, string reason = "")
|
||||
{
|
||||
Success = isSuccess;
|
||||
Reason = reason;
|
||||
}
|
||||
}
|
||||
}
|
||||
44
Shogi.Domain/Piece.cs
Normal file
44
Shogi.Domain/Piece.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Shogi.Domain
|
||||
{
|
||||
[DebuggerDisplay("{WhichPiece} {Owner}")]
|
||||
public class Piece
|
||||
{
|
||||
public WhichPiece WhichPiece { get; }
|
||||
public WhichPlayer Owner { get; private set; }
|
||||
public bool IsPromoted { get; private set; }
|
||||
public bool IsUpsideDown => Owner == WhichPlayer.Player2;
|
||||
|
||||
public Piece(WhichPiece piece, WhichPlayer owner, bool isPromoted = false)
|
||||
{
|
||||
WhichPiece = piece;
|
||||
Owner = owner;
|
||||
IsPromoted = isPromoted;
|
||||
}
|
||||
public Piece(Piece piece) : this(piece.WhichPiece, piece.Owner, piece.IsPromoted)
|
||||
{
|
||||
}
|
||||
|
||||
public bool CanPromote => !IsPromoted
|
||||
&& WhichPiece != WhichPiece.King
|
||||
&& WhichPiece != WhichPiece.GoldGeneral;
|
||||
|
||||
public void ToggleOwnership()
|
||||
{
|
||||
Owner = Owner == WhichPlayer.Player1
|
||||
? WhichPlayer.Player2
|
||||
: WhichPlayer.Player1;
|
||||
}
|
||||
|
||||
public void Promote() => IsPromoted = CanPromote;
|
||||
|
||||
public void Demote() => IsPromoted = false;
|
||||
|
||||
public void Capture()
|
||||
{
|
||||
ToggleOwnership();
|
||||
Demote();
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Shogi.Domain/Shogi.Domain.csproj
Normal file
9
Shogi.Domain/Shogi.Domain.csproj
Normal file
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
90
Shogi.Domain/Shogi.cs
Normal file
90
Shogi.Domain/Shogi.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace Shogi.Domain
|
||||
{
|
||||
/// <summary>
|
||||
/// Facilitates Shogi board state transitions, cognisant of Shogi rules.
|
||||
/// The board is always from Player1's perspective.
|
||||
/// [0,0] is the lower-left position, [8,8] is the higher-right position
|
||||
/// </summary>
|
||||
public sealed class Shogi
|
||||
{
|
||||
private readonly ShogiBoardState board;
|
||||
private readonly StandardRules rules;
|
||||
public string Error { get; private set; }
|
||||
|
||||
public Shogi(ShogiBoardState board)
|
||||
{
|
||||
this.board = board;
|
||||
rules = new StandardRules(this.board);
|
||||
Error = string.Empty;
|
||||
}
|
||||
|
||||
public Shogi(IList<Move> moves) : this()
|
||||
{
|
||||
for (var i = 0; i < moves.Count; i++)
|
||||
{
|
||||
if (!Move(moves[i]))
|
||||
{
|
||||
// Todo: Add some smarts to know why a move was invalid. In check? Piece not found? etc.
|
||||
throw new InvalidOperationException($"Unable to construct ShogiBoard with the given move at index {i}. {Error}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool Move(Move move)
|
||||
{
|
||||
var moveSuccess = TryMove(move);
|
||||
|
||||
if (!moveSuccess)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var otherPlayer = WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
||||
if (EvaluateCheckAfterMove(move, otherPlayer))
|
||||
{
|
||||
InCheck = otherPlayer;
|
||||
IsCheckmate = EvaluateCheckmate();
|
||||
}
|
||||
else
|
||||
{
|
||||
InCheck = null;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts a given move. Returns false if the move is illegal.
|
||||
/// </summary>
|
||||
private bool TryMove(Move move)
|
||||
{
|
||||
// Try making the move in a "throw away" board.
|
||||
var simulator = new StandardRules(new ShogiBoardState(this.board));
|
||||
|
||||
var simulatedMoveResults = move.PieceFromHand.HasValue
|
||||
? simulator.PlaceFromHand(move)
|
||||
: simulator.PlaceFromBoard(move);
|
||||
if (!simulatedMoveResults)
|
||||
{
|
||||
// Surface the error description.
|
||||
Error = simulationBoard.Error;
|
||||
return false;
|
||||
}
|
||||
// If already in check, assert the move that resulted in check no longer results in check.
|
||||
if (InCheck == WhoseTurn)
|
||||
{
|
||||
if (simulationBoard.EvaluateCheckAfterMove(MoveHistory[^1], WhoseTurn))
|
||||
{
|
||||
// Sneakily using this.WhoseTurn instead of validationBoard.WhoseTurn;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// The move is valid and legal; update board state.
|
||||
if (move.PieceFromHand.HasValue) PlaceFromHand(move);
|
||||
else PlaceFromBoard(move);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
268
Shogi.Domain/StandardRules.cs
Normal file
268
Shogi.Domain/StandardRules.cs
Normal file
@@ -0,0 +1,268 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace Shogi.Domain
|
||||
{
|
||||
internal class StandardRules
|
||||
{
|
||||
private readonly ShogiBoardState board;
|
||||
private Vector2 player1KingPosition;
|
||||
private Vector2 player2KingPosition;
|
||||
|
||||
public StandardRules(ShogiBoardState board)
|
||||
{
|
||||
this.board = board;
|
||||
CacheKingPositions();
|
||||
}
|
||||
|
||||
private void CacheKingPositions()
|
||||
{
|
||||
this.board.ForEachNotNull((tile, position) =>
|
||||
{
|
||||
if (tile.WhichPiece == WhichPiece.King)
|
||||
{
|
||||
if (tile.Owner == WhichPlayer.Player1)
|
||||
{
|
||||
player1KingPosition = position;
|
||||
}
|
||||
else if (tile.Owner == WhichPlayer.Player2)
|
||||
{
|
||||
player2KingPosition = position;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move a piece from a board tile to another board tile.
|
||||
/// </summary>
|
||||
/// <param name="from">The position of the piece being moved expressed in board notation.</param>
|
||||
/// <param name="to">The target position expressed in board notation.</param>
|
||||
/// <returns>A <see cref="MoveResult" /> describing the success or failure of the simulation.</returns>
|
||||
public MoveResult Move(string from, string to, bool isPromotion = false)
|
||||
{
|
||||
var fromPiece = board[from];
|
||||
if (fromPiece == null)
|
||||
{
|
||||
return new MoveResult(false, $"Tile [{from}] is empty. There is no piece to move.");
|
||||
}
|
||||
if (fromPiece.Owner != board.WhoseTurn)
|
||||
{
|
||||
return new MoveResult(false, "Not allowed to move the opponents piece");
|
||||
}
|
||||
if (IsPathable(move.From.Value, move.To) == false)
|
||||
{
|
||||
return new MoveResult(false, $"Proposed move is not part of the move-set for piece {fromPiece.WhichPiece}.");
|
||||
}
|
||||
|
||||
var captured = board[to];
|
||||
if (captured != null)
|
||||
{
|
||||
if (captured.Owner == board.WhoseTurn)
|
||||
{
|
||||
return new MoveResult(false, "Capturing your own piece is not allowed.");
|
||||
}
|
||||
captured.Capture();
|
||||
board.Hand.Add(captured);
|
||||
}
|
||||
|
||||
//Mutate the board.
|
||||
if (isPromotion)
|
||||
{
|
||||
var fromVector = ShogiBoardState.FromBoardNotation(from);
|
||||
var toVector = ShogiBoardState.FromBoardNotation(to);
|
||||
if (board.WhoseTurn == WhichPlayer.Player1 && (toVector.Y > 5 || fromVector.Y > 5))
|
||||
{
|
||||
fromPiece.Promote();
|
||||
}
|
||||
else if (board.WhoseTurn == WhichPlayer.Player2 && (toVector.Y < 3 || fromVector.Y < 3))
|
||||
{
|
||||
fromPiece.Promote();
|
||||
}
|
||||
}
|
||||
board[to] = fromPiece;
|
||||
board[from] = null;
|
||||
if (fromPiece.WhichPiece == WhichPiece.King)
|
||||
{
|
||||
if (fromPiece.Owner == WhichPlayer.Player1)
|
||||
{
|
||||
player1King.X = move.To.X;
|
||||
player1King.Y = move.To.Y;
|
||||
}
|
||||
else if (fromPiece.Owner == WhichPlayer.Player2)
|
||||
{
|
||||
player2King.X = move.To.X;
|
||||
player2King.Y = move.To.Y;
|
||||
}
|
||||
}
|
||||
MoveHistory.Add(move);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move a piece from the hand to the board.
|
||||
/// </summary>
|
||||
/// <param name="pieceInHand"></param>
|
||||
/// <param name="to">The target position expressed in board notation.</param>
|
||||
/// <returns>A <see cref="MoveResult" /> describing the success or failure of the simulation.</returns>
|
||||
public void Move(WhichPiece pieceInHand, string to)
|
||||
{
|
||||
var index = Hand.FindIndex(p => p.WhichPiece == move.PieceFromHand);
|
||||
if (index == -1)
|
||||
{
|
||||
Error = $"{move.PieceFromHand} does not exist in the hand.";
|
||||
return false;
|
||||
}
|
||||
if (Board[move.To] != null)
|
||||
{
|
||||
Error = $"Illegal move - attempting to capture while playing a piece from the hand.";
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (move.PieceFromHand!.Value)
|
||||
{
|
||||
case WhichPiece.Knight:
|
||||
{
|
||||
// Knight cannot be placed onto the farthest two ranks from the hand.
|
||||
if ((WhoseTurn == WhichPlayer.Player1 && move.To.Y > 6)
|
||||
|| (WhoseTurn == WhichPlayer.Player2 && move.To.Y < 2))
|
||||
{
|
||||
Error = $"Knight has no valid moves after placed.";
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case WhichPiece.Lance:
|
||||
case WhichPiece.Pawn:
|
||||
{
|
||||
// Lance and Pawn cannot be placed onto the farthest rank from the hand.
|
||||
if ((WhoseTurn == WhichPlayer.Player1 && move.To.Y == 8)
|
||||
|| (WhoseTurn == WhichPlayer.Player2 && move.To.Y == 0))
|
||||
{
|
||||
Error = $"{move.PieceFromHand} has no valid moves after placed.";
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Mutate the board.
|
||||
Board[move.To] = Hand[index];
|
||||
Hand.RemoveAt(index);
|
||||
MoveHistory.Add(move);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool IsPathable(Vector2 from, Vector2 to)
|
||||
{
|
||||
var piece = Board[from];
|
||||
if (piece == null) return false;
|
||||
|
||||
var isObstructed = false;
|
||||
var isPathable = pathFinder.PathTo(from, to, (other, position) =>
|
||||
{
|
||||
if (other.Owner == piece.Owner) isObstructed = true;
|
||||
});
|
||||
return !isObstructed && isPathable;
|
||||
}
|
||||
|
||||
private bool EvaluateCheckAfterMove(Move move, WhichPlayer WhichPerspective)
|
||||
{
|
||||
if (WhichPerspective == InCheck) return true; // If we already know the player is in check, don't bother.
|
||||
|
||||
var isCheck = false;
|
||||
var kingPosition = WhichPerspective == WhichPlayer.Player1 ? player1King : player2King;
|
||||
|
||||
// Check if the move put the king in check.
|
||||
if (pathFinder.PathTo(move.To, kingPosition)) return true;
|
||||
|
||||
if (move.From.HasValue)
|
||||
{
|
||||
// Get line equation from king through the now-unoccupied location.
|
||||
var direction = Vector2.Subtract(kingPosition, move.From!.Value);
|
||||
var slope = Math.Abs(direction.Y / direction.X);
|
||||
// If absolute slope is 45°, look for a bishop along the line.
|
||||
// If absolute slope is 0° or 90°, look for a rook along the line.
|
||||
// if absolute slope is 0°, look for lance along the line.
|
||||
if (float.IsInfinity(slope))
|
||||
{
|
||||
// if slope of the move is also infinity...can skip this?
|
||||
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
|
||||
{
|
||||
if (piece.Owner != WhichPerspective)
|
||||
{
|
||||
switch (piece.WhichPiece)
|
||||
{
|
||||
case WhichPiece.Rook:
|
||||
isCheck = true;
|
||||
break;
|
||||
case WhichPiece.Lance:
|
||||
if (!piece.IsPromoted) isCheck = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (slope == 1)
|
||||
{
|
||||
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
|
||||
{
|
||||
if (piece.Owner != WhichPerspective && piece.WhichPiece == WhichPiece.Bishop)
|
||||
{
|
||||
isCheck = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (slope == 0)
|
||||
{
|
||||
pathFinder.LinePathTo(kingPosition, direction, (piece, position) =>
|
||||
{
|
||||
if (piece.Owner != WhichPerspective && piece.WhichPiece == WhichPiece.Rook)
|
||||
{
|
||||
isCheck = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: Check for illegal move from hand. It is illegal to place from the hand such that you check-mate your opponent.
|
||||
// Go read the shogi rules to be sure this is true.
|
||||
}
|
||||
|
||||
return isCheck;
|
||||
}
|
||||
|
||||
private bool EvaluateCheckmate()
|
||||
{
|
||||
if (!InCheck.HasValue) return false;
|
||||
|
||||
// Assume true and try to disprove.
|
||||
var isCheckmate = true;
|
||||
Board.ForEachNotNull((piece, from) => // For each piece...
|
||||
{
|
||||
// Short circuit
|
||||
if (!isCheckmate) return;
|
||||
|
||||
if (piece.Owner == InCheck) // ...owned by the player in check...
|
||||
{
|
||||
// ...evaluate if any move gets the player out of check.
|
||||
pathFinder.PathEvery(from, (other, position) =>
|
||||
{
|
||||
var simulationBoard = new Shogi(this);
|
||||
var moveToTry = new Move(from, position);
|
||||
var moveSuccess = simulationBoard.TryMove(moveToTry);
|
||||
if (moveSuccess)
|
||||
{
|
||||
if (!EvaluateCheckAfterMove(moveToTry, InCheck.Value))
|
||||
{
|
||||
isCheckmate = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
return isCheckmate;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Shogi.Domain/WhichPiece.cs
Normal file
14
Shogi.Domain/WhichPiece.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Shogi.Domain
|
||||
{
|
||||
public enum WhichPiece
|
||||
{
|
||||
King,
|
||||
GoldGeneral,
|
||||
SilverGeneral,
|
||||
Bishop,
|
||||
Rook,
|
||||
Knight,
|
||||
Lance,
|
||||
Pawn
|
||||
}
|
||||
}
|
||||
8
Shogi.Domain/WhichPlayer.cs
Normal file
8
Shogi.Domain/WhichPlayer.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Shogi.Domain
|
||||
{
|
||||
public enum WhichPlayer
|
||||
{
|
||||
Player1,
|
||||
Player2
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user