Finish logic around placing pieces from the hand.
This commit is contained in:
@@ -17,7 +17,6 @@ public class BoardState
|
||||
playerInCheck: null,
|
||||
previousMove: new Move());
|
||||
|
||||
public delegate void ForEachDelegate(Piece element, Vector2 position);
|
||||
/// <summary>
|
||||
/// Key is position notation, such as "E4".
|
||||
/// </summary>
|
||||
|
||||
@@ -76,7 +76,7 @@ namespace Shogi.Domain
|
||||
}
|
||||
if (boardState[to] != null)
|
||||
{
|
||||
return new MoveResult(false, $"Illegal move - attempting to capture while playing a piece from the hand.");
|
||||
return new MoveResult(false, "Illegal move - attempting to capture while playing a piece from the hand.");
|
||||
}
|
||||
|
||||
switch (pieceInHand)
|
||||
@@ -107,7 +107,7 @@ namespace Shogi.Domain
|
||||
// Mutate the board.
|
||||
boardState[to] = boardState.ActivePlayerHand[index];
|
||||
boardState.ActivePlayerHand.RemoveAt(index);
|
||||
//MoveHistory.Add(move);
|
||||
boardState.PreviousMove = new Move(pieceInHand, to);
|
||||
return new MoveResult(true);
|
||||
}
|
||||
|
||||
@@ -118,18 +118,24 @@ namespace Shogi.Domain
|
||||
/// This strategy recognizes that a "discover check" could only occur from a subset of pieces: Rook, Bishop, Lance.
|
||||
/// In this way, only those pieces need to be considered when evaluating if a move placed the moving player in check.
|
||||
/// </remarks>
|
||||
internal bool IsPlayerInCheckAfterMove()
|
||||
internal bool DidPlayerPutThemselfInCheck()
|
||||
{
|
||||
if (boardState.PreviousMove.From == null)
|
||||
{
|
||||
// You can't place yourself in check by placing a piece from your hand.
|
||||
return false;
|
||||
}
|
||||
|
||||
var previousMovedPiece = boardState[boardState.PreviousMove.To];
|
||||
if (previousMovedPiece == null) throw new ArgumentNullException(nameof(previousMovedPiece), $"No piece exists at position {boardState.PreviousMove.To}.");
|
||||
var kingPosition = previousMovedPiece.Owner == WhichPlayer.Player1 ? boardState.Player1KingPosition : boardState.Player2KingPosition;
|
||||
|
||||
var isCheck = false;
|
||||
|
||||
var isDiscoverCheck = false;
|
||||
// Get line equation from king through the now-unoccupied location.
|
||||
var direction = Vector2.Subtract(kingPosition, boardState.PreviousMove.From);
|
||||
var direction = Vector2.Subtract(kingPosition, boardState.PreviousMove.From.Value);
|
||||
var slope = Math.Abs(direction.Y / direction.X);
|
||||
var path = BoardState.GetPathAlongDirectionFromStartToEdgeOfBoard(boardState.PreviousMove.From, Vector2.Normalize(direction));
|
||||
var path = BoardState.GetPathAlongDirectionFromStartToEdgeOfBoard(boardState.PreviousMove.From.Value, Vector2.Normalize(direction));
|
||||
var threat = boardState.QueryFirstPieceInPath(path);
|
||||
if (threat == null || threat.Owner == previousMovedPiece.Owner) return false;
|
||||
// If absolute slope is 45°, look for a bishop along the line.
|
||||
@@ -137,7 +143,7 @@ namespace Shogi.Domain
|
||||
// if absolute slope is 0°, look for lance along the line.
|
||||
if (float.IsInfinity(slope))
|
||||
{
|
||||
isCheck = threat.WhichPiece switch
|
||||
isDiscoverCheck = threat.WhichPiece switch
|
||||
{
|
||||
WhichPiece.Lance => !threat.IsPromoted,
|
||||
WhichPiece.Rook => true,
|
||||
@@ -146,7 +152,7 @@ namespace Shogi.Domain
|
||||
}
|
||||
else if (slope == 1)
|
||||
{
|
||||
isCheck = threat.WhichPiece switch
|
||||
isDiscoverCheck = threat.WhichPiece switch
|
||||
{
|
||||
WhichPiece.Bishop => true,
|
||||
_ => false
|
||||
@@ -154,14 +160,13 @@ namespace Shogi.Domain
|
||||
}
|
||||
else if (slope == 0)
|
||||
{
|
||||
isCheck = threat.WhichPiece switch
|
||||
isDiscoverCheck = threat.WhichPiece switch
|
||||
{
|
||||
WhichPiece.Rook => true,
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
|
||||
return isCheck;
|
||||
return isDiscoverCheck;
|
||||
}
|
||||
|
||||
internal bool IsOpponentInCheckAfterMove() => IsOpposingKingThreatenedByPosition(boardState.PreviousMove.To);
|
||||
|
||||
@@ -3,6 +3,20 @@
|
||||
/// <summary>
|
||||
/// Represents a single piece being moved by a player from <paramref name="From"/> to <paramref name="To"/>.
|
||||
/// </summary>
|
||||
public record struct Move(Vector2 From, Vector2 To)
|
||||
public readonly record struct Move
|
||||
{
|
||||
public Move(Vector2 from, Vector2 to)
|
||||
{
|
||||
From = from;
|
||||
To = to;
|
||||
}
|
||||
public Move(WhichPiece pieceFromHand, Vector2 to)
|
||||
{
|
||||
PieceFromHand = pieceFromHand;
|
||||
To = to;
|
||||
}
|
||||
|
||||
public Vector2? From { get; }
|
||||
public Vector2 To { get; }
|
||||
public WhichPiece PieceFromHand { get; }
|
||||
}
|
||||
|
||||
@@ -44,12 +44,14 @@ public sealed class ShogiBoard
|
||||
throw new InvalidOperationException("Unable to move because you are still in check.");
|
||||
}
|
||||
|
||||
var otherPlayer = BoardState.WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
||||
if (simulation.IsPlayerInCheckAfterMove())
|
||||
if (simulation.DidPlayerPutThemselfInCheck())
|
||||
{
|
||||
throw new InvalidOperationException("Illegal move. This move places you in check.");
|
||||
}
|
||||
|
||||
var otherPlayer = BoardState.WhoseTurn == WhichPlayer.Player1
|
||||
? WhichPlayer.Player2
|
||||
: WhichPlayer.Player1;
|
||||
_ = rules.Move(from, to, isPromotion);
|
||||
if (rules.IsOpponentInCheckAfterMove())
|
||||
{
|
||||
@@ -113,31 +115,36 @@ public sealed class ShogiBoard
|
||||
throw new InvalidOperationException(moveResult.Reason);
|
||||
}
|
||||
|
||||
var otherPlayer = tempBoard.WhoseTurn == WhichPlayer.Player1 ? WhichPlayer.Player2 : WhichPlayer.Player1;
|
||||
if (BoardState.InCheck == BoardState.WhoseTurn)
|
||||
// If already in check, assert the move that resulted in check no longer results in check.
|
||||
if (BoardState.InCheck == BoardState.WhoseTurn
|
||||
&& simulation.IsOpposingKingThreatenedByPosition(BoardState.PreviousMove.To))
|
||||
{
|
||||
//if (simulation.IsPlayerInCheckAfterMove(boardState.PreviousMoveTo, toVector, boardState.WhoseTurn))
|
||||
//{
|
||||
// throw new InvalidOperationException("Illegal move. You're still in check!");
|
||||
//}
|
||||
throw new InvalidOperationException("Unable to drop piece becauase you are still in check.");
|
||||
}
|
||||
|
||||
var kingPosition = otherPlayer == WhichPlayer.Player1 ? tempBoard.Player1KingPosition : tempBoard.Player2KingPosition;
|
||||
//if (simulation.IsPlayerInCheckAfterMove(toVector, kingPosition, otherPlayer))
|
||||
//{
|
||||
if (simulation.DidPlayerPutThemselfInCheck())
|
||||
{
|
||||
throw new InvalidOperationException("Illegal move. This move places you in check.");
|
||||
}
|
||||
|
||||
//}
|
||||
// Update the non-simulation board.
|
||||
var otherPlayer = tempBoard.WhoseTurn == WhichPlayer.Player1
|
||||
? WhichPlayer.Player2
|
||||
: WhichPlayer.Player1;
|
||||
_ = rules.Move(pieceInHand, to);
|
||||
if (rules.IsOpponentInCheckAfterMove())
|
||||
{
|
||||
BoardState.InCheck = otherPlayer;
|
||||
// A pawn, placed from the hand, cannot be the cause of checkmate.
|
||||
if (rules.IsOpponentInCheckMate() && pieceInHand != WhichPiece.Pawn)
|
||||
{
|
||||
BoardState.IsCheckmate = true;
|
||||
}
|
||||
}
|
||||
|
||||
//rules.Move(from, to, isPromotion);
|
||||
//if (rules.IsPlayerInCheckAfterMove(fromVector, toVector, otherPlayer))
|
||||
//{
|
||||
// board.InCheck = otherPlayer;
|
||||
// board.IsCheckmate = rules.EvaluateCheckmate();
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// board.InCheck = null;
|
||||
//}
|
||||
var kingPosition = otherPlayer == WhichPlayer.Player1
|
||||
? tempBoard.Player1KingPosition
|
||||
: tempBoard.Player2KingPosition;
|
||||
BoardState.WhoseTurn = otherPlayer;
|
||||
}
|
||||
|
||||
@@ -149,7 +156,7 @@ public sealed class ShogiBoard
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
builder.Append(" ");
|
||||
builder.Append("Player 2(.)");
|
||||
builder.Append("Player 2");
|
||||
builder.AppendLine();
|
||||
for (var rank = 8; rank >= 0; rank--)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Shogi.Domain.ValueObjects;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Shogi.Domain.UnitTests
|
||||
{
|
||||
@@ -299,15 +300,16 @@ namespace Shogi.Domain.UnitTests
|
||||
shogi.Move("A7", "A6", false);
|
||||
// P1 drop Bishop, place P2 in check
|
||||
shogi.Move(WhichPiece.Bishop, "G7");
|
||||
|
||||
shogi.BoardState.InCheck.Should().Be(WhichPlayer.Player2);
|
||||
shogi.BoardState.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
|
||||
shogi.BoardState["E5"].Should().BeNull();
|
||||
|
||||
// Act - P2 places a Bishop while in check.
|
||||
shogi.Move(WhichPiece.Bishop, "E5");
|
||||
var act = () => shogi.Move(WhichPiece.Bishop, "E5");
|
||||
|
||||
// Assert
|
||||
using var scope = new AssertionScope();
|
||||
act.Should().Throw<InvalidOperationException>();
|
||||
shogi.BoardState["E5"].Should().BeNull();
|
||||
shogi.BoardState.InCheck.Should().Be(WhichPlayer.Player2);
|
||||
shogi.BoardState.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
|
||||
@@ -326,26 +328,22 @@ namespace Shogi.Domain.UnitTests
|
||||
shogi.Move("B2", "H8", false);
|
||||
// P2 Pawn
|
||||
shogi.Move("G6", "G5", false);
|
||||
using (new AssertionScope())
|
||||
{
|
||||
shogi.BoardState.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
|
||||
shogi.BoardState["I9"].Should().NotBeNull();
|
||||
shogi.BoardState["I9"]!.WhichPiece.Should().Be(WhichPiece.Lance);
|
||||
shogi.BoardState["I9"]!.Owner.Should().Be(WhichPlayer.Player2);
|
||||
}
|
||||
|
||||
// Act - P1 tries to place a piece where an opponent's piece resides.
|
||||
shogi.Move(WhichPiece.Bishop, "I9");
|
||||
var act = () => shogi.Move(WhichPiece.Bishop, "I9");
|
||||
|
||||
// Assert
|
||||
using (new AssertionScope())
|
||||
{
|
||||
using var scope = new AssertionScope();
|
||||
act.Should().Throw<InvalidOperationException>();
|
||||
shogi.BoardState.Player1Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
|
||||
shogi.BoardState["I9"].Should().NotBeNull();
|
||||
shogi.BoardState["I9"]!.WhichPiece.Should().Be(WhichPiece.Lance);
|
||||
shogi.BoardState["I9"]!.Owner.Should().Be(WhichPlayer.Player2);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Check()
|
||||
@@ -446,6 +444,7 @@ namespace Shogi.Domain.UnitTests
|
||||
|
||||
// Assert - checkmate
|
||||
console.WriteLine(shogi.ToStringStateAsAscii());
|
||||
console.WriteLine(string.Join(",", shogi.BoardState.Player1Hand.Select(p => p.WhichPiece.ToString())));
|
||||
board.IsCheckmate.Should().BeTrue();
|
||||
board.InCheck.Should().Be(WhichPlayer.Player2);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user