public async Task AddBattleship_IsGameOn()
        {
            string           expectedErrMsg = "This game already started, adding new ship during the game is forbidden";
            GameController   gameController = new GameController(_gameService, _mapper);
            ShipForAddingDto shipForAdding  = new ShipForAddingDto()
            {
                StartRow     = 1,
                StartColumn  = 1,
                ShipLength   = 3,
                IsVertically = true
            };
            AttackCoordinatesDto attackCoordinatesDto = new AttackCoordinatesDto()
            {
                Row    = 1,
                Column = 1
            };

            var newGameObjectResult = await gameController.CreateBoard() as OkObjectResult;

            var gameForReturn = newGameObjectResult.Value as GameForReturnDto;
            await gameController.Attack(gameForReturn.GameId, attackCoordinatesDto);

            var result = await gameController.AddBattleship(gameForReturn.GameId, shipForAdding);

            var objectResult = Assert.IsType <BadRequestObjectResult>(result);

            Assert.Equal(expectedErrMsg, objectResult.Value);
        }
        public async Task AddBattleship_Success()
        {
            GameController   gameController = new GameController(_gameService, _mapper);
            ShipForAddingDto shipForAdding  = new ShipForAddingDto()
            {
                StartRow     = 1,
                StartColumn  = 1,
                ShipLength   = 3,
                IsVertically = true
            };

            var newGameObjectResult = await gameController.CreateBoard() as OkObjectResult;

            var gameForReturn = newGameObjectResult.Value as GameForReturnDto;
            await gameController.AddBattleship(gameForReturn.GameId, shipForAdding);

            var result = await gameController.GetGame(gameForReturn.GameId) as OkObjectResult;

            Game         gameResult        = Assert.IsType <Game>(result.Value);
            List <Panel> occupiedPanelList = gameResult.GameBoard.Panels
                                             .Where(p => p.OccupationType == Helpers.OccupationType.Ship).OrderBy(p => p.Row).ToList();

            Assert.Equal(3, occupiedPanelList.Count);
            Assert.Equal(1, occupiedPanelList[0].Row);
            Assert.Equal(1, occupiedPanelList[0].Column);
            Assert.Equal(2, occupiedPanelList[1].Row);
            Assert.Equal(1, occupiedPanelList[1].Column);
            Assert.Equal(3, occupiedPanelList[2].Row);
            Assert.Equal(1, occupiedPanelList[2].Column);
        }
예제 #3
0
        public void AddShip_OccupiedByAnotherShip()
        {
            using (AutoMock mock = AutoMock.GetLoose())
            {
                string expectedErrorMsg = "You can't add ship on the location because it's occupied by another ship";
                mock.Mock <IBattleshipRepository>()
                .Setup(x => x.GetGameById(1))
                .ReturnsAsync(CreateSomeGames().FirstOrDefault(x => x.GameId == 1));
                ShipForAddingDto shipForAddingDto = new ShipForAddingDto()
                {
                    StartRow     = 5,
                    StartColumn  = 1,
                    ShipLength   = 3,
                    IsVertically = true
                };
                GameService cls = mock.Create <GameService>();

                var ex = Record.ExceptionAsync(() => cls.AddShip(1, shipForAddingDto)).Result;

                Assert.NotNull(ex);
                Assert.IsType <Exception>(ex);
                if (ex is Exception exception)
                {
                    Assert.Equal(expectedErrorMsg, exception.Message);
                }
            }
        }
        public async Task Attack_Success(int attackRow, int attackColumn, string expectedResult)
        {
            GameController   gameController = new GameController(_gameService, _mapper);
            ShipForAddingDto shipForAdding  = new ShipForAddingDto()
            {
                StartRow     = 5,
                StartColumn  = 1,
                ShipLength   = 3,
                IsVertically = true
            };
            AttackCoordinatesDto attackCoordinatesDto = new AttackCoordinatesDto()
            {
                Row    = attackRow,
                Column = attackColumn
            };

            var newGameObjectResult = await gameController.CreateBoard() as OkObjectResult;

            var gameForReturn = newGameObjectResult.Value as GameForReturnDto;
            await gameController.AddBattleship(gameForReturn.GameId, shipForAdding);

            var result = await gameController.Attack(gameForReturn.GameId, attackCoordinatesDto);

            var objectResult = Assert.IsType <OkObjectResult>(result);
            var actualResult = Assert.IsType <string>(objectResult.Value);

            Assert.Equal(expectedResult, actualResult);
        }
예제 #5
0
        public void AddShip_NotEntireOnTheBoard(int startRow, int startColumn, int shipLengh, bool isVertically)
        {
            using (AutoMock mock = AutoMock.GetLoose())
            {
                string expectedErrorMsg = $"This ship is not entirely on the board";
                mock.Mock <IBattleshipRepository>()
                .Setup(x => x.GetGameById(1))
                .ReturnsAsync(CreateSomeGames().FirstOrDefault(x => x.GameId == 1));
                ShipForAddingDto shipForAddingDto = new ShipForAddingDto()
                {
                    StartRow     = startRow,
                    StartColumn  = startColumn,
                    ShipLength   = shipLengh,
                    IsVertically = isVertically
                };
                GameService cls = mock.Create <GameService>();

                var ex = Record.ExceptionAsync(() => cls.AddShip(1, shipForAddingDto)).Result;

                Assert.NotNull(ex);
                Assert.IsType <Exception>(ex);
                if (ex is Exception exception)
                {
                    Assert.Equal(expectedErrorMsg, exception.Message);
                }
            }
        }
예제 #6
0
        public void AddShip_GameNotExist()
        {
            using (AutoMock mock = AutoMock.GetLoose())
            {
                string expectedErrorMsg = $"the game 10 does not exist, adding this ship failed";
                mock.Mock <IBattleshipRepository>()
                .Setup(x => x.GetGameById(10))
                .ReturnsAsync(CreateSomeGames().FirstOrDefault(x => x.GameId == 100));
                ShipForAddingDto shipForAddingDto = new ShipForAddingDto()
                {
                    StartRow     = 1,
                    StartColumn  = 1,
                    ShipLength   = 3,
                    IsVertically = true
                };
                GameService cls = mock.Create <GameService>();

                var ex = Record.ExceptionAsync(() => cls.AddShip(10, shipForAddingDto)).Result;

                Assert.NotNull(ex);
                Assert.IsType <Exception>(ex);
                if (ex is Exception exception)
                {
                    Assert.Equal(expectedErrorMsg, exception.Message);
                }
            }
        }
예제 #7
0
        public void AddShip_Success(int startRow, int startColumn, int shipLength, bool isVertically)
        {
            using (AutoMock mock = AutoMock.GetLoose())
            {
                mock.Mock <IBattleshipRepository>()
                .Setup(x => x.GetGameById(1))
                .ReturnsAsync(CreateSomeGames().FirstOrDefault(x => x.GameId == 1));
                mock.Mock <IBattleshipRepository>()
                .Setup(x => x.SaveAll())
                .ReturnsAsync(true);
                ShipForAddingDto shipForAddingDto = new ShipForAddingDto()
                {
                    StartRow     = startRow,
                    StartColumn  = startColumn,
                    ShipLength   = shipLength,
                    IsVertically = isVertically
                };
                GameService cls = mock.Create <GameService>();

                cls.AddShip(1, shipForAddingDto).Wait();

                mock.Mock <IBattleshipRepository>()
                .Verify(x => x.SaveAll(), Times.Exactly(1));
            }
        }
        public async Task AddBattleship_GameNotExist()
        {
            GameController   gameController = new GameController(_gameService, _mapper);
            ShipForAddingDto shipForAdding  = new ShipForAddingDto()
            {
                StartRow     = 1,
                StartColumn  = 1,
                ShipLength   = 3,
                IsVertically = true
            };

            var result = await gameController.AddBattleship(1, shipForAdding);

            Assert.IsType <NotFoundResult>(result);
        }
예제 #9
0
        public async Task <IActionResult> AddBattleship(int gameId, ShipForAddingDto shipForAddingDto)
        {
            if (!await _gameService.IsExists(gameId))
            {
                return(NotFound());
            }

            if (await _gameService.IsGameOn(gameId))
            {
                return(BadRequest("This game already started, adding new ship during the game is forbidden"));
            }

            await _gameService.AddShip(gameId, shipForAddingDto);

            return(Ok(shipForAddingDto));
        }
예제 #10
0
        public async Task AddShip(int gameId, ShipForAddingDto shipForAddingDto)
        {
            Game game = await _repo.GetGameById(gameId);

            if (game == null)
            {
                throw new Exception($"the game {gameId} does not exist, adding this ship failed");
            }

            int startRow = shipForAddingDto.StartRow;
            int startColumn = shipForAddingDto.StartColumn;
            int endRow, endColumn;

            if (shipForAddingDto.IsVertically)
            {
                endRow    = startRow + shipForAddingDto.ShipLength - 1;
                endColumn = startColumn;
            }
            else
            {
                endRow    = startRow;
                endColumn = startColumn + shipForAddingDto.ShipLength - 1;
            }

            //Check if entirely on the board
            if (startRow < 1 || startColumn < 1 || endRow > game.Row || endColumn > game.Column)
            {
                throw new Exception("This ship is not entirely on the board");
            }
            //Check if specified panels are occupied
            List <Panel> affectedPanels = game.GameBoard.Panels.Range(startRow, startColumn, endRow, endColumn);

            if (affectedPanels.Any(x => x.IsOccupied))
            {
                throw new Exception("You can't add ship on the location because it's occupied by another ship");
            }

            foreach (Panel panel in affectedPanels)
            {
                panel.OccupationType = OccupationType.Ship;
            }

            if (!await _repo.SaveAll())
            {
                throw new Exception("Adding ship in the game failed");
            }
        }