checkpoint

This commit is contained in:
2022-06-07 20:29:04 -05:00
parent b3e0d154bd
commit 3a9a627e0d
8 changed files with 105 additions and 166 deletions

View File

@@ -1,11 +1,10 @@
using Gameboard.ShogiUI.Sockets.ServiceModels.Types; using Gameboard.ShogiUI.Sockets.ServiceModels.Types;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Domain = Shogi.Domain;
namespace Gameboard.ShogiUI.Sockets.Extensions namespace Gameboard.ShogiUI.Sockets.Extensions
{ {
public static class ModelExtensions public static class ModelExtensions
{ {
public static string GetShortName(this Models.Piece self) public static string GetShortName(this Models.Piece self)
{ {

View File

@@ -24,9 +24,7 @@ using Newtonsoft.Json.Serialization;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security.Claims;
using System.Text; using System.Text;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Sockets namespace Gameboard.ShogiUI.Sockets
{ {

View File

@@ -1,17 +0,0 @@
using Shogi.Domain.Pieces;
namespace Shogi.Domain
{
internal class BoardTile
{
public BoardTile(Piece piece, Vector2 position)
{
Piece = piece;
Position = position;
}
public Piece Piece { get; }
public Vector2 Position { get; }
}
}

View File

@@ -0,0 +1,10 @@
using Shogi.Domain.Pieces;
namespace Shogi.Domain
{
internal static class DomainExtensions
{
public static bool IsKing(this Piece self) => self.WhichPiece == WhichPiece.King;
}
}

View File

@@ -55,6 +55,13 @@ namespace Shogi.Domain.Pieces
IsPromoted = false; IsPromoted = false;
} }
/// <summary>
/// Respecting the move-set of the Piece, collect all positions from start to end.
/// Useful if you need to iterate a move-set.
/// </summary>
/// <param name="start"></param>
/// <param name="end"></param>
/// <returns>An empty list if the piece cannot legally traverse from start to end. Otherwise, a list of positions.</returns>
public IEnumerable<Vector2> GetPathFromStartToEnd(Vector2 start, Vector2 end) public IEnumerable<Vector2> GetPathFromStartToEnd(Vector2 start, Vector2 end)
{ {
var steps = new List<Vector2>(10); var steps = new List<Vector2>(10);

View File

@@ -1,5 +1,4 @@
using Shogi.Domain.Pieces; using Shogi.Domain.Pieces;
using System;
using System.Text; using System.Text;
namespace Shogi.Domain namespace Shogi.Domain
@@ -20,8 +19,8 @@ namespace Shogi.Domain
public Shogi(ShogiBoardState board) public Shogi(ShogiBoardState board)
{ {
this.rules = new StandardRules(board);
this.boardState = board; this.boardState = board;
rules = new StandardRules(this.boardState);
} }
/// <summary> /// <summary>

View File

@@ -1,5 +1,6 @@
using Shogi.Domain.Pieces; using Shogi.Domain.Pieces;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using BoardTile = System.Collections.Generic.KeyValuePair<System.Numerics.Vector2, Shogi.Domain.Pieces.Piece>;
namespace Shogi.Domain namespace Shogi.Domain
{ {
@@ -15,25 +16,6 @@ namespace Shogi.Domain
/// </summary> /// </summary>
private readonly Dictionary<string, Piece?> board; private readonly Dictionary<string, Piece?> board;
public List<Piece> ActivePlayerHand => WhoseTurn == WhichPlayer.Player1 ? Player1Hand : Player2Hand;
/// <summary>
/// "Active Player" means the player whose turn it is.
/// </summary>
public Vector2 ActivePlayerKingPosition => WhoseTurn == WhichPlayer.Player1 ? Player1KingPosition : Player2KingPosition;
/// <summary>
/// "Opposing Player" means the player whose turn it isn't.
/// </summary>
public Vector2 OpposingPlayerKingPosition => WhoseTurn == WhichPlayer.Player1 ? Player2KingPosition : Player1KingPosition;
public Vector2 Player1KingPosition { get; set; }
public Vector2 Player2KingPosition { get; set; }
public List<Piece> Player1Hand { get; }
public List<Piece> Player2Hand { get; }
public Vector2 PreviousMoveFrom { get; private set; }
public Vector2 PreviousMoveTo { get; private set; }
public WhichPlayer WhoseTurn { get; set; }
public WhichPlayer? InCheck { get; set; }
public bool IsCheckmate { get; set; }
public ShogiBoardState() public ShogiBoardState()
{ {
board = new Dictionary<string, Piece?>(81, StringComparer.OrdinalIgnoreCase); board = new Dictionary<string, Piece?>(81, StringComparer.OrdinalIgnoreCase);
@@ -41,9 +23,26 @@ namespace Shogi.Domain
Player1Hand = new List<Piece>(); Player1Hand = new List<Piece>();
Player2Hand = new List<Piece>(); Player2Hand = new List<Piece>();
PreviousMoveTo = Vector2.Zero; PreviousMoveTo = Vector2.Zero;
CacheKingPositions();
} }
public List<Piece> ActivePlayerHand => WhoseTurn == WhichPlayer.Player1 ? Player1Hand : Player2Hand;
public Vector2 Player1KingPosition => 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 => 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 Vector2 PreviousMoveFrom { get; private set; }
public Vector2 PreviousMoveTo { get; private set; }
public WhichPlayer WhoseTurn { get; set; }
public WhichPlayer? InCheck { get; set; }
public bool IsCheckmate { get; set; }
/// <summary> /// <summary>
/// Copy constructor. /// Copy constructor.
@@ -61,30 +60,13 @@ namespace Shogi.Domain
PreviousMoveTo = other.PreviousMoveTo; PreviousMoveTo = other.PreviousMoveTo;
Player1Hand.AddRange(other.Player1Hand); Player1Hand.AddRange(other.Player1Hand);
Player2Hand.AddRange(other.Player2Hand); Player2Hand.AddRange(other.Player2Hand);
Player1KingPosition = other.Player1KingPosition;
Player2KingPosition = other.Player2KingPosition;
} }
public Piece? this[string notation] public Piece? this[string notation]
{ {
// TODO: Validate "notation" here and throw an exception if invalid. // TODO: Validate "notation" here and throw an exception if invalid.
get => board[notation]; get => board[notation];
set set => board[notation] = value;
{
if (value?.WhichPiece == WhichPiece.King)
{
if (value.Owner == WhichPlayer.Player1)
{
// TODO: This FromBoardNotation() is a waste if the Vector2 indexer was called. :(
Player1KingPosition = FromBoardNotation(notation);
}
else if (value.Owner == WhichPlayer.Player2)
{
Player2KingPosition = FromBoardNotation(notation);
}
}
board[notation] = value;
}
} }
public Piece? this[Vector2 vector] public Piece? this[Vector2 vector]
@@ -112,21 +94,7 @@ namespace Shogi.Domain
{ {
return !path.Any() return !path.Any()
|| path.SkipLast(1).Any(position => this[position] != null) || path.SkipLast(1).Any(position => this[position] != null)
|| this[path.Last()]?.Owner == WhoseTurn; || this[path.Last()]?.Owner == WhoseTurn;
}
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);
}
}
} }
internal bool IsWithinPromotionZone(Vector2 position) internal bool IsWithinPromotionZone(Vector2 position)
@@ -141,9 +109,10 @@ namespace Shogi.Domain
&& position.Y <= 8 && position.Y >= 0; && position.Y <= 8 && position.Y >= 0;
} }
internal IEnumerable<BoardTile> GetTilesOccupiedBy(WhichPlayer whichPlayer) => board internal List<BoardTile> GetTilesOccupiedBy(WhichPlayer whichPlayer) => board
.Where(kvp => kvp.Value?.Owner == whichPlayer) .Where(kvp => kvp.Value?.Owner == whichPlayer)
.Select(kvp => new BoardTile(kvp.Value!, FromBoardNotation(kvp.Key))); .Select(kvp => new BoardTile(FromBoardNotation(kvp.Key), kvp.Value!))
.ToList();
internal void Capture(Vector2 to) internal void Capture(Vector2 to)
{ {
@@ -167,7 +136,7 @@ namespace Shogi.Domain
} }
} }
internal Piece? GetFirstPieceAlongPath(IEnumerable<Vector2> path) internal Piece? QueryFirstPieceInPath(IEnumerable<Vector2> path)
{ {
foreach (var step in path) foreach (var step in path)
{ {
@@ -197,24 +166,6 @@ namespace Shogi.Domain
throw new ArgumentException($"Board notation not recognized. Notation given: {notation}"); throw new ArgumentException($"Board notation not recognized. Notation given: {notation}");
} }
private void CacheKingPositions()
{
ForEachNotNull((tile, position) =>
{
if (tile.WhichPiece == WhichPiece.King)
{
if (tile.Owner == WhichPlayer.Player1)
{
Player1KingPosition = position;
}
else if (tile.Owner == WhichPlayer.Player2)
{
Player2KingPosition = position;
}
}
});
}
private void InitializeBoardState() private void InitializeBoardState()
{ {
this["A1"] = new Lance(WhichPlayer.Player1); this["A1"] = new Lance(WhichPlayer.Player1);

View File

@@ -1,6 +1,5 @@
using System.Runtime.CompilerServices; using BoardTile = System.Collections.Generic.KeyValuePair<System.Numerics.Vector2, Shogi.Domain.Pieces.Piece>;
[assembly: InternalsVisibleTo("Shogi.Domain.UnitTests")]
namespace Shogi.Domain namespace Shogi.Domain
{ {
internal class StandardRules internal class StandardRules
@@ -130,7 +129,7 @@ namespace Shogi.Domain
var direction = Vector2.Subtract(kingPosition, boardState.PreviousMoveFrom); var direction = Vector2.Subtract(kingPosition, boardState.PreviousMoveFrom);
var slope = Math.Abs(direction.Y / direction.X); var slope = Math.Abs(direction.Y / direction.X);
var path = ShogiBoardState.GetPathAlongDirectionFromStartToEdgeOfBoard(boardState.PreviousMoveFrom, Vector2.Normalize(direction)); var path = ShogiBoardState.GetPathAlongDirectionFromStartToEdgeOfBoard(boardState.PreviousMoveFrom, Vector2.Normalize(direction));
var threat = boardState.GetFirstPieceAlongPath(path); var threat = boardState.QueryFirstPieceInPath(path);
if (threat == null || threat.Owner == previousMovedPiece.Owner) return false; 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 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° or 90°, look for a rook along the line.
@@ -173,7 +172,7 @@ namespace Shogi.Domain
var kingPosition = previousMovedPiece.Owner == WhichPlayer.Player1 ? boardState.Player2KingPosition : boardState.Player1KingPosition; var kingPosition = previousMovedPiece.Owner == WhichPlayer.Player1 ? boardState.Player2KingPosition : boardState.Player1KingPosition;
var path = previousMovedPiece.GetPathFromStartToEnd(position, kingPosition); var path = previousMovedPiece.GetPathFromStartToEnd(position, kingPosition);
var threatenedPiece = boardState.GetFirstPieceAlongPath(path); var threatenedPiece = boardState.QueryFirstPieceInPath(path);
if (!path.Any() || threatenedPiece == null) return false; if (!path.Any() || threatenedPiece == null) return false;
return threatenedPiece.WhichPiece == WhichPiece.King; return threatenedPiece.WhichPiece == WhichPiece.King;
@@ -183,32 +182,44 @@ namespace Shogi.Domain
{ {
if (!boardState.InCheck.HasValue) return false; if (!boardState.InCheck.HasValue) return false;
// Get all pieces from "other player" who threaten the king in question. // Get all pieces from "other player" who threaten the king in question.
var otherPlayer = whichPlayer == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1; var otherPlayer = whichPlayer == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
var tilesOccupiedByOtherPlayer = boardState.GetTilesOccupiedBy(otherPlayer); var tilesOccupiedByOtherPlayer = boardState.GetTilesOccupiedBy(otherPlayer);
var kingPosition = whichPlayer == WhichPlayer.Player1
? boardState.Player1KingPosition
: boardState.Player2KingPosition;
var threats = tilesOccupiedByOtherPlayer.Where(tile => PieceHasLineOfSight(tile, kingPosition)).ToList();
if (tilesOccupiedByOtherPlayer.SingleOrDefault() != default)
if (threats.Count == 1)
{ {
/* If there is exactly one threat it is possible to block the check. /* If there is exactly one threat it is possible to block the check.
* Foreach piece owned by whichPlayer * Foreach piece owned by whichPlayer
* if piece can intercept check, return false; * if piece can intercept check, return false;
*/ */
var threat = tilesOccupiedByOtherPlayer.Single(); var threat = threats.Single();
var kingPosition = whichPlayer == WhichPlayer.Player1 var pathFromThreatToKing = threat.Value.GetPathFromStartToEnd(threat.Key, kingPosition);
? boardState.Player1KingPosition var tilesThatCouldBlockTheThreat = boardState.GetTilesOccupiedBy(whichPlayer);
: boardState.Player2KingPosition; foreach (var threatBlockingPosition in pathFromThreatToKing)
var tiles = boardState.GetTilesOccupiedBy(whichPlayer);
var line = Vector2.Subtract(kingPosition, threat.Position);
var slope = line.Y / line.X;
foreach (var tile in tiles)
{ {
// y = mx + b; slope intercept var tilesThatDoBlockThreat = tilesThatCouldBlockTheThreat
// b = -mx + y; .Where(tile => PieceHasLineOfSight(tile, threatBlockingPosition))
var b = -slope * tile.Position.X + tile.Position.Y; .ToList();
//if (tile.Position.Y = slope * tile.Position.X + )
}
if (tilesThatCouldBlockTheThreat.Any())
{
return false; // Cannot be check-mate if a piece can intercept the threat.
}
}
//var line = Vector2.Subtract(kingPosition, threat.Position);
//var slope = line.Y / line.X;
//foreach (var tile in tilesThatCouldBlockTheThreat)
//{
// // y = mx + b; slope intercept
// // b = -mx + y;
// var b = -slope * tile.Position.X + tile.Position.Y;
// //if (tile.Position.Y = slope * tile.Position.X + )
//}
} }
/* If no ability to block the check, maybe the king can evade check by moving. /* If no ability to block the check, maybe the king can evade check by moving.
@@ -217,34 +228,15 @@ namespace Shogi.Domain
* Foreach piece owned by "other player", check if piece threatens king position. * Foreach piece owned by "other player", check if piece threatens king position.
*/ */
//
return false; return false;
}
private bool PieceHasLineOfSight(BoardTile tile, Vector2 lineOfSightTarget)
{
var path = tile.Value.GetPathFromStartToEnd(tile.Key, lineOfSightTarget);
return path
.SkipLast(1)
//foreach (var kvp in boardState) .All(position => this.boardState[ShogiBoardState.ToBoardNotation(position)] == null);
//{
// if (kvp.Value == null) continue;
// var position = ShogiBoardState.FromBoardNotation(kvp.Key);
// var piece = kvp.Value;
// foreach (var path in piece.MoveSet)
// {
// if (path.Distance == Distance.OneStep)
// {
// var move = path.Direction + position;
// var simulationState = new ShogiBoardState(boardState);
// var simulation = new Shogi(simulationState);
// simulation.Move(position, move);
// }
// else if (path.Distance == Distance.MultiStep)
// {
// }
// }
//}
} }
public bool EvaluateCheckmate_Old() public bool EvaluateCheckmate_Old()
@@ -253,31 +245,31 @@ namespace Shogi.Domain
// Assume true and try to disprove. // Assume true and try to disprove.
var isCheckmate = true; var isCheckmate = true;
boardState.ForEachNotNull((piece, from) => // For each piece... //boardState.ForEachNotNull((piece, from) => // For each piece...
{ //{
// Short circuit // Short circuit
if (!isCheckmate) return; //if (!isCheckmate) return;
if (piece.Owner == boardState.InCheck) // ...owned by the player in check... //if (piece.Owner == boardState.InCheck) // ...owned by the player in check...
{ //{
// ...evaluate if any move gets the player out of check. // ...evaluate if any move gets the player out of check.
//PathEvery(from, (other, position) => //PathEvery(from, (other, position) =>
//{ //{
// var simulationBoard = new StandardRules(new ShogiBoardState(board)); // var simulationBoard = new StandardRules(new ShogiBoardState(board));
// var fromNotation = ShogiBoardState.ToBoardNotation(from); // var fromNotation = ShogiBoardState.ToBoardNotation(from);
// var toNotation = ShogiBoardState.ToBoardNotation(position); // var toNotation = ShogiBoardState.ToBoardNotation(position);
// var simulationResult = simulationBoard.Move(fromNotation, toNotation, false); // var simulationResult = simulationBoard.Move(fromNotation, toNotation, false);
// if (simulationResult.Success) // if (simulationResult.Success)
// { // {
// //if (!IsPlayerInCheckAfterMove(from, position, board.InCheck.Value)) // //if (!IsPlayerInCheckAfterMove(from, position, board.InCheck.Value))
// //{ // //{
// // isCheckmate = false; // // isCheckmate = false;
// //} // //}
// } // }
//}); //});
} //}
// TODO: Assert that a player could not place a piece from their hand to avoid check. // TODO: Assert that a player could not place a piece from their hand to avoid check.
}); //});
return isCheckmate; return isCheckmate;
} }
} }