This commit is contained in:
2021-12-29 22:11:49 -06:00
parent 9ec91615a3
commit 499e480851
4 changed files with 57 additions and 52 deletions

View File

@@ -1,5 +1,6 @@
using FluentAssertions; using FluentAssertions;
using FluentAssertions.Execution; using FluentAssertions.Execution;
using System;
using System.Linq; using System.Linq;
using Xunit; using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
@@ -14,62 +15,65 @@ namespace Shogi.Domain.UnitTests
this.output = output; this.output = output;
} }
//[Fact] [Fact]
//public void InitializeBoardStateWithMoves() public void MoveAPieceToAnEmptyPosition()
//{ {
// var board = new ShogiBoardState(); // Arrange
// var rules = new StandardRules(board); var board = new ShogiBoardState();
// var moves = new[] var shogi = new Shogi(board);
// { board["A4"].Should().BeNull();
// // P1 Pawn var expectedPiece = board["A3"];
// new Move("A3", "A4") expectedPiece.Should().NotBeNull();
// };
// var shogi = new Shogi(moves);
// shogi.Board["A3"].Should().BeNull();
// shogi.Board["A4"].WhichPiece.Should().Be(WhichPiece.Pawn);
//}
//[Fact] // Act
//public void AllowValidMoves_AfterCheck() shogi.Move("A3", "A4", false);
//{
// // Arrange
// var moves = new[]
// {
// // P1 Pawn
// new Move("C3", "C4"),
// // P2 Pawn
// new Move("G7", "G6"),
// // P1 Bishop puts P2 in check
// new Move("B2", "G7"),
// };
// var shogi = new Shogi(moves);
// shogi.InCheck.Should().Be(WhichPlayer.Player2);
// // Act - P2 is able to un-check theirself. // Assert
// /// P2 King moves out of check board["A3"].Should().BeNull();
// var moveSuccess = shogi.Move(new Move("E9", "E8")); board["A4"].Should().Be(expectedPiece);
}
// // Assert [Fact]
// using var _ = new AssertionScope(); public void AllowValidMoves_AfterCheck()
// moveSuccess.Should().BeTrue(); {
// shogi.InCheck.Should().BeNull(); // Arrange
//} var board = new ShogiBoardState();
var shogi = new Shogi(board);
// P1 Pawn
shogi.Move("C3", "C4", false);
// P2 Pawn
shogi.Move("G7", "G6", false);
// P1 Bishop puts P2 in check
shogi.Move("B2", "G7", false);
board.InCheck.Should().Be(WhichPlayer.Player2);
//[Fact] // Act - P2 is able to un-check theirself.
//public void PreventInvalidMoves_MoveFromEmptyPosition() /// P2 King moves out of check
//{ shogi.Move("E9", "E8", false);
// // Arrange
// var shogi = new Shogi();
// shogi.Board["D5"].Should().BeNull();
// // Act // Assert
// var moveSuccess = shogi.Move(new Move("D5", "D6")); using (new AssertionScope())
{
board.InCheck.Should().BeNull();
}
}
// // Assert [Fact]
// moveSuccess.Should().BeFalse(); public void PreventInvalidMoves_MoveFromEmptyPosition()
// shogi.Board["D5"].Should().BeNull(); {
// shogi.Board["D6"].Should().BeNull(); // Arrange
//} var board = new ShogiBoardState();
var shogi = new Shogi(board);
board["D5"].Should().BeNull();
// Act
var act = () => shogi.Move("D5", "D6", false);
// Assert
act.Should().Throw<InvalidOperationException>();
board["D5"].Should().BeNull();
board["D6"].Should().BeNull();
}
//[Fact] //[Fact]
//public void PreventInvalidMoves_MoveToCurrentPosition() //public void PreventInvalidMoves_MoveToCurrentPosition()

View File

@@ -61,6 +61,7 @@
{ {
board.InCheck = null; board.InCheck = null;
} }
board.WhoseTurn = otherPlayer;
} }
///// <summary> ///// <summary>

View File

@@ -19,7 +19,7 @@ namespace Shogi.Domain
public List<Piece> Player1Hand { get; } public List<Piece> Player1Hand { get; }
public List<Piece> Player2Hand { get; } public List<Piece> Player2Hand { get; }
public List<Move> MoveHistory { get; } public List<Move> MoveHistory { get; }
public WhichPlayer WhoseTurn => MoveHistory.Count % 2 == 0 ? WhichPlayer.Player1 : WhichPlayer.Player2; public WhichPlayer WhoseTurn { get; set; }
public WhichPlayer? InCheck { get; set; } public WhichPlayer? InCheck { get; set; }
public bool IsCheckmate { get; set; } public bool IsCheckmate { get; set; }

View File

@@ -51,7 +51,7 @@ namespace Shogi.Domain
var fromPiece = board[from]; var fromPiece = board[from];
if (fromPiece == null) if (fromPiece == null)
{ {
return new MoveResult(false, $"Tile [{from}] is empty. There is no piece to move."); return new MoveResult(false, $"Tile [{fromNotation}] is empty. There is no piece to move.");
} }
if (fromPiece.Owner != board.WhoseTurn) if (fromPiece.Owner != board.WhoseTurn)