Exemple #1
0
        public async Task Given_Ship_Must_Not_Be_Placed_If_Its_Already_Placed()
        {
            var boardId = Guid.NewGuid();
            var shipId  = Guid.NewGuid();

            _boardService.GetBoard(boardId).Returns(x => new Board {
                Id = boardId
            });
            _shipService.GetShip(shipId).Returns(x => new Ship {
                Id = shipId
            });
            _shipService.GetShipsPlaced(boardId).Returns(x => new List <Ship>
            {
                new Ship {
                    Id = shipId
                }
            });

            var response = await _shipController.PlaceShip(boardId, shipId, null);

            response.Should().BeOfType <BadRequestObjectResult>();

            ((BadRequestObjectResult)response).Value.Should().Be("Ship cannot be placed as requested ship already placed");
        }
Exemple #2
0
        public async Task <IActionResult> Attack(Guid boardId, AttackRequest attackRequest)
        {
            var boardTask = _boardService.GetBoard(boardId);
            var shipsTask = _shipService.GetShipsPlaced(boardId);

            await Task.WhenAll(boardTask, shipsTask);

            var board = await boardTask;

            if (board == null)
            {
                return(NotFound());
            }

            var  ships        = await shipsTask;
            var  attackStatus = AttackStatus.Miss;
            Guid?shipId       = null;

            foreach (var ship in ships)
            {
                var shipPosition = ship.Positions.FirstOrDefault(x => x.RowPosition == attackRequest.RowPosition &&
                                                                 x.ColumnPosition == attackRequest.ColumnPosition);

                if (shipPosition != null)
                {
                    attackStatus = AttackStatus.Hit;
                    shipId       = ship.Id;

                    var currentAttackPositions = await _attackService.GetAttackPositionsForShip(boardId, ship.Id);

                    if (ship.Size == (currentAttackPositions.Count + 1))
                    {
                        await _shipService.UpdateShipStatus(ship.Id, ShipStatus.Sunk);
                    }
                    break;
                }
            }

            await _attackService.UpdateAttackStatus(boardId, shipId, attackRequest, attackStatus);

            return(Ok(new AttackResponse {
                AttackStatus = attackStatus
            }));
        }
Exemple #3
0
        public async Task <IActionResult> PlaceShip(Guid boardId, Guid shipId, PlaceShipRequest placeShipRequest)
        {
            var boardTask       = _boardService.GetBoard(boardId);
            var shipTask        = _shipService.GetShip(shipId);
            var shipsPlacedTask = _shipService.GetShipsPlaced(boardId);

            await Task.WhenAll(boardTask, shipTask, shipsPlacedTask);

            var board       = await boardTask;
            var ship        = await shipTask;
            var shipsPlaced = await shipsPlacedTask;

            if (board == null || ship == null)
            {
                return(NotFound());
            }

            if (shipsPlaced.Any(x => x.Id == shipId))
            {
                return(BadRequest("Ship cannot be placed as requested ship already placed"));
            }

            var isShipToBePlacedWithInBoundaries = IsShipToBePlacedWithInBoundaries(board, ship, placeShipRequest);

            if (isShipToBePlacedWithInBoundaries == false)
            {
                return(BadRequest("Ship cannot be placed as positions should not cross boundaries"));
            }

            var shipPlacedPositions = shipsPlaced.SelectMany(x => x.Positions).ToList();
            var haveShipsAlreadyOccupiedThePosition = HaveShipsAlreadyOccupiedThePosition(ship, shipPlacedPositions, placeShipRequest);

            if (haveShipsAlreadyOccupiedThePosition)
            {
                return(BadRequest("Ship cannot be placed as the positions are occupied with ships"));
            }

            await _shipService.PlaceShip(boardId, ship, placeShipRequest);

            return(NoContent());
        }
Exemple #4
0
        public async Task <IActionResult> GetBoard(Guid boardId)
        {
            var boardTask           = _boardService.GetBoard(boardId);
            var shipsTask           = _shipService.GetShipsPlaced(boardId);
            var attackPositionsTask = _attackService.GetAttackPositions(boardId);

            await Task.WhenAll(boardTask, shipsTask, attackPositionsTask);

            var board = await boardTask;

            if (board == null)
            {
                return(NotFound());
            }

            var ships           = await shipsTask;
            var attackPositions = await attackPositionsTask;

            board.Ships   = ships;
            board.Attacks = attackPositions;

            return(Ok(board));
        }