Organize domain project.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using Shogi.Domain.Pathing;
|
||||
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Shogi.Domain.ValueObjects
|
||||
|
||||
252
Shogi.Domain/ValueObjects/BoardState.cs
Normal file
252
Shogi.Domain/ValueObjects/BoardState.cs
Normal 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)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using Shogi.Domain.Pathing;
|
||||
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Shogi.Domain.ValueObjects
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Shogi.Domain.Pathing;
|
||||
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Shogi.Domain.ValueObjects
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Shogi.Domain.Pathing;
|
||||
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Shogi.Domain.ValueObjects
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Shogi.Domain.Pathing;
|
||||
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Shogi.Domain.ValueObjects
|
||||
|
||||
14
Shogi.Domain/ValueObjects/MoveResult.cs
Normal file
14
Shogi.Domain/ValueObjects/MoveResult.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using Shogi.Domain.Pathing;
|
||||
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Shogi.Domain.ValueObjects
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Shogi.Domain.Pathing;
|
||||
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Shogi.Domain.ValueObjects
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Shogi.Domain.Pathing;
|
||||
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Shogi.Domain.ValueObjects;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Text;
|
||||
using Shogi.Domain.YetToBeAssimilatedIntoDDD;
|
||||
|
||||
namespace Shogi.Domain.ValueObjects;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Shogi.Domain.Pathing;
|
||||
using Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Shogi.Domain.ValueObjects
|
||||
|
||||
263
Shogi.Domain/ValueObjects/StandardRules.cs
Normal file
263
Shogi.Domain/ValueObjects/StandardRules.cs
Normal file
@@ -0,0 +1,263 @@
|
||||
using Shogi.Domain.YetToBeAssimilatedIntoDDD;
|
||||
using BoardTile = System.Collections.Generic.KeyValuePair<System.Numerics.Vector2, Shogi.Domain.ValueObjects.Piece>;
|
||||
|
||||
namespace Shogi.Domain.ValueObjects
|
||||
{
|
||||
internal class StandardRules
|
||||
{
|
||||
private readonly BoardState boardState;
|
||||
|
||||
internal StandardRules(BoardState board)
|
||||
{
|
||||
boardState = board;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move a piece from a board tile to another board tile ignorant of check or check-mate.
|
||||
/// </summary>
|
||||
/// <param name="fromNotation">The position of the piece being moved expressed in board notation.</param>
|
||||
/// <param name="toNotation">The target position expressed in board notation.</param>
|
||||
/// <param name="isPromotionRequested">True if a promotion is expected as a result of this move.</param>
|
||||
/// <returns>A <see cref="MoveResult" /> describing the success or failure of the move.</returns>
|
||||
internal MoveResult Move(string fromNotation, string toNotation, bool isPromotionRequested = false)
|
||||
{
|
||||
var from = Notation.FromBoardNotation(fromNotation);
|
||||
var to = Notation.FromBoardNotation(toNotation);
|
||||
var fromPiece = boardState[from];
|
||||
if (fromPiece == null)
|
||||
{
|
||||
return new MoveResult(false, $"Tile [{fromNotation}] is empty. There is no piece to move.");
|
||||
}
|
||||
|
||||
if (fromPiece.Owner != boardState.WhoseTurn)
|
||||
{
|
||||
return new MoveResult(false, "Not allowed to move the opponents piece");
|
||||
}
|
||||
|
||||
var path = fromPiece.GetPathFromStartToEnd(from, to);
|
||||
|
||||
if (boardState.IsPathBlocked(path))
|
||||
{
|
||||
return new MoveResult(false, "Another piece obstructs the desired move.");
|
||||
}
|
||||
|
||||
if (boardState[to] != null)
|
||||
{
|
||||
boardState.Capture(to);
|
||||
}
|
||||
|
||||
if (isPromotionRequested && (boardState.IsWithinPromotionZone(to) || boardState.IsWithinPromotionZone(from)))
|
||||
{
|
||||
fromPiece.Promote();
|
||||
}
|
||||
|
||||
boardState[to] = fromPiece;
|
||||
boardState[from] = null;
|
||||
|
||||
boardState.PreviousMove = new Move(from, to);
|
||||
var otherPlayer = boardState.WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
||||
boardState.WhoseTurn = otherPlayer;
|
||||
|
||||
return new MoveResult(true);
|
||||
}
|
||||
|
||||
/// Move a piece from the hand to the board ignorant if check or check-mate.
|
||||
/// </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>
|
||||
internal MoveResult Move(WhichPiece pieceInHand, string toNotation)
|
||||
{
|
||||
var to = Notation.FromBoardNotation(toNotation);
|
||||
var index = boardState.ActivePlayerHand.FindIndex(p => p.WhichPiece == pieceInHand);
|
||||
if (index == -1)
|
||||
{
|
||||
return new MoveResult(false, $"{pieceInHand} does not exist in the hand.");
|
||||
}
|
||||
if (boardState[to] != null)
|
||||
{
|
||||
return new MoveResult(false, "Illegal move - attempting to capture while playing a piece from the hand.");
|
||||
}
|
||||
|
||||
switch (pieceInHand)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return new MoveResult(false, "Knight has no valid moves after placed.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
case WhichPiece.Lance:
|
||||
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)
|
||||
{
|
||||
return new MoveResult(false, $"{pieceInHand} has no valid moves after placed.");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Mutate the board.
|
||||
boardState[to] = boardState.ActivePlayerHand[index];
|
||||
boardState.ActivePlayerHand.RemoveAt(index);
|
||||
boardState.PreviousMove = new Move(pieceInHand, to);
|
||||
return new MoveResult(true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the last move put the player who moved in check.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This strategy recognizes that a "discover check" could only occur from a subset of pieces: Rook, Bishop, Lance.
|
||||
/// In this way, only those pieces need to be considered when evaluating if a move placed the moving player in check.
|
||||
/// </remarks>
|
||||
internal bool DidPlayerPutThemselfInCheck()
|
||||
{
|
||||
if (boardState.PreviousMove.From == null)
|
||||
{
|
||||
// You can't place yourself in check by placing a piece from your hand.
|
||||
return false;
|
||||
}
|
||||
|
||||
var previousMovedPiece = boardState[boardState.PreviousMove.To];
|
||||
if (previousMovedPiece == null) throw new ArgumentNullException(nameof(previousMovedPiece), $"No piece exists at position {boardState.PreviousMove.To}.");
|
||||
var kingPosition = previousMovedPiece.Owner == WhichPlayer.Player1 ? boardState.Player1KingPosition : boardState.Player2KingPosition;
|
||||
|
||||
|
||||
var isDiscoverCheck = false;
|
||||
// Get line equation from king through the now-unoccupied location.
|
||||
var direction = Vector2.Subtract(kingPosition, boardState.PreviousMove.From.Value);
|
||||
var slope = Math.Abs(direction.Y / direction.X);
|
||||
var path = BoardState.GetPathAlongDirectionFromStartToEdgeOfBoard(boardState.PreviousMove.From.Value, Vector2.Normalize(direction));
|
||||
var threat = boardState.QueryFirstPieceInPath(path);
|
||||
if (threat == null || threat.Owner == previousMovedPiece.Owner) return false;
|
||||
// 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))
|
||||
{
|
||||
isDiscoverCheck = threat.WhichPiece switch
|
||||
{
|
||||
WhichPiece.Lance => !threat.IsPromoted,
|
||||
WhichPiece.Rook => true,
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
else if (slope == 1)
|
||||
{
|
||||
isDiscoverCheck = threat.WhichPiece switch
|
||||
{
|
||||
WhichPiece.Bishop => true,
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
else if (slope == 0)
|
||||
{
|
||||
isDiscoverCheck = threat.WhichPiece switch
|
||||
{
|
||||
WhichPiece.Rook => true,
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
return isDiscoverCheck;
|
||||
}
|
||||
|
||||
internal bool IsOpponentInCheckAfterMove() => IsOpposingKingThreatenedByPosition(boardState.PreviousMove.To);
|
||||
|
||||
internal bool IsOpposingKingThreatenedByPosition(Vector2 position)
|
||||
{
|
||||
var previousMovedPiece = boardState[position];
|
||||
if (previousMovedPiece == null) return false;
|
||||
|
||||
var kingPosition = previousMovedPiece.Owner == WhichPlayer.Player1 ? boardState.Player2KingPosition : boardState.Player1KingPosition;
|
||||
var path = previousMovedPiece.GetPathFromStartToEnd(position, kingPosition);
|
||||
var threatenedPiece = boardState.QueryFirstPieceInPath(path);
|
||||
if (!path.Any() || threatenedPiece == null) return false;
|
||||
|
||||
return threatenedPiece.WhichPiece == WhichPiece.King;
|
||||
}
|
||||
|
||||
internal bool IsOpponentInCheckMate()
|
||||
{
|
||||
// Assume checkmate, then try to disprove.
|
||||
if (!boardState.InCheck.HasValue) return false;
|
||||
// Get all pieces from opponent who threaten the king in question.
|
||||
var opponent = boardState.WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
||||
var tilesOccupiedByOpponent = boardState.GetTilesOccupiedBy(opponent);
|
||||
var kingPosition = boardState.WhoseTurn == WhichPlayer.Player1
|
||||
? boardState.Player1KingPosition
|
||||
: boardState.Player2KingPosition;
|
||||
var threats = tilesOccupiedByOpponent.Where(tile => PieceHasLineOfSight(tile, kingPosition)).ToList();
|
||||
if (threats.Count == 1)
|
||||
{
|
||||
/* If there is exactly one threat it is possible to block the check.
|
||||
* Foreach piece owned by whichPlayer
|
||||
* if piece can intercept check, return false;
|
||||
*/
|
||||
var threat = threats.Single();
|
||||
var pathFromThreatToKing = threat.Value.GetPathFromStartToEnd(threat.Key, kingPosition);
|
||||
var tilesThatCouldBlockTheThreat = boardState.GetTilesOccupiedBy(boardState.WhoseTurn);
|
||||
foreach (var threatBlockingPosition in pathFromThreatToKing)
|
||||
{
|
||||
var tilesThatDoBlockThreat = tilesThatCouldBlockTheThreat
|
||||
.Where(tile => PieceHasLineOfSight(tile, threatBlockingPosition))
|
||||
.ToList();
|
||||
|
||||
if (tilesThatDoBlockThreat.Any())
|
||||
{
|
||||
return false; // Cannot be check-mate if a piece can intercept the threat.
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If no ability to block the check, maybe the king can evade check by moving.
|
||||
*/
|
||||
|
||||
foreach (var maybeSafePosition in GetPossiblePositionsForKing(boardState.WhoseTurn))
|
||||
{
|
||||
threats = tilesOccupiedByOpponent
|
||||
.Where(tile => PieceHasLineOfSight(tile, maybeSafePosition))
|
||||
.ToList();
|
||||
if (!threats.Any())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private IList<Vector2> GetPossiblePositionsForKing(WhichPlayer whichPlayer)
|
||||
{
|
||||
var kingPosition = whichPlayer == WhichPlayer.Player1
|
||||
? boardState.Player1KingPosition
|
||||
: boardState.Player2KingPosition;
|
||||
|
||||
return King.KingPaths
|
||||
.Select(path => path.Direction + kingPosition)
|
||||
.Where(newPosition => newPosition.IsInsideBoardBoundary())
|
||||
// Where tile at position is empty, meaning the king could move there.
|
||||
.Where(newPosition => boardState[newPosition] == null)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private bool PieceHasLineOfSight(BoardTile tile, Vector2 lineOfSightTarget)
|
||||
{
|
||||
var path = tile.Value.GetPathFromStartToEnd(tile.Key, lineOfSightTarget);
|
||||
return path
|
||||
.SkipLast(1)
|
||||
.All(position => boardState[Notation.ToBoardNotation(position)] == null);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Shogi.Domain/ValueObjects/WhichPiece.cs
Normal file
14
Shogi.Domain/ValueObjects/WhichPiece.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Shogi.Domain.ValueObjects
|
||||
{
|
||||
public enum WhichPiece
|
||||
{
|
||||
King,
|
||||
GoldGeneral,
|
||||
SilverGeneral,
|
||||
Bishop,
|
||||
Rook,
|
||||
Knight,
|
||||
Lance,
|
||||
Pawn
|
||||
}
|
||||
}
|
||||
8
Shogi.Domain/ValueObjects/WhichPlayer.cs
Normal file
8
Shogi.Domain/ValueObjects/WhichPlayer.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Shogi.Domain.ValueObjects
|
||||
{
|
||||
public enum WhichPlayer
|
||||
{
|
||||
Player1,
|
||||
Player2
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user