checkpoint
This commit is contained in:
@@ -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
|
||||
{
|
||||
internal class StandardRules
|
||||
@@ -130,7 +129,7 @@ namespace Shogi.Domain
|
||||
var direction = Vector2.Subtract(kingPosition, boardState.PreviousMoveFrom);
|
||||
var slope = Math.Abs(direction.Y / direction.X);
|
||||
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 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.
|
||||
@@ -173,7 +172,7 @@ namespace Shogi.Domain
|
||||
|
||||
var kingPosition = previousMovedPiece.Owner == WhichPlayer.Player1 ? boardState.Player2KingPosition : boardState.Player1KingPosition;
|
||||
var path = previousMovedPiece.GetPathFromStartToEnd(position, kingPosition);
|
||||
var threatenedPiece = boardState.GetFirstPieceAlongPath(path);
|
||||
var threatenedPiece = boardState.QueryFirstPieceInPath(path);
|
||||
if (!path.Any() || threatenedPiece == null) return false;
|
||||
|
||||
return threatenedPiece.WhichPiece == WhichPiece.King;
|
||||
@@ -183,32 +182,44 @@ namespace Shogi.Domain
|
||||
{
|
||||
if (!boardState.InCheck.HasValue) return false;
|
||||
|
||||
|
||||
// Get all pieces from "other player" who threaten the king in question.
|
||||
var otherPlayer = whichPlayer == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
||||
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.
|
||||
* Foreach piece owned by whichPlayer
|
||||
* if piece can intercept check, return false;
|
||||
*/
|
||||
var threat = tilesOccupiedByOtherPlayer.Single();
|
||||
var kingPosition = whichPlayer == WhichPlayer.Player1
|
||||
? boardState.Player1KingPosition
|
||||
: boardState.Player2KingPosition;
|
||||
var tiles = boardState.GetTilesOccupiedBy(whichPlayer);
|
||||
var line = Vector2.Subtract(kingPosition, threat.Position);
|
||||
var slope = line.Y / line.X;
|
||||
foreach (var tile in tiles)
|
||||
var threat = threats.Single();
|
||||
var pathFromThreatToKing = threat.Value.GetPathFromStartToEnd(threat.Key, kingPosition);
|
||||
var tilesThatCouldBlockTheThreat = boardState.GetTilesOccupiedBy(whichPlayer);
|
||||
foreach (var threatBlockingPosition in pathFromThreatToKing)
|
||||
{
|
||||
// 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 + )
|
||||
}
|
||||
var tilesThatDoBlockThreat = tilesThatCouldBlockTheThreat
|
||||
.Where(tile => PieceHasLineOfSight(tile, threatBlockingPosition))
|
||||
.ToList();
|
||||
|
||||
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.
|
||||
@@ -217,34 +228,15 @@ namespace Shogi.Domain
|
||||
* Foreach piece owned by "other player", check if piece threatens king position.
|
||||
*/
|
||||
|
||||
//
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//foreach (var kvp in boardState)
|
||||
//{
|
||||
// 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)
|
||||
// {
|
||||
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
private bool PieceHasLineOfSight(BoardTile tile, Vector2 lineOfSightTarget)
|
||||
{
|
||||
var path = tile.Value.GetPathFromStartToEnd(tile.Key, lineOfSightTarget);
|
||||
return path
|
||||
.SkipLast(1)
|
||||
.All(position => this.boardState[ShogiBoardState.ToBoardNotation(position)] == null);
|
||||
}
|
||||
|
||||
public bool EvaluateCheckmate_Old()
|
||||
@@ -253,31 +245,31 @@ namespace Shogi.Domain
|
||||
|
||||
// Assume true and try to disprove.
|
||||
var isCheckmate = true;
|
||||
boardState.ForEachNotNull((piece, from) => // For each piece...
|
||||
{
|
||||
// Short circuit
|
||||
if (!isCheckmate) return;
|
||||
//boardState.ForEachNotNull((piece, from) => // For each piece...
|
||||
//{
|
||||
// Short circuit
|
||||
//if (!isCheckmate) return;
|
||||
|
||||
if (piece.Owner == boardState.InCheck) // ...owned by the player in check...
|
||||
{
|
||||
// ...evaluate if any move gets the player out of check.
|
||||
//PathEvery(from, (other, position) =>
|
||||
//{
|
||||
// var simulationBoard = new StandardRules(new ShogiBoardState(board));
|
||||
// var fromNotation = ShogiBoardState.ToBoardNotation(from);
|
||||
// var toNotation = ShogiBoardState.ToBoardNotation(position);
|
||||
// var simulationResult = simulationBoard.Move(fromNotation, toNotation, false);
|
||||
// if (simulationResult.Success)
|
||||
// {
|
||||
// //if (!IsPlayerInCheckAfterMove(from, position, board.InCheck.Value))
|
||||
// //{
|
||||
// // isCheckmate = false;
|
||||
// //}
|
||||
// }
|
||||
//});
|
||||
}
|
||||
// TODO: Assert that a player could not place a piece from their hand to avoid check.
|
||||
});
|
||||
//if (piece.Owner == boardState.InCheck) // ...owned by the player in check...
|
||||
//{
|
||||
// ...evaluate if any move gets the player out of check.
|
||||
//PathEvery(from, (other, position) =>
|
||||
//{
|
||||
// var simulationBoard = new StandardRules(new ShogiBoardState(board));
|
||||
// var fromNotation = ShogiBoardState.ToBoardNotation(from);
|
||||
// var toNotation = ShogiBoardState.ToBoardNotation(position);
|
||||
// var simulationResult = simulationBoard.Move(fromNotation, toNotation, false);
|
||||
// if (simulationResult.Success)
|
||||
// {
|
||||
// //if (!IsPlayerInCheckAfterMove(from, position, board.InCheck.Value))
|
||||
// //{
|
||||
// // isCheckmate = false;
|
||||
// //}
|
||||
// }
|
||||
//});
|
||||
//}
|
||||
// TODO: Assert that a player could not place a piece from their hand to avoid check.
|
||||
//});
|
||||
return isCheckmate;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user