Better fix
This commit is contained in:
@@ -330,23 +330,35 @@ public sealed class ShogiBoard(BoardState initialState)
|
||||
|
||||
var clampedFromTo = Vector2.Clamp(to - from, -Vector2.One, Vector2.One);
|
||||
var matchingPaths = piece.MoveSet.Where(p => p.Step == clampedFromTo);
|
||||
if (!matchingPaths.Any())
|
||||
if (Vector2.Distance(to, from) < 2)
|
||||
{
|
||||
return new MoveResult(false, "Piece cannot move like that.");
|
||||
}
|
||||
|
||||
var multiStepPaths = matchingPaths.Where(path => path.Distance == YetToBeAssimilatedIntoDDD.Pathing.Distance.MultiStep).ToArray();
|
||||
foreach (var path in multiStepPaths)
|
||||
{
|
||||
// Assert that no pieces exist along the from -> to path.
|
||||
var isPathObstructed = GetPositionsAlongPath(from, to, path)
|
||||
.SkipLast(1)
|
||||
.Any(pos => BoardState[pos] != null);
|
||||
if (isPathObstructed)
|
||||
if (!matchingPaths.Any())
|
||||
{
|
||||
return new MoveResult(false, "Piece cannot move through other pieces.");
|
||||
return new MoveResult(false, "Piece cannot move like that.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var multiStepPaths = matchingPaths
|
||||
.Where(path => path.Distance == YetToBeAssimilatedIntoDDD.Pathing.Distance.MultiStep)
|
||||
.ToArray();
|
||||
if (multiStepPaths.Length == 0)
|
||||
{
|
||||
return new MoveResult(false, "Piece cannot move like that");
|
||||
}
|
||||
|
||||
foreach (var path in multiStepPaths)
|
||||
{
|
||||
// Assert that no pieces exist along the from -> to path.
|
||||
var isPathObstructed = GetPositionsAlongPath(from, to, path)
|
||||
.SkipLast(1)
|
||||
.Any(pos => BoardState[pos] != null);
|
||||
if (isPathObstructed)
|
||||
{
|
||||
return new MoveResult(false, "Piece cannot move through other pieces.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
var pieceAtTo = BoardState[to];
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
.MainLayout {
|
||||
display: grid;
|
||||
grid-template-columns: auto 1fr;
|
||||
grid-template-rows: 100vh;
|
||||
place-items: stretch;
|
||||
height: 100vh;
|
||||
}
|
||||
}
|
||||
|
||||
@media all and (max-width: 600px) {
|
||||
.MainLayout {
|
||||
grid-template-columns: min-content max-content;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,18 +103,19 @@ namespace UnitTests
|
||||
// Arrange
|
||||
var shogi = MockShogiBoard();
|
||||
var board = shogi.BoardState;
|
||||
var expectedPiece = board["A1"];
|
||||
expectedPiece!.WhichPiece.Should().Be(WhichPiece.Lance);
|
||||
var expectedPiece = board["D1"];
|
||||
expectedPiece!.WhichPiece.Should().Be(WhichPiece.GoldGeneral);
|
||||
|
||||
// Act - Move Lance illegally
|
||||
var moveResult = shogi.Move("A1", "D5", false);
|
||||
// Act - Move General illegally
|
||||
var moveResult = shogi.Move("D1", "D5", false);
|
||||
|
||||
// Assert
|
||||
using (new AssertionScope())
|
||||
{
|
||||
moveResult.Should().NotBeNull();
|
||||
moveResult.IsSuccess.Should().BeFalse(); board["A1"].Should().Be(expectedPiece);
|
||||
board["A5"].Should().BeNull();
|
||||
moveResult.IsSuccess.Should().BeFalse();
|
||||
board["D1"].Should().Be(expectedPiece);
|
||||
board["D5"].Should().BeNull();
|
||||
board.Player1Hand.Should().BeEmpty();
|
||||
board.Player2Hand.Should().BeEmpty();
|
||||
}
|
||||
@@ -292,19 +293,19 @@ namespace UnitTests
|
||||
// Arrange
|
||||
var shogi = MockShogiBoard();
|
||||
// P1 Pawn
|
||||
shogi.Move("C3", "C4", false);
|
||||
shogi.Move("C3", "C4", false).IsSuccess.Should().BeTrue();
|
||||
// P2 Pawn
|
||||
shogi.Move("G7", "G6", false);
|
||||
shogi.Move("G7", "G6", false).IsSuccess.Should().BeTrue();
|
||||
// P1 Pawn, arbitrary move.
|
||||
shogi.Move("A3", "A4", false);
|
||||
shogi.Move("A3", "A4", false).IsSuccess.Should().BeTrue();
|
||||
// P2 Bishop takes P1 Bishop
|
||||
shogi.Move("H8", "B2", false);
|
||||
shogi.Move("H8", "B2", false).IsSuccess.Should().BeTrue();
|
||||
// P1 Silver takes P2 Bishop
|
||||
shogi.Move("C1", "B2", false);
|
||||
shogi.Move("C1", "B2", false).IsSuccess.Should().BeTrue();
|
||||
// P2 Pawn, arbtrary move
|
||||
shogi.Move("A7", "A6", false);
|
||||
shogi.Move("A7", "A6", false).IsSuccess.Should().BeTrue();
|
||||
// P1 drop Bishop, place P2 in check
|
||||
shogi.Move(WhichPiece.Bishop, "G7");
|
||||
shogi.Move(WhichPiece.Bishop, "G7").IsSuccess.Should().BeTrue();
|
||||
shogi.BoardState.InCheck.Should().Be(WhichPlayer.Player2);
|
||||
shogi.BoardState.Player2Hand.Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
|
||||
shogi.BoardState["E5"].Should().BeNull();
|
||||
|
||||
Reference in New Issue
Block a user