Esempio n. 1
0
        public void PlayerMoveMustActivateCorrectCoordinateInGrid()
        {
            // Given
            Player     playerA = new Player();
            Player     playerB = new Player();
            ITicTacToe game    = new TicTacToeImpl();
            IGrid      grid;

            // When
            game.StartGame(this.grid, playerA, playerB);
            game.PlayerMove(playerA, new Coordinate(0, 0));
            game.PlayerMove(playerA, new Coordinate(0, 1));
            game.PlayerMove(playerA, new Coordinate(1, 0));
            grid = game.GetGrid();

            // Then
            Assert.That(grid.GetGrid()[0, 0].GetStatus() == CellPlayer.PLAYER_A, "Cell must be assigned to Player A");
            Assert.That(grid.GetGrid()[0, 1].GetStatus() == CellPlayer.PLAYER_A, "Cell must be assigned to Player A");
            Assert.That(grid.GetGrid()[0, 2].GetStatus() == CellPlayer.NONE, "Cell must not be assigned");
            Assert.That(grid.GetGrid()[1, 0].GetStatus() == CellPlayer.PLAYER_A, "Cell must be assigned to Player A");
            Assert.That(grid.GetGrid()[1, 1].GetStatus() == CellPlayer.NONE, "Cell must not be assigned");
            Assert.That(grid.GetGrid()[1, 2].GetStatus() == CellPlayer.NONE, "Cell must not be assigned");
            Assert.That(grid.GetGrid()[2, 0].GetStatus() == CellPlayer.NONE, "Cell must not be assigned");
            Assert.That(grid.GetGrid()[2, 1].GetStatus() == CellPlayer.NONE, "Cell must not be assigned");
            Assert.That(grid.GetGrid()[2, 2].GetStatus() == CellPlayer.NONE, "Cell must not be assigned");
        }
Esempio n. 2
0
        public void TicTacToeImplMustHaveGuidId()
        {
            // Given
            ITicTacToe game = new TicTacToeImpl();

            // When / Then
            Assert.DoesNotThrow(() => Guid.Parse(game.GetId().ToString()), "Game's GuidId must be a Guid");
        }
Esempio n. 3
0
        public void TicTacToeMoveBeforeStart()
        {
            // Given
            ITicTacToe game = new TicTacToeImpl();
            Player     nextPlayer;

            // When/Then
            Assert.Throws <NotValidStateException>(() => nextPlayer = game.PlayerMove(new Player(), new Coordinate(0, 0)), "A movement before initialization must throw NotValidStateException");
        }
Esempio n. 4
0
        /// <inheritdoc/>
        public long NewGame(BoringToeNewGameRequest request)
        {
            Player     playerA = FindPlayerInDatabase(request.PlayerAId, ErrorCode.PLAYER_A_NOT_EXISTING);
            Player     playerB = FindPlayerInDatabase(request.PlayerBId, ErrorCode.PLAYER_B_NOT_EXISTING);
            ITicTacToe game    = new TicTacToeImpl();
            IGrid      grid    = new Grid();

            game.StartGame(grid, playerA, playerB);
            return(_boringToeRepository.AddGame((TicTacToeImpl)game));
        }
Esempio n. 5
0
        public void PlayerAMustBeDifferentFromPlayerB()
        {
            // Given
            Player     player = new Player();
            ITicTacToe game   = new TicTacToeImpl();

            // When/Then
            NotValidValueException exc = Assert.Throws <NotValidValueException>(() => game.StartGame(grid, player, player), "Initializing with same player must return exception");

            Assert.AreEqual(ErrorCode.PLAYERS_ARE_SAME, exc.ErrorCode, "Exception ErrorCode must be PLAYERS_ARE_SAME");
        }
        public void FirstGameAddMustReturn1()
        {
            // Given
            TicTacToeImpl        game = new TicTacToeImpl();
            IBoringToeRepository repo = new BoringToeSetRepository();
            long id;

            // When
            id = repo.AddGame(game);

            // Then
            Assert.IsTrue(id == 1, "First game's ID must be 1");
        }
Esempio n. 7
0
        public void PlayersMustNotBeNull()
        {
            // Given
            Player     player = new Player();
            ITicTacToe game   = new TicTacToeImpl();

            // When/Then
            NotValidValueException exc;

            exc = Assert.Throws <NotValidValueException>(() => game.StartGame(grid, null, player), "If a player is null on initialization, must return exception");
            Assert.AreEqual(ErrorCode.NULL_VALUE_NOT_ALLOWED, exc.ErrorCode, "Exception ErrorCode must be NULL_VALUE_NOT_ALLOWED");
            exc = Assert.Throws <NotValidValueException>(() => game.StartGame(grid, player, null), "If a player is null on initialization, must return exception");
            Assert.AreEqual(ErrorCode.NULL_VALUE_NOT_ALLOWED, exc.ErrorCode, "Exception ErrorCode must be NULL_VALUE_NOT_ALLOWED");
        }
Esempio n. 8
0
        public void MovementPlayerMustMatchInitializedOnes()
        {
            // Given
            Player     playerA = new Player();
            Player     playerB = new Player();
            ITicTacToe game    = new TicTacToeImpl();
            Player     firstPlayer;

            // When
            firstPlayer = game.StartGame(grid, playerA, playerB);

            // Then
            Assert.IsTrue(firstPlayer.Equals(playerA) || firstPlayer.Equals(playerB), "First player must be Player A or Player B");
        }
        public void AfterAddingAGameItMustBeAccessableByGuid()
        {
            // Given
            IBoringToeRepository repo = new BoringToeSetRepository();
            long       id;
            ITicTacToe game = new TicTacToeImpl();
            ITicTacToe testGame;

            // When
            id       = repo.AddGame(game);
            testGame = repo.GetGameByGuid(game.GetId());

            // Then
            Assert.AreEqual(game, testGame, "Returned game must be the same as added one");
        }
        public void IfAGameAlreadyExistsItMustReturnDuplicatedValueException()
        {
            // Given
            TicTacToeImpl        game1 = new TicTacToeImpl();
            IBoringToeRepository repo  = new BoringToeSetRepository();
            long id;

            // When
            id = repo.AddGame(game1);

            // Then
            DuplicatedValueException excep = Assert.Throws <DuplicatedValueException>(() => id = repo.AddGame(game1), "Must raise an exception if a game already exists");

            Assert.AreEqual(excep.ErrorCode, ErrorCode.VALUE_ALREADY_EXISTS_IN_DATABASE, "Exception's error code must be VALUE_ALREADY_EXISTS_IN_DATABASE");
        }
        public void SecondGameAddMustReturn2()
        {
            // Given
            TicTacToeImpl        game1 = new TicTacToeImpl();
            TicTacToeImpl        game2 = new TicTacToeImpl();
            IBoringToeRepository repo  = new BoringToeSetRepository();
            long id;

            // When
            id = repo.AddGame(game1);
            id = repo.AddGame(game2);

            // Then
            Assert.IsTrue(id == 2, "Second game's ID must be 2");
        }
Esempio n. 12
0
        public void InexistingPlayerMustThrowException()
        {
            // Given
            Player     playerA = new Player();
            Player     playerB = new Player();
            Player     other   = new Player();
            ITicTacToe game    = new TicTacToeImpl();

            // When
            game.StartGame(grid, playerA, playerB);

            // Then
            NotValidValueException exc = Assert.Throws <NotValidValueException>(() => game.PlayerMove(other, new Coordinate(0, 0)), "Move must return an exception is player is not in initialization");

            Assert.AreEqual(ErrorCode.PLAYER_NOT_EXISTS, exc.ErrorCode, "Exception ErrorCode must be PLAYER_NOT_EXISTS");
        }
Esempio n. 13
0
        public void GridIsFullMustRaiseAGameOverException()
        {
            // Given
            ITicTacToe game    = new TicTacToeImpl();
            Player     playerA = new Player();
            Player     playerB = new Player();

            igridMock.Setup(m => m.IsFull()).Returns(true);
            igridMock.Setup(m => m.Check()).Returns(CellPlayer.NONE);

            // When
            game.StartGame(igridMock.Object, playerA, playerB);

            // Then
            Assert.Throws <GameOverException>(() => game.PlayerMove(playerA, new Coordinate(0, 0)), "PlayerMove must raise GameOverException");
        }
        public void GettingADeletedGameMustRaiseException()
        {
            // Given
            IBoringToeRepository repo      = new BoringToeSetRepository();
            TicTacToeImpl        TicTacToe = new TicTacToeImpl();
            long id;

            // When
            id = repo.AddGame(TicTacToe);
            repo.DeleteGame(id);

            // When / Then
            NotExistingValueException excep = Assert.Throws <NotExistingValueException>(() => repo.GetGameById(id), "Getting an inexisting game must raise an exception");

            Assert.AreEqual(excep.ErrorCode, ErrorCode.VALUE_NOT_EXISTING_IN_DATABASE, "Error code must be VALUE_NOT_EXISTING_IN_DATABASE");
        }
Esempio n. 15
0
        public void StartGameAndPlayerMoveMustReturnNextPlayer()
        {
            // Given
            Player     playerA = new Player();
            Player     playerB = new Player();
            ITicTacToe game    = new TicTacToeImpl();
            Player     firstPlayer;
            Player     nextPlayer;

            // When
            firstPlayer = game.StartGame(grid, playerA, playerB);
            nextPlayer  = game.PlayerMove(firstPlayer, new Coordinate(0, 0));

            // Then
            Assert.IsTrue(firstPlayer.Equals(playerA) || firstPlayer.Equals(playerB), "First player must be Player A or Player B");
            Assert.IsFalse(firstPlayer.Equals(nextPlayer), "First player must be different from next player");
        }
Esempio n. 16
0
        public void SameMovementMustThrowPlayerMovementExceptionWithMustRetry()
        {
            // Given
            Player     playerA = new Player();
            Player     playerB = new Player();
            Player     other   = new Player();
            ITicTacToe game    = new TicTacToeImpl();

            // When
            game.StartGame(grid, playerA, playerB);
            game.PlayerMove(playerA, new Coordinate(0, 0));

            // Then
            PlayerMovementException pme = Assert.Throws <PlayerMovementException>(() => game.PlayerMove(playerB, new Coordinate(0, 0)), "If the movement has been already made, must raise an exception");

            Assert.AreEqual(pme.ErrorCode, ErrorCode.MOVEMENT_ERROR_MUST_RETRY, "Exception must have MOVEMENT_ERROR_MUST_RETRY");
        }
Esempio n. 17
0
        public void PlayerMoveMustReturnAnExceptionIfGridCheckReturnsNotValidPlayerValue()
        {
            // Given
            ITicTacToe game    = new TicTacToeImpl();
            Player     playerA = new Player();
            Player     playerB = new Player();

            igridMock.Setup(m => m.Check()).Returns(CellPlayer.TEST_OTHER_PLAYER);

            // When
            game.StartGame(igridMock.Object, playerA, playerB);

            // Then
            NotValidStateException exc = Assert.Throws <NotValidStateException>(() => game.PlayerMove(playerA, new Coordinate(0, 0)), "PlayerMove must raise NotValidStateException");

            Assert.AreEqual(exc.ErrorCode, ErrorCode.OUT_OF_RANGE, "Exception must have OUT_OF_RANGE error code");
        }
Esempio n. 18
0
        public void PlayerMoveMustSpreadExceptionWhenGridSetReturnsNotValidStateExceptionWithUnknownErrorCode()
        {
            // Given
            ITicTacToe game    = new TicTacToeImpl();
            Player     playerA = new Player();
            Player     playerB = new Player();

            igridMock.Setup(m => m.Set(It.IsAny <int>(), It.IsAny <int>(), It.IsAny <CellPlayer>())).Throws(new NotValidStateException("", ErrorCode.UNKNOWN));

            // When
            game.StartGame(igridMock.Object, playerA, playerB);

            // Then
            NotValidStateException exc = Assert.Throws <NotValidStateException>(() => game.PlayerMove(playerA, new Coordinate(0, 0)), "PlayerMove must raise NotValidStateException");

            Assert.AreEqual(exc.ErrorCode, ErrorCode.UNKNOWN, "Exception must have UNKNOWN error code");
        }
Esempio n. 19
0
        public void TicTacToeHappyPath()
        {
            // Given
            Player     playerA = new Player();
            Player     playerB = new Player();
            ITicTacToe game    = new TicTacToeImpl();

            // When
            game.StartGame(grid, playerA, playerB);
            game.PlayerMove(playerA, new Coordinate(0, 0));
            game.PlayerMove(playerA, new Coordinate(0, 1));

            // Then
            TicTacToeGameOverException rest = Assert.Throws <TicTacToeGameOverException>(() => game.PlayerMove(playerA, new Coordinate(0, 2)), "Winning the game throws TicTacToeGameOverException");

            Assert.AreEqual(rest.Player, playerA, "Winning player must be Player A");
            Assert.AreEqual(rest.Winner, true, "Winner flag must be set to true");
        }