From a3d6065e956d0a13891722e1f7817ef8b888a250 Mon Sep 17 00:00:00 2001 From: Lucas Morgan Date: Sun, 27 Oct 2024 21:15:35 -0500 Subject: [PATCH] First pass. Needs testing. --- Shogi.Domain/ValueObjects/ShogiBoard.cs | 49 +++++-------------- .../YetToBeAssimilatedIntoDDD/Pathing/Path.cs | 5 +- 2 files changed, 15 insertions(+), 39 deletions(-) diff --git a/Shogi.Domain/ValueObjects/ShogiBoard.cs b/Shogi.Domain/ValueObjects/ShogiBoard.cs index 1ddfa64..f961d65 100644 --- a/Shogi.Domain/ValueObjects/ShogiBoard.cs +++ b/Shogi.Domain/ValueObjects/ShogiBoard.cs @@ -1,5 +1,4 @@ using Shogi.Domain.YetToBeAssimilatedIntoDDD; -using System.Threading.Tasks; namespace Shogi.Domain.ValueObjects; /// @@ -173,9 +172,8 @@ public sealed class ShogiBoard } - private async Task 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>() - .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) { diff --git a/Shogi.Domain/YetToBeAssimilatedIntoDDD/Pathing/Path.cs b/Shogi.Domain/YetToBeAssimilatedIntoDDD/Pathing/Path.cs index 4be696e..ffd22f3 100644 --- a/Shogi.Domain/YetToBeAssimilatedIntoDDD/Pathing/Path.cs +++ b/Shogi.Domain/YetToBeAssimilatedIntoDDD/Pathing/Path.cs @@ -1,5 +1,4 @@ using System.Diagnostics; -using static Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing.Path; namespace Shogi.Domain.YetToBeAssimilatedIntoDDD.Pathing; @@ -20,4 +19,6 @@ public record Path Step = step; this.Distance = distance; } - public Path Invert() => new(Vector2.Negate(Step), Distance); \ No newline at end of file + + public Path Invert() => new(Vector2.Negate(Step), Distance); +} \ No newline at end of file