Code smells

This commit is contained in:
2021-03-04 20:35:23 -06:00
parent e64f75e3cc
commit 7ed771d467
31 changed files with 310 additions and 339 deletions

View File

@@ -0,0 +1,65 @@
using Gameboard.ShogiUI.BoardState;
using Gameboard.ShogiUI.BoardState.Pieces;
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace Gameboard.ShogiUI.UnitTests.BoardState
{
public static class BoardStateExtensions
{
public static string GetShortName(this Piece self)
{
var name = self.WhichPiece switch
{
WhichPiece.King => " K ",
WhichPiece.GoldenGeneral => " G ",
WhichPiece.SilverGeneral => self.IsPromoted ? "^S " : " S ",
WhichPiece.Bishop => self.IsPromoted ? "^B " : " B ",
WhichPiece.Rook => self.IsPromoted ? "^R " : " R ",
WhichPiece.Knight => self.IsPromoted ? "^k " : " k ",
WhichPiece.Lance => self.IsPromoted ? "^L " : " L ",
WhichPiece.Pawn => self.IsPromoted ? "^P " : " P ",
_ => " ? ",
};
if (self.Owner == WhichPlayer.Player2)
name = Regex.Replace(name, @"([^\s]+)\s", "$1.");
return name;
}
public static void PrintStateAsAscii(this ShogiBoard self)
{
var builder = new StringBuilder();
builder.Append(" Player 2(.)");
builder.AppendLine();
for (var y = 8; y > -1; y--)
{
builder.Append("- ");
for (var x = 0; x < 8; x++) builder.Append("- - ");
builder.Append("- -");
builder.AppendLine();
builder.Append('|');
for (var x = 0; x < 9; x++)
{
var piece = self.Board[x, y];
if (piece == null)
{
builder.Append(" ");
}
else
{
builder.AppendFormat("{0}", piece.GetShortName());
}
builder.Append('|');
}
builder.AppendLine();
}
builder.Append("- ");
for (var x = 0; x < 8; x++) builder.Append("- - ");
builder.Append("- -");
builder.AppendLine();
builder.Append(" Player 1");
Console.WriteLine(builder.ToString());
}
}
}

View File

@@ -1,5 +1,6 @@
using FluentAssertions;
using Gameboard.ShogiUI.BoardState;
using Gameboard.ShogiUI.BoardState.Pieces;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
@@ -81,7 +82,6 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
shogi.Board[0, 3].WhichPiece.Should().Be(WhichPiece.Pawn);
}
[TestMethod]
public void PreventInvalidMoves_MoveFromEmptyPosition()
{
@@ -245,30 +245,110 @@ namespace Gameboard.ShogiUI.UnitTests.BoardState
var shogi = new ShogiBoard(moves);
shogi.PrintStateAsAscii();
// Prerequisite
// Prerequisites
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Knight);
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn);
// Act | Assert - It is P1 turn
// try illegally placing Knight from the hand.
/// try illegally placing Knight from the hand.
shogi.Board[7, 8].Should().BeNull();
var moveSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Knight, To = new Vector2(7, 8) });
shogi.PrintStateAsAscii();
moveSuccess.Should().BeFalse();
var dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Knight, To = new Vector2(7, 8) });
dropSuccess.Should().BeFalse();
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
shogi.Board[7, 8].Should().BeNull();
dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Knight, To = new Vector2(7, 7) });
dropSuccess.Should().BeFalse();
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
shogi.Board[7, 7].Should().BeNull();
// Assert
//var pawnDropSuccess = shogi.Move(new Move)
/// try illegally placing Pawn from the hand
dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Pawn, To = new Vector2(7, 8) });
dropSuccess.Should().BeFalse();
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Pawn);
shogi.Board[7, 8].Should().BeNull();
// Assert
/// try illegally placing Lance from the hand
dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Lance, To = new Vector2(7, 8) });
dropSuccess.Should().BeFalse();
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
shogi.Board[7, 8].Should().BeNull();
}
[TestMethod]
public void PreventInvalidDrop_Check()
{
// Arrange
var moves = new[]
{
// P1 Pawn
new Move { From = new Vector2(2, 2), To = new Vector2(2, 3) },
// P2 Pawn
new Move { From = new Vector2(8, 6), To = new Vector2(8, 5) },
// P1 Bishop, check
new Move { From = new Vector2(1, 1), To = new Vector2(6, 6) },
// P2 Gold, block check
new Move { From = new Vector2(5, 8), To = new Vector2(5, 7) },
// P1 arbitrary move
new Move { From = new Vector2(0, 2), To = new Vector2(0, 3) },
// P2 Bishop
new Move { From = new Vector2(7, 7), To = new Vector2(8, 6) },
// P1 Bishop takes P2 Lance
new Move { From = new Vector2(6, 6), To = new Vector2(8, 8) },
// P2 Bishop
new Move { From = new Vector2(8, 6), To = new Vector2(7, 7) },
// P1 arbitrary move
new Move { From = new Vector2(0, 3), To = new Vector2(0, 4) },
// P2 Bishop, check
new Move { From = new Vector2(7, 7), To = new Vector2(2, 2) },
};
var shogi = new ShogiBoard(moves);
// Prerequisites
shogi.InCheck.Should().Be(WhichPlayer.Player1);
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
// Act - P1 tries to place a Lance while in check.
var dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Lance, To = new Vector2(4, 4) });
// Assert
dropSuccess.Should().BeFalse();
shogi.Board[4, 4].Should().BeNull();
shogi.InCheck.Should().Be(WhichPlayer.Player1);
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Lance);
}
[TestMethod]
public void PreventInvalidDrop_Capture()
{
// Arrange
var moves = new[]
{
// P1 Pawn
new Move { From = new Vector2(2, 2), To = new Vector2(2, 3) },
// P2 Pawn
new Move { From = new Vector2(6, 6), To = new Vector2(6, 5) },
// P1 Bishop, capture P2 Pawn, check
new Move { From = new Vector2(1, 1), To = new Vector2(6, 6) },
// P2 Gold, block check
new Move { From = new Vector2(5, 8), To = new Vector2(5, 7) },
// P1 Bishop capture P2 Bishop
new Move { From = new Vector2(6, 6), To = new Vector2(7, 7) },
// P2 arbitrary move
new Move { From = new Vector2(0, 8), To = new Vector2(0, 7) },
};
var shogi = new ShogiBoard(moves);
// Prerequisites
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
// Act - P1 tries to place Bishop from hand to an already-occupied position
var dropSuccess = shogi.Move(new Move { PieceFromCaptured = WhichPiece.Bishop, To = new Vector2(4, 0) });
// Assert
dropSuccess.Should().BeFalse();
shogi.Hands[WhichPlayer.Player1].Should().ContainSingle(_ => _.WhichPiece == WhichPiece.Bishop);
shogi.Board[4, 0].WhichPiece.Should().Be(WhichPiece.King);
}
[TestMethod]

View File

@@ -1,8 +1,7 @@
using FluentAssertions;
using Gameboard.ShogiUI.BoardState;
using Gameboard.ShogiUI.BoardState.Pieces;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PathFinding;
using System.Collections.Generic;
using System.Numerics;
namespace Gameboard.ShogiUI.UnitTests.PathFinding
@@ -10,32 +9,25 @@ namespace Gameboard.ShogiUI.UnitTests.PathFinding
[TestClass]
public class PathFinder2DShould
{
class TestElement : IPlanarElement
{
public ICollection<Path> GetPaths() => throw new System.NotImplementedException();
public bool IsUpsideDown => false;
}
[TestMethod]
public void Maths()
{
var finder = new PathFinder2D<TestElement>(new Array2D<TestElement>(5, 5));
var result = finder.IsPathable(
var result = PathFinder2D<Piece>.IsPathable(
new Vector2(2, 2),
new Vector2(7, 7),
new Vector2(1, 1)
);
result.Should().BeTrue();
result = finder.IsPathable(
result = PathFinder2D<Piece>.IsPathable(
new Vector2(2, 2),
new Vector2(7, 7),
new Vector2(0, 0)
);
result.Should().BeFalse();
result = finder.IsPathable(
result = PathFinder2D<Piece>.IsPathable(
new Vector2(2, 2),
new Vector2(7, 7),
new Vector2(-1, 1)