public void TestExtraTurnForBlackBug() { var sut = new TurnCalculator(); var board = new MatrixBoard(); { board.IsTurnOfBlackPlayer = true; board.TryConsumeNamedCellByWhitePlayer("E4"); board.TryConsumeNamedCellByWhitePlayer("E5"); board.TryConsumeNamedCellByBlackPlayer("C5"); board.TryConsumeNamedCellByBlackPlayer("D5"); board.TryConsumeNamedCellByBlackPlayer("D4"); board.TryConsumeNamedCellByBlackPlayer("E6"); } var result = sut.GetValidTurnsForBoard(board); Assert.IsNotNull(result); Assert.AreEqual(5, result.Count()); string strTurnPositions = BoardCoordinatesConverter.PrintTurnPositions(result); string expectedTurnPositions = "E3; F3; F4; F5; F6"; Assert.AreEqual(expectedTurnPositions, strTurnPositions); }
void Start() { this._isGameOver = false; var board = new MatrixBoard(); { this._boardModel = board; this._mutableBoardModel = board; } this._turnCalculator = new TurnCalculator(); this._turnSelector = TurnSelectorBuilder.CreateCornerAndGreedyTurnSelector(); this._root = GameObject.Find("root"); populateLabels(); populateBallsList(); populateCellsList(); populateCellsMatrix(); populateCellColours(); getAvailableTurns(); }
// Use this for initialization void Start() { this._isGameOver = false; var board = new MatrixBoard(); { this._boardModel = board; this._mutableBoardModel = board; } this._turnCalculator = new TurnCalculator(); this._turnSelector = TurnSelectorBuilder.CreateCornerAndGreedyTurnSelector(); this._root = GameObject.Find("root"); // Using lowercase methods in this class // to distinguish own methods from built-in unity methods populateLabels(); populateBallsList(); populateCellsList(); populateCellsMatrix(); populateCellColours(); getAvailableTurns(); }
public void TestTurnsResultIsNullForEmptyBoard() { var board = new MatrixBoard(); var sut = new TurnCalculator(); var turns = sut.GetValidTurnsForBoard(board); Assert.IsNull(turns); }
public void RestartGame() { for (int i = 0; i < MatrixBoard.GetLength(0); i++) { for (int j = 0; j < MatrixBoard.GetLength(1); j++) { MatrixBoard[i, j] = null; } } StartGame(); }
public void SetUp() { this._initialBoard = new MatrixBoard(); { this._initialBoard.TryConsumeNamedCellByBlackPlayer("D4"); this._initialBoard.TryConsumeNamedCellByBlackPlayer("E5"); this._initialBoard.TryConsumeNamedCellByWhitePlayer("E4"); this._initialBoard.TryConsumeNamedCellByWhitePlayer("D5"); this._initialBoard.IsTurnOfBlackPlayer = true; } this._sut = new TurnCalculator(); }
public void Print() { var board = new MatrixBoard(new int[9, 9] { { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 1, 2, 3, 4, 5, 6, 7, 8, 9 } }); var actual = board.Print(); actual.Should().Be("123456789\n123456789\n123456789\n123456789\n123456789\n123456789\n123456789\n123456789\n123456789\n"); }
public void TestNoFlipsIfBoardEndReached() { var sut = new TurnCalculator(); var board = new MatrixBoard(); { board.IsTurnOfBlackPlayer = false; board.TryConsumeNamedCellByBlackPlayer("A1"); board.TryConsumeNamedCellByBlackPlayer("H1"); board.TryConsumeNamedCellByBlackPlayer("A8"); board.TryConsumeNamedCellByBlackPlayer("H8"); board.TryConsumeNamedCellByBlackPlayer("A4"); board.TryConsumeNamedCellByBlackPlayer("H4"); board.TryConsumeNamedCellByBlackPlayer("D1"); board.TryConsumeNamedCellByBlackPlayer("D8"); // Diagonal board.TryConsumeNamedCellByBlackPlayer("B2"); board.TryConsumeNamedCellByBlackPlayer("C3"); board.TryConsumeNamedCellByBlackPlayer("E5"); board.TryConsumeNamedCellByBlackPlayer("F6"); board.TryConsumeNamedCellByBlackPlayer("G7"); // Horizontal board.TryConsumeNamedCellByBlackPlayer("B4"); board.TryConsumeNamedCellByBlackPlayer("C4"); board.TryConsumeNamedCellByBlackPlayer("E4"); board.TryConsumeNamedCellByBlackPlayer("F4"); board.TryConsumeNamedCellByBlackPlayer("G4"); // Vertical board.TryConsumeNamedCellByBlackPlayer("D2"); board.TryConsumeNamedCellByBlackPlayer("D3"); board.TryConsumeNamedCellByBlackPlayer("D5"); board.TryConsumeNamedCellByBlackPlayer("D6"); board.TryConsumeNamedCellByBlackPlayer("D7"); } var result = sut.GetValidTurnsForBoard(board); Assert.IsNull(result); }
public void WhenValueDoesNotExist_ReturnTrue() { var value = 8; var board = new MatrixBoard(new int[9, 9] { { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 } }); var sut = new RowRule(); var actual = sut.Test(board, new Point(6, 4), value); actual.Should().BeTrue(); }
public void WhenValueExists_ReturnFalse() { var value = 8; var board = new MatrixBoard(new int[9, 9] { { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, value, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 } }); var sut = new SquareRule(); var actual = sut.Test(board, new Point(6, 4), value); actual.Should().BeFalse(); }
private void RefreshGrid(Piece piece) { var position = piece.Position; var rowLength = MatrixBoard.GetLength(0); var colLength = MatrixBoard.GetLength(1); for (var row = position.Row - 1; row <= position.Row + 1; row++) { for (var col = position.Column - 1; col <= position.Column + 1; col++) { if (row >= 0 && row < rowLength && col >= 0 && col < colLength && position.Row != row && position.Column != col && !HasPiece(new Position(row, col))) { MatrixBoard[row, col].AddSurroundingPiece(piece); } } } }
public void Put(int x, int y) { var board = new MatrixBoard(new int[9, 9] { { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 } }); var point = new Point(x, y); var value = 1; board.Put(point, value); var actual = board.Get(point); actual.Should().Be(value); }
public void TestReverseDiagonalFlips() { var sut = new TurnCalculator(); var board = new MatrixBoard(); { board.IsTurnOfBlackPlayer = false; board.TryConsumeNamedCellByWhitePlayer("A8"); board.TryConsumeNamedCellByWhitePlayer("H1"); board.TryConsumeNamedCellByBlackPlayer("B7"); board.TryConsumeNamedCellByBlackPlayer("C6"); board.TryConsumeNamedCellByBlackPlayer("D5"); board.TryConsumeNamedCellByBlackPlayer("E4"); board.TryConsumeNamedCellByBlackPlayer("G2"); } var result = sut.GetValidTurnsForBoard(board); Assert.IsNotNull(result); Assert.AreEqual(1, result.Count()); IReversiTurn turn = result.First(); string strPosition = BoardCoordinatesConverter.CoordinatesToCellName(turn.Position); Assert.AreEqual("F3", strPosition); Assert.AreEqual(5, turn.PositionsOfFlippedItems.Count()); var sortedFlips = turn.PositionsOfFlippedItems.OrderBy(c => { return(BoardCoordinatesConverter.CoordinatesToCellName(c)); }); string strSortedFlips = BoardCoordinatesConverter.PrintCoordinates(sortedFlips); string expectedFlips = "B7; C6; D5; E4; G2"; Assert.AreEqual(expectedFlips, strSortedFlips); }
public void Solve2() { var board = new MatrixBoard(new int[9, 9] { { 0, 2, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 6, 0, 0, 0, 0, 3 }, { 0, 7, 4, 0, 8, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 3, 0, 0, 2 }, { 0, 8, 0, 0, 4, 0, 0, 1, 0 }, { 6, 0, 0, 5, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1, 0, 7, 8, 0 }, { 5, 0, 0, 0, 0, 9, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 4, 0 } }); var solution = new MatrixBoard(new int[9, 9] { { 1, 2, 6, 4, 3, 7, 9, 5, 8 }, { 8, 9, 5, 6, 2, 1, 4, 7, 3 }, { 3, 7, 4, 9, 8, 5, 1, 2, 6 }, { 4, 5, 7, 1, 9, 3, 8, 6, 2 }, { 9, 8, 3, 2, 4, 6, 5, 1, 7 }, { 6, 1, 2, 5, 7, 8, 3, 9, 4 }, { 2, 6, 9, 3, 1, 4, 7, 8, 5 }, { 5, 4, 8, 7, 6, 9, 2, 3, 1 }, { 7, 3, 1, 8, 5, 2, 6, 4, 9 } }); var sut = Setup(); var actual = sut.Solve(board, new List <IBoardRule> { new RowRule(), new ColumnRule(), new SquareRule() }); actual.Print().Should().Be(solution.Print()); }
public void TearDown() { this._initialBoard = null; this._sut = null; }
public void TestMultipleDirections() { var sut = new TurnCalculator(); var board = new MatrixBoard(); { board.IsTurnOfBlackPlayer = false; board.TryConsumeNamedCellByWhitePlayer("A1"); board.TryConsumeNamedCellByWhitePlayer("H1"); board.TryConsumeNamedCellByWhitePlayer("A8"); board.TryConsumeNamedCellByWhitePlayer("H8"); board.TryConsumeNamedCellByWhitePlayer("A4"); board.TryConsumeNamedCellByWhitePlayer("H4"); board.TryConsumeNamedCellByWhitePlayer("D1"); board.TryConsumeNamedCellByWhitePlayer("D8"); // Diagonal board.TryConsumeNamedCellByBlackPlayer("B2"); board.TryConsumeNamedCellByBlackPlayer("C3"); board.TryConsumeNamedCellByBlackPlayer("E5"); board.TryConsumeNamedCellByBlackPlayer("F6"); board.TryConsumeNamedCellByBlackPlayer("G7"); // Horizontal board.TryConsumeNamedCellByBlackPlayer("B4"); board.TryConsumeNamedCellByBlackPlayer("C4"); board.TryConsumeNamedCellByBlackPlayer("E4"); board.TryConsumeNamedCellByBlackPlayer("F4"); board.TryConsumeNamedCellByBlackPlayer("G4"); // Vertical board.TryConsumeNamedCellByBlackPlayer("D2"); board.TryConsumeNamedCellByBlackPlayer("D3"); board.TryConsumeNamedCellByBlackPlayer("D5"); board.TryConsumeNamedCellByBlackPlayer("D6"); board.TryConsumeNamedCellByBlackPlayer("D7"); } var result = sut.GetValidTurnsForBoard(board); Assert.IsNotNull(result); Assert.AreEqual(1, result.Count()); IReversiTurn turn = result.First(); string strPosition = BoardCoordinatesConverter.CoordinatesToCellName(turn.Position); Assert.AreEqual("D4", strPosition); Assert.AreEqual(15, turn.PositionsOfFlippedItems.Count()); var sortedFlips = turn.PositionsOfFlippedItems.OrderBy(c => { return(BoardCoordinatesConverter.CoordinatesToCellName(c)); }); string strSortedFlips = BoardCoordinatesConverter.PrintCoordinates(sortedFlips); string expectedFlips = "B2; B4; C3; C4; D2; D3; D5; D6; D7; E4; E5; F4; F6; G4; G7"; Assert.AreEqual(expectedFlips, strSortedFlips); }