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 Shogi.Domain.YetToBeAssimilatedIntoDDD;
using System.Threading.Tasks;
namespace Shogi.Domain.ValueObjects; namespace Shogi.Domain.ValueObjects;
/// <summary> /// <summary>
@@ -173,9 +172,8 @@ public sealed class ShogiBoard
} }
private async Task<GameOverResult> EvaluateGameOver() private GameOverResult EvaluateGameOver()
{ {
if (!BoardState.InCheck.HasValue) if (!BoardState.InCheck.HasValue)
{ {
return GameOverResult.GameIsNotOver; return GameOverResult.GameIsNotOver;
@@ -198,43 +196,20 @@ public sealed class ShogiBoard
var simState = new BoardState(BoardState); var simState = new BoardState(BoardState);
simState.Move(notation, Notation.ToBoardNotation(move), false); simState.Move(notation, Notation.ToBoardNotation(move), false);
var inCheckResult = IsEitherPlayerInCheck(simState); var inCheckResult = IsEitherPlayerInCheck(simState);
if (inCheckResult == InCheckResult.) var isStillInCheck = BoardState.InCheck == WhichPlayer.Player1
} ? inCheckResult.HasFlag(InCheckResult.Player1InCheck)
} : inCheckResult.HasFlag(InCheckResult.Player2InCheck);
// while (tasks.Any()) var result = Task.WhenAny(tasks); // Then check for GameIsNotOver and maybe return early. if (!isStillInCheck)
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 GameOverResult.GameIsNotOver;
}
}
}
return BoardState.InCheck == WhichPlayer.Player1
? GameOverResult.Player2Wins
: GameOverResult.Player1Wins;
Vector2[] GetPossiblePositionsForPiece(Vector2 piecePosition, Piece piece) Vector2[] GetPossiblePositionsForPiece(Vector2 piecePosition, Piece piece)
{ {

View File

@@ -1,5 +1,4 @@
using System.Diagnostics; using System.Diagnostics;
using static Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing.Path;
namespace Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing; namespace Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing;
@@ -20,4 +19,6 @@ public record Path
Step = step; Step = step;
this.Distance = distance; this.Distance = distance;
} }
public Path Invert() => new(Vector2.Negate(Step), Distance); public Path Invert() => new(Vector2.Negate(Step), Distance);
}