This commit is contained in:
2024-10-25 10:30:47 -05:00
parent 3593785421
commit 7d47fafea0
11 changed files with 124 additions and 107 deletions

View File

@@ -59,9 +59,9 @@ public class ShogiApplication(
{
session.Board.Move(move.PieceFromHand.Value, move.To);
}
else if (move.From != null)
else if (move.From != null && move.IsPromotion.HasValue)
{
session.Board.Move(move.From, move.To, false);
session.Board.Move(move.From, move.To, move.IsPromotion.Value);
}
else
{
@@ -95,7 +95,8 @@ public class ShogiApplication(
}
else
{
session.Board.Move(command.From!, command.To, command.IsPromotion ?? false);
var isPromotion = command.IsPromotion.HasValue ? command.IsPromotion.Value : false;
session.Board.Move(command.From!, command.To, isPromotion);
}
}
catch (InvalidOperationException e)

View File

@@ -23,16 +23,16 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.28" />
<PackageReference Include="FluentValidation" Version="11.9.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="FluentValidation" Version="11.10.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.10" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.9.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
</ItemGroup>

View File

@@ -1,5 +1,20 @@
namespace Shogi.Domain.ValueObjects;
[Flags]
internal enum InCheckResult
{
NobodyInCheck = 1, // This kinda doesn't make sense from a Flags perspective, but it works. =/
Player1InCheck = 2,
Player2InCheck = 4
}
internal enum GameOverResult
{
GameIsNotOver,
Player1Wins,
Player2Wins
}
public enum WhichPiece
{
King,

View File

@@ -1,9 +0,0 @@
namespace Shogi.Domain.ValueObjects;
[Flags]
internal enum InCheckResult
{
NobodyInCheck = 1, // This kinda doesn't make sense from a Flags perspective, but it works. =/
Player1InCheck = 2,
Player2InCheck = 4
}

View File

@@ -59,34 +59,7 @@ public sealed class ShogiBoard
if (kings.Length != 2) throw new InvalidOperationException("Unexpected scenario: board does not have two kings in play.");
// Look for threats against the kings.
var inCheckResult = simState.State
.Where(kvp => kvp.Value != null)
.Cast<KeyValuePair<string, Piece>>()
.Aggregate(InCheckResult.NobodyInCheck, (inCheckResult, kvp) =>
{
var newInCheckResult = inCheckResult;
var threatPiece = kvp.Value;
var opposingKingPosition = Notation.FromBoardNotation(kings.Single(king => king.Value.Owner != threatPiece.Owner).Key);
var candidatePositions = threatPiece.GetPathFromStartToEnd(Notation.FromBoardNotation(kvp.Key), opposingKingPosition);
foreach (var position in candidatePositions)
{
// No piece at this position, so pathing is unobstructed. Continue pathing.
if (simState[position] == null) continue;
var pieceAtPosition = simState[position]!;
if (pieceAtPosition.WhichPiece == WhichPiece.King && pieceAtPosition.Owner != threatPiece.Owner)
{
newInCheckResult &= pieceAtPosition.Owner == WhichPlayer.Player1 ? InCheckResult.Player2InCheck : InCheckResult.Player1InCheck;
}
else
{
break;
}
}
return newInCheckResult;
});
InCheckResult inCheckResult = IsEitherPlayerInCheck(simState, kings);
var playerPutThemselfInCheck = BoardState.WhoseTurn == WhichPlayer.Player1
? inCheckResult.HasFlag(InCheckResult.Player1InCheck)
@@ -96,12 +69,12 @@ public sealed class ShogiBoard
{
return new MoveResult(false, "This move puts the moving player in check, which is illega.");
}
// Move is legal; mutate the real state.
BoardState.Move(from, to, isPromotion);
var playerPutOpponentInCheck = BoardState.WhoseTurn == WhichPlayer.Player1
? inCheckResult.HasFlag(InCheckResult.Player2InCheck)
: inCheckResult.HasFlag(InCheckResult.Player1InCheck);
// Move is legal; mutate the real state.
BoardState.Move(from, to, isPromotion);
if (playerPutOpponentInCheck)
{
BoardState.InCheck = BoardState.WhoseTurn == WhichPlayer.Player1
@@ -112,15 +85,6 @@ public sealed class ShogiBoard
// TODO: Look for check-mate.
return new MoveResult(true);
//var simulation = new StandardRules(simState);
//// If already in check, assert the move that resulted in check no longer results in check.
//if (BoardState.InCheck == BoardState.WhoseTurn
@@ -233,6 +197,44 @@ public sealed class ShogiBoard
//BoardState.WhoseTurn = otherPlayer;
}
public GameOverResult EvaluateGameOver()
{
}
private static InCheckResult IsEitherPlayerInCheck(BoardState simState, KeyValuePair<string, Piece>[] kings)
{
return simState.State
.Where(kvp => kvp.Value != null)
.Cast<KeyValuePair<string, Piece>>()
.Aggregate(InCheckResult.NobodyInCheck, (inCheckResult, kvp) =>
{
var newInCheckResult = inCheckResult;
var threatPiece = kvp.Value;
var opposingKingPosition = Notation.FromBoardNotation(kings.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;
});
}
/// <summary>
/// The purpose is to ensure a proposed board move is valid with regard to the moved piece's rules.
/// This event does not worry about check or check-mate, or if a move is legal according to all Shogi rules.

View File

@@ -19,7 +19,7 @@
var position = $"{file}{rank}";
var piece = Session?.BoardState.Board[position];
var isSelected = piece != null && SelectedPosition == position;
<div class="tile" @onclick="OnClickTileInternal(piece, position)"
<div class="tile" @onclick="OnClickTileInternal(position)"
data-position="@(position)"
data-selected="@(isSelected)"
style="grid-area: @position">
@@ -80,7 +80,7 @@
</div>
<div class="player-area">
@if (this.OnClickJoinGame != null && string.IsNullOrEmpty(Session.Player2) && !string.IsNullOrEmpty(Session.Player1))
@if (string.IsNullOrEmpty(Session.Player2) && !string.IsNullOrEmpty(Session.Player1))
{
<div class="place-self-center">
<button @onclick="OnClickJoinGameInternal">Join Game</button>
@@ -121,9 +121,9 @@
[Parameter] public string? SelectedPosition { get; set; }
[Parameter] public WhichPiece? SelectedPieceFromHand { get; set; }
// TODO: Exchange these OnClick actions for events like "SelectionChangedEvent" and "MoveFromBoardEvent" and "MoveFromHandEvent".
[Parameter] public Func<Piece?, string, Task>? OnClickTile { get; set; }
[Parameter] public Func<Piece, Task>? OnClickHand { get; set; }
[Parameter] public Func<Task>? OnClickJoinGame { get; set; }
[Parameter] public EventCallback<string> OnClickTile { get; set; }
[Parameter] public EventCallback<Piece> OnClickHand { get; set; }
[Parameter] public EventCallback OnClickJoinGame { get; set; }
[Parameter] public bool IsMyTurn { get; set; }
[Parameter] public bool UseSideboard { get; set; } = true;
@@ -167,7 +167,9 @@
}
}
private Action OnClickTileInternal(Piece? piece, string position) => () => OnClickTile?.Invoke(piece, position);
private Action OnClickHandInternal(Piece piece) => () => OnClickHand?.Invoke(piece);
private void OnClickJoinGameInternal() => OnClickJoinGame?.Invoke();
private Func<Task> OnClickTileInternal(string position) => () => OnClickTile.InvokeAsync(position);
private Func<Task> OnClickHandInternal(Piece piece) => () => OnClickHand.InvokeAsync(piece);
private Task OnClickJoinGameInternal() => OnClickJoinGame.InvokeAsync();
}

View File

@@ -50,7 +50,7 @@
}
}
bool ShouldPromptForPromotion(string position)
bool IsWithinPromoteArea(string position)
{
if (Perspective == WhichPlayer.Player1 && Regex.IsMatch(position, ".[7-9]"))
{
@@ -63,11 +63,11 @@
return false;
}
async Task OnClickTile(Piece? pieceAtPosition, string position)
async Task OnClickTile(string position)
{
if (!IsMyTurn) return;
if (!IsMyTurn || showPromotePrompt) return;
var pieceAtPosition = Session.BoardState.Board[position];
if (selectedBoardPosition == position)
{
// Deselect the selected position.
@@ -105,12 +105,13 @@
if (selectedBoardPosition != null)
{
Console.WriteLine("pieceAtPosition is null? {0}", pieceAtPosition == null);
if (pieceAtPosition == null || pieceAtPosition?.Owner != Perspective)
{
var pieceBeingMoved = Session.BoardState.Board[selectedBoardPosition];
var isPromotedAlready = pieceBeingMoved != null && pieceBeingMoved.IsPromoted;
Console.WriteLine("Is promoted? {0}", isPromotedAlready);
// Moving to an empty space or capturing an opponent's piece.
if (ShouldPromptForPromotion(position) || ShouldPromptForPromotion(selectedBoardPosition))
if (!isPromotedAlready && (IsWithinPromoteArea(position) || IsWithinPromoteArea(selectedBoardPosition)))
{
Console.WriteLine("Prompt!");
moveTo = position;
@@ -118,11 +119,7 @@
}
else
{
Console.WriteLine("OnClick to move to {0}", position);
var success = await ShogiApi.Move(Session.SessionId, new MovePieceCommand(selectedBoardPosition, position, false));
Console.WriteLine("Success? {0}", success);
if (!success)
{
selectedBoardPosition = null;
@@ -134,9 +131,9 @@
}
}
async Task OnClickHand(Piece piece)
void OnClickHand(Piece piece)
{
if (!IsMyTurn) return;
if (!IsMyTurn || showPromotePrompt) return;
// Prevent selecting from both the hand and the board.
selectedBoardPosition = null;
@@ -149,11 +146,13 @@
StateHasChanged();
}
private Task OnClickPromotionChoice(bool shouldPromote)
private async Task OnClickPromotionChoice(bool shouldPromote)
{
if (selectedBoardPosition == null && selectedPieceFromHand.HasValue && moveTo != null)
if (selectedBoardPosition != null && moveTo != null)
{
return ShogiApi.Move(Session.SessionId, new MovePieceCommand(selectedPieceFromHand.Value, moveTo));
await ShogiApi.Move(Session.SessionId, new MovePieceCommand(selectedBoardPosition, moveTo, shouldPromote));
showPromotePrompt = false;
return;
}
throw new InvalidOperationException("Unexpected scenario during OnClickPromotionChoice.");

View File

@@ -0,0 +1,7 @@
namespace Shogi.UI.Pages.Play.GameBoard
{
public class TestCbParams
{
public string Name { get; set; }
}
}

View File

@@ -26,16 +26,16 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="2.1.21" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.8" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client.Core" Version="8.0.8" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="8.0.1" />
<PackageReference Include="Microsoft.Authentication.WebAssembly.Msal" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.10" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client.Core" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="8.0.10" />
<PackageReference Include="Microsoft.Authentication.WebAssembly.Msal" Version="8.0.10" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="8.0.1" />
</ItemGroup>
<ItemGroup>

View File

@@ -25,20 +25,20 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Microsoft.Net.Http.Headers" Version="8.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.1" />
<PackageReference Include="Microsoft.Net.Http.Headers" Version="8.0.10" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>

View File

@@ -8,14 +8,14 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>