First pass. Needs testing.

This commit is contained in:
2024-10-27 21:15:35 -05:00
parent f8bf967581
commit a3d6065e95
2 changed files with 15 additions and 39 deletions

View File

@@ -1,5 +1,4 @@
using Shogi.Domain.YetToBeAssimilatedIntoDDD;
using System.Threading.Tasks;
namespace Shogi.Domain.ValueObjects;
/// <summary>
@@ -173,9 +172,8 @@ public sealed class ShogiBoard
}
private async Task<GameOverResult> EvaluateGameOver()
private GameOverResult EvaluateGameOver()
{
if (!BoardState.InCheck.HasValue)
{
return GameOverResult.GameIsNotOver;
@@ -198,43 +196,20 @@ public sealed class ShogiBoard
var simState = new BoardState(BoardState);
simState.Move(notation, Notation.ToBoardNotation(move), false);
var inCheckResult = IsEitherPlayerInCheck(simState);
if (inCheckResult == InCheckResult.)
var isStillInCheck = BoardState.InCheck == WhichPlayer.Player1
? inCheckResult.HasFlag(InCheckResult.Player1InCheck)
: inCheckResult.HasFlag(InCheckResult.Player2InCheck);
if (!isStillInCheck)
{
return GameOverResult.GameIsNotOver;
}
}
}
// while (tasks.Any()) var result = Task.WhenAny(tasks); // Then check for GameIsNotOver and maybe return early.
var gameOverResult = BoardState.State
.Where(kvp => kvp.Value != null)
.Cast<KeyValuePair<string, Piece>>()
.Aggregate(GameOverResult.GameIsNotOver, (inCheckResult, kvp) =>
{
var newInCheckResult = inCheckResult;
var threatPiece = kvp.Value;
var opposingKingPosition = Notation.FromBoardNotation(kingInCheck.Single(king => king.Value.Owner != threatPiece.Owner).Key);
var positionsThreatened = threatPiece.GetPathFromStartToEnd(Notation.FromBoardNotation(kvp.Key), opposingKingPosition);
foreach (var position in positionsThreatened)
{
// No piece at this position, so pathing is unobstructed. Continue pathing.
if (simState[position] == null) continue;
var threatenedPiece = simState[position]!;
if (threatenedPiece.WhichPiece == WhichPiece.King && threatenedPiece.Owner != threatPiece.Owner)
{
newInCheckResult |= threatenedPiece.Owner == WhichPlayer.Player1 ? InCheckResult.Player1InCheck : InCheckResult.Player2InCheck;
}
else
{
break;
}
}
return newInCheckResult;
});
return GameOverResult.GameIsNotOver;
return BoardState.InCheck == WhichPlayer.Player1
? GameOverResult.Player2Wins
: GameOverResult.Player1Wins;
Vector2[] GetPossiblePositionsForPiece(Vector2 piecePosition, Piece piece)
{