예제 #1
0
        public void TestValidFieldToText()
        {
            var    position = new CellCoordinates(4, 2);
            string received = BoardCoordinatesConverter.CoordinatesToCellName(position);

            Assert.AreEqual("C5", received);
        }
예제 #2
0
        public void TestValidTextToField()
        {
            var received = BoardCoordinatesConverter.CellNameToCoordinates("C5");

            Assert.AreEqual(4, received.Row);
            Assert.AreEqual(2, received.Column);
        }
예제 #3
0
        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);
        }
예제 #4
0
        public void TestLomgNameCausesException()
        {
            TestDelegate failure = delegate()
            {
                BoardCoordinatesConverter.CellNameToCoordinates("A12");
            };

            Assert.Catch(failure, "invalid column name");
        }
예제 #5
0
        public void TestLargeColumnInNameCausesException()
        {
            TestDelegate failure = delegate()
            {
                BoardCoordinatesConverter.CellNameToCoordinates("A9");
            };

            Assert.Catch(failure, "invalid row name");
        }
예제 #6
0
        public void TestLargeColumnCausesException()
        {
            var          position = new CellCoordinates(0, 9);
            TestDelegate failure  = delegate()
            {
                BoardCoordinatesConverter.CoordinatesToCellName(position);
            };

            Assert.Catch(failure, "invalid row name");
        }
예제 #7
0
        public void TestNegativeColumnIndexCausesException()
        {
            var position = new CellCoordinates(3, -1);

            TestDelegate failure = delegate()
            {
                BoardCoordinatesConverter.CoordinatesToCellName(position);
            };

            Assert.Catch(failure, "invalid column name");
        }
    private void populateCellsMatrix()
    {
        this._cellsMatrix = new GameObject[BOARD_SIZE, BOARD_SIZE];

        for (int row = 0; row != BOARD_SIZE; ++row)
        {
            for (int column = 0; column != BOARD_SIZE; ++column)
            {
                int    flatIndex = row * BOARD_SIZE + column;
                var    cell      = this._cellsList[flatIndex];
                string cellName  = cell.name;

                var cellPosition = BoardCoordinatesConverter.CellNameToCoordinates(cellName);
                this._cellsMatrix [cellPosition.Row, cellPosition.Column] = cell;
            }
        }
    }
    private void handleTapOnCell(GameObject cellCube)
    {
        if (this.IsTurnOfAI)
        {
            return;
        }


        // TODO : maybe compute matrix index by reference
        string           cellName             = cellCube.name;
        ICellCoordinates selectedCellPosition = BoardCoordinatesConverter.CellNameToCoordinates(cellName);

        if (tryPassTurnOrGameOver())
        {
            return;
        }


        var  turnValidator       = new SearchInSetTurnValidator(this._validTurns);
        bool isSelectedTurnValid = turnValidator.IsValidPositionForTurnOnBoard(selectedCellPosition, this._boardModel);

        if (!isSelectedTurnValid)
        {
            // TODO : maybe show alert
            return;
        }


        IReversiTurn turn = this._validTurns.Where(t =>
        {
            string turnPositionName = BoardCoordinatesConverter.CoordinatesToCellName(t.Position);
            return(cellName.Equals(turnPositionName));
        }).First();

        this.makeTurn(turn);


        if (IS_OPPONENT_PLAYER_AI)
        {
            StartCoroutine("coroutineMakeTurnByAI");
        }
    }
예제 #10
0
        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);
        }
    private void populateBallsList()
    {
        this._ballsMatrix = new GameObject[8, 8];
        {
            var d4Pos = BoardCoordinatesConverter.CellNameToCoordinates("D4");
            this._ballsMatrix[d4Pos.Row, d4Pos.Column] = this._ballD4;
            this._mutableBoardModel.TryConsumeCellByBlackPlayer(d4Pos);

            var d5Pos = BoardCoordinatesConverter.CellNameToCoordinates("D5");
            this._ballsMatrix[d5Pos.Row, d5Pos.Column] = this._ballD5;
            this._mutableBoardModel.TryConsumeCellByWhitePlayer(d5Pos);

            var e4Pos = BoardCoordinatesConverter.CellNameToCoordinates("E4");
            this._ballsMatrix[e4Pos.Row, e4Pos.Column] = this._ballE4;
            this._mutableBoardModel.TryConsumeCellByWhitePlayer(e4Pos);

            var e5Pos = BoardCoordinatesConverter.CellNameToCoordinates("E5");
            this._ballsMatrix[e5Pos.Row, e5Pos.Column] = this._ballE5;
            this._mutableBoardModel.TryConsumeCellByBlackPlayer(e5Pos);

            this._mutableBoardModel.IsTurnOfBlackPlayer = true;
        }
    }
예제 #12
0
        public void TestTurnsForInitialState()
        {
            var turns = this._sut.GetValidTurnsForBoard(this._initialBoard);

            Assert.IsNotNull(turns);

            var turnsCount = turns.Count();

            Assert.AreEqual(4, turnsCount);

            IReversiTurn     turn        = null;
            ICellCoordinates position    = null;
            ICellCoordinates flippedCell = null;

            {
                position = BoardCoordinatesConverter.CellNameToCoordinates("C5");
                turn     = turns.Where
                           (
                    t => (t.Position.Row == position.Row &&
                          t.Position.Column == position.Column)
                           ).First();

                Assert.IsNotNull(turn);
                Assert.AreEqual(1, turn.PositionsOfFlippedItems.Count());

                flippedCell = turn.PositionsOfFlippedItems.First();
                Assert.IsNotNull(flippedCell);

                string flippedCellName = BoardCoordinatesConverter.CoordinatesToCellName(flippedCell);
                Assert.AreEqual("D5", flippedCellName);
            }

            {
                position = BoardCoordinatesConverter.CellNameToCoordinates("D6");
                turn     = turns.Where
                           (
                    t => (t.Position.Row == position.Row &&
                          t.Position.Column == position.Column)
                           ).First();

                Assert.IsNotNull(turn);
                Assert.AreEqual(1, turn.PositionsOfFlippedItems.Count());

                flippedCell = turn.PositionsOfFlippedItems.First();
                Assert.IsNotNull(flippedCell);

                string flippedCellName = BoardCoordinatesConverter.CoordinatesToCellName(flippedCell);
                Assert.AreEqual("D5", flippedCellName);
            }

            {
                position = BoardCoordinatesConverter.CellNameToCoordinates("F4");
                turn     = turns.Where
                           (
                    t => (t.Position.Row == position.Row &&
                          t.Position.Column == position.Column)
                           ).First();

                Assert.IsNotNull(turn);
                Assert.AreEqual(1, turn.PositionsOfFlippedItems.Count());

                flippedCell = turn.PositionsOfFlippedItems.First();
                Assert.IsNotNull(flippedCell);

                string flippedCellName = BoardCoordinatesConverter.CoordinatesToCellName(flippedCell);
                Assert.AreEqual("E4", flippedCellName);
            }

            {
                position = BoardCoordinatesConverter.CellNameToCoordinates("E3");
                turn     = turns.Where
                           (
                    t => (t.Position.Row == position.Row &&
                          t.Position.Column == position.Column)
                           ).First();

                Assert.IsNotNull(turn);
                Assert.AreEqual(1, turn.PositionsOfFlippedItems.Count());

                flippedCell = turn.PositionsOfFlippedItems.First();
                Assert.IsNotNull(flippedCell);

                string flippedCellName = BoardCoordinatesConverter.CoordinatesToCellName(flippedCell);
                Assert.AreEqual("E4", flippedCellName);
            }
        }
예제 #13
0
        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);
        }