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);
        }
        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);
        }
Exemplo n.º 3
0
        public void Attack_TargetNotExist()
        {
            using (AutoMock mock = AutoMock.GetLoose())
            {
                string expectedErrorMsg = "Can't find this target";
                mock.Mock <IBattleshipRepository>()
                .Setup(x => x.GetGameById(1))
                .ReturnsAsync(CreateSomeGames().FirstOrDefault(x => x.GameId == 1));
                GameService          cls = mock.Create <GameService>();
                AttackCoordinatesDto attackCoordinates = new AttackCoordinatesDto()
                {
                    Row    = 10,
                    Column = 11
                };

                var ex = Record.ExceptionAsync(() => cls.Attack(1, attackCoordinates)).Result;

                Assert.NotNull(ex);
                Assert.IsType <Exception>(ex);
                if (ex is Exception exception)
                {
                    Assert.Equal(expectedErrorMsg, exception.Message);
                }
            }
        }
Exemplo n.º 4
0
        public async Task <string> Attack(int gameId, AttackCoordinatesDto coordinates)
        {
            Game gameFromRepo = await _repo.GetGameById(gameId);

            Panel targetPanel = gameFromRepo.GameBoard.Panels.GetPanel(coordinates.Row, coordinates.Column);
            Panel firingPanel = gameFromRepo.FiringBoard.Panels.GetPanel(coordinates.Row, coordinates.Column);

            if (targetPanel == null || firingPanel == null)
            {
                throw new Exception("Can't find this target");
            }
            if (firingPanel.OccupationType != OccupationType.Empty)
            {
                throw new Exception("You attacked this coordinates before");
            }
            if (targetPanel.IsOccupied)
            {
                firingPanel.OccupationType = OccupationType.Hit;
                await _repo.SaveAll();

                return(OccupationType.Hit.ToString());
            }
            else
            {
                firingPanel.OccupationType = OccupationType.Miss;
                await _repo.SaveAll();

                return(OccupationType.Miss.ToString());
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Attack(int gameId, AttackCoordinatesDto coordinates)
        {
            if (!await _gameService.IsExists(gameId))
            {
                return(NotFound());
            }

            string result = await _gameService.Attack(gameId, coordinates);

            return(Ok(result)); //return hit or miss
        }
        public async Task Attack_GameNotExist()
        {
            GameController       gameController       = new GameController(_gameService, _mapper);
            AttackCoordinatesDto attackCoordinatesDto = new AttackCoordinatesDto()
            {
                Row    = 1,
                Column = 1
            };

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

            Assert.IsType <NotFoundResult>(result);
        }
Exemplo n.º 7
0
        public void Attack_HitOrMiss(int row, int column, string expectedResult)
        {
            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);
                GameService          cls = mock.Create <GameService>();
                AttackCoordinatesDto attackCoordinates = new AttackCoordinatesDto()
                {
                    Row    = row,
                    Column = column
                };

                string actual = cls.Attack(1, attackCoordinates).Result;

                Assert.Equal(expectedResult, actual);
                mock.Mock <IBattleshipRepository>()
                .Verify(x => x.SaveAll(), Times.Exactly(1));
            }
        }