checkmate complete
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Shogi.Domain.Pathing;
|
||||
using Shogi.Domain.Pieces;
|
||||
using BoardTile = System.Collections.Generic.KeyValuePair<System.Numerics.Vector2, Shogi.Domain.Pieces.Piece>;
|
||||
|
||||
namespace Shogi.Domain
|
||||
@@ -9,7 +10,7 @@ namespace Shogi.Domain
|
||||
|
||||
internal StandardRules(ShogiBoardState board)
|
||||
{
|
||||
this.boardState = board;
|
||||
boardState = board;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -179,19 +180,17 @@ namespace Shogi.Domain
|
||||
return threatenedPiece.WhichPiece == WhichPiece.King;
|
||||
}
|
||||
|
||||
internal bool IsPlayerInCheckMate(WhichPlayer whichPlayer)
|
||||
internal bool IsOpponentInCheckMate()
|
||||
{
|
||||
// Assume checkmate, then try to disprove.
|
||||
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
|
||||
// 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 = tilesOccupiedByOtherPlayer.Where(tile => PieceHasLineOfSight(tile, kingPosition)).ToList();
|
||||
|
||||
|
||||
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.
|
||||
@@ -200,7 +199,7 @@ namespace Shogi.Domain
|
||||
*/
|
||||
var threat = threats.Single();
|
||||
var pathFromThreatToKing = threat.Value.GetPathFromStartToEnd(threat.Key, kingPosition);
|
||||
var tilesThatCouldBlockTheThreat = boardState.GetTilesOccupiedBy(whichPlayer);
|
||||
var tilesThatCouldBlockTheThreat = boardState.GetTilesOccupiedBy(boardState.WhoseTurn);
|
||||
foreach (var threatBlockingPosition in pathFromThreatToKing)
|
||||
{
|
||||
var tilesThatDoBlockThreat = tilesThatCouldBlockTheThreat
|
||||
@@ -218,25 +217,35 @@ namespace Shogi.Domain
|
||||
/*
|
||||
* If no ability to block the check, maybe the king can evade check by moving.
|
||||
*/
|
||||
|
||||
|
||||
// TODO: Implement this in the Piece class instead.
|
||||
var possibleSafePositions = new[]
|
||||
foreach (var maybeSafePosition in GetPossiblePositionsForKing(this.boardState.WhoseTurn))
|
||||
{
|
||||
Direction.Up, Direction.Down, Direction.Left, Direction.Right, Direction.UpLeft, Direction.UpRight, Direction.DownLeft, Direction.DownRight
|
||||
}
|
||||
.Select(direction => kingPosition + direction);
|
||||
|
||||
foreach (var maybeSafePosition in possibleSafePositions)
|
||||
{
|
||||
|
||||
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 false;
|
||||
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)
|
||||
@@ -244,41 +253,7 @@ namespace Shogi.Domain
|
||||
var path = tile.Value.GetPathFromStartToEnd(tile.Key, lineOfSightTarget);
|
||||
return path
|
||||
.SkipLast(1)
|
||||
.All(position => this.boardState[ShogiBoardState.ToBoardNotation(position)] == null);
|
||||
}
|
||||
|
||||
public bool EvaluateCheckmate_Old()
|
||||
{
|
||||
if (!boardState.InCheck.HasValue) return false;
|
||||
|
||||
// Assume true and try to disprove.
|
||||
var isCheckmate = true;
|
||||
//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.
|
||||
//});
|
||||
return isCheckmate;
|
||||
.All(position => boardState[ShogiBoardState.ToBoardNotation(position)] == null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user