From 499e48085116910267250560e9da643d6c2ad44a Mon Sep 17 00:00:00 2001 From: Lucas Morgan Date: Wed, 29 Dec 2021 22:11:49 -0600 Subject: [PATCH] yep --- Shogi.Domain.UnitTests/ShogiShould.cs | 104 +++++++++++++------------- Shogi.Domain/Shogi.cs | 1 + Shogi.Domain/ShogiBoardState.cs | 2 +- Shogi.Domain/StandardRules.cs | 2 +- 4 files changed, 57 insertions(+), 52 deletions(-) diff --git a/Shogi.Domain.UnitTests/ShogiShould.cs b/Shogi.Domain.UnitTests/ShogiShould.cs index 745e28f..9e7724e 100644 --- a/Shogi.Domain.UnitTests/ShogiShould.cs +++ b/Shogi.Domain.UnitTests/ShogiShould.cs @@ -1,5 +1,6 @@ using FluentAssertions; using FluentAssertions.Execution; +using System; using System.Linq; using Xunit; using Xunit.Abstractions; @@ -14,62 +15,65 @@ namespace Shogi.Domain.UnitTests this.output = output; } - //[Fact] - //public void InitializeBoardStateWithMoves() - //{ - // var board = new ShogiBoardState(); - // var rules = new StandardRules(board); - // var moves = new[] - // { - // // P1 Pawn - // new Move("A3", "A4") - // }; - // var shogi = new Shogi(moves); - // shogi.Board["A3"].Should().BeNull(); - // shogi.Board["A4"].WhichPiece.Should().Be(WhichPiece.Pawn); - //} + [Fact] + public void MoveAPieceToAnEmptyPosition() + { + // Arrange + var board = new ShogiBoardState(); + var shogi = new Shogi(board); + board["A4"].Should().BeNull(); + var expectedPiece = board["A3"]; + expectedPiece.Should().NotBeNull(); - //[Fact] - //public void AllowValidMoves_AfterCheck() - //{ - // // 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 + shogi.Move("A3", "A4", false); - // // Act - P2 is able to un-check theirself. - // /// P2 King moves out of check - // var moveSuccess = shogi.Move(new Move("E9", "E8")); + // Assert + board["A3"].Should().BeNull(); + board["A4"].Should().Be(expectedPiece); + } - // // Assert - // using var _ = new AssertionScope(); - // moveSuccess.Should().BeTrue(); - // shogi.InCheck.Should().BeNull(); - //} + [Fact] + public void AllowValidMoves_AfterCheck() + { + // 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] - //public void PreventInvalidMoves_MoveFromEmptyPosition() - //{ - // // Arrange - // var shogi = new Shogi(); - // shogi.Board["D5"].Should().BeNull(); + // Act - P2 is able to un-check theirself. + /// P2 King moves out of check + shogi.Move("E9", "E8", false); - // // Act - // var moveSuccess = shogi.Move(new Move("D5", "D6")); + // Assert + using (new AssertionScope()) + { + board.InCheck.Should().BeNull(); + } + } - // // Assert - // moveSuccess.Should().BeFalse(); - // shogi.Board["D5"].Should().BeNull(); - // shogi.Board["D6"].Should().BeNull(); - //} + [Fact] + public void PreventInvalidMoves_MoveFromEmptyPosition() + { + // 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(); + board["D5"].Should().BeNull(); + board["D6"].Should().BeNull(); + } //[Fact] //public void PreventInvalidMoves_MoveToCurrentPosition() diff --git a/Shogi.Domain/Shogi.cs b/Shogi.Domain/Shogi.cs index 1aca58a..2cf501c 100644 --- a/Shogi.Domain/Shogi.cs +++ b/Shogi.Domain/Shogi.cs @@ -61,6 +61,7 @@ { board.InCheck = null; } + board.WhoseTurn = otherPlayer; } ///// diff --git a/Shogi.Domain/ShogiBoardState.cs b/Shogi.Domain/ShogiBoardState.cs index 49e9683..216b4b3 100644 --- a/Shogi.Domain/ShogiBoardState.cs +++ b/Shogi.Domain/ShogiBoardState.cs @@ -19,7 +19,7 @@ namespace Shogi.Domain public List Player1Hand { get; } public List Player2Hand { get; } public List MoveHistory { get; } - public WhichPlayer WhoseTurn => MoveHistory.Count % 2 == 0 ? WhichPlayer.Player1 : WhichPlayer.Player2; + public WhichPlayer WhoseTurn { get; set; } public WhichPlayer? InCheck { get; set; } public bool IsCheckmate { get; set; } diff --git a/Shogi.Domain/StandardRules.cs b/Shogi.Domain/StandardRules.cs index 8b89f5d..9d8d908 100644 --- a/Shogi.Domain/StandardRules.cs +++ b/Shogi.Domain/StandardRules.cs @@ -51,7 +51,7 @@ namespace Shogi.Domain var fromPiece = board[from]; 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)