Example #1
0
        /// <summary>
        /// Verifies if the passed in position is valid or not.
        /// </summary>
        /// <param name="battleship">battleship</param>
        /// <param name="boardSize">boardSize</param>
        /// <returns>Tuple<bool, int, Orientation></returns>
        public Tuple <bool, int, Orientation> IsValidPosition(BattleshipRequest battleship, int boardSize)
        {
            int         shipEndCoordinate = -1;
            Orientation orientation;

            switch (battleship?.Orientation?.ToUpper())
            {
            case Constants.Orientation.VERTICAL:
                shipEndCoordinate = battleship.Position.Y + battleship.Length - 1;
                orientation       = Orientation.Vertical;
                break;

            case Constants.Orientation.HORIZONTAL:
                shipEndCoordinate = battleship.Position.X + battleship.Length - 1;
                orientation       = Orientation.Horizontal;
                break;

            default:
                orientation = Orientation.Invalid;
                break;
            }
            var validRange = Enumerable.Range(1, boardSize);

            var isValid = validRange.Contains(battleship.Position.X) &&
                          validRange.Contains(battleship.Position.Y) &&
                          validRange.Contains(shipEndCoordinate);

            return(Tuple.Create(isValid, shipEndCoordinate, orientation));
        }
        /// <summary>
        /// Add ship to the board.
        /// </summary>
        /// <param name="battleship">battleship</param>
        /// <returns>string</returns>
        public string PlaceShipOnBoard(BattleshipRequest battleship)
        {
            var(isCreated, size, boardCells) = validationService.IsBattleBoardCreated();
            if (!isCreated)
            {
                return(Constants.Messages.BOARD_NOT_CREATED_YET);
            }

            if (validationService.IsShipPlacedOnBoard())
            {
                return(Constants.Messages.SHIP_IS_ALREADY_PLACED);
            }

            var(isValid, endCoordinate, orientation) = validationService.IsValidPosition(battleship, size - 1);
            if (!isValid)
            {
                return(Constants.Messages.CANNOT_PLACE_OUTSIDE_BOARD);
            }

            if (!validationService.IsBoardCellEmpty(battleship.Position.X,
                                                    battleship.Position.Y,
                                                    endCoordinate, orientation))
            {
                return(Constants.Messages.CELL_IS_OCCUPIED);
            }

            var battleBoard = new BattleBoard
            {
                BoardCells = boardCells,
                BoardSize  = size - 1,
                Length     = battleship.Length
            };

            if (orientation.Equals(Orientation.Horizontal))
            {
                for (var i = battleship.Position.X; i <= endCoordinate; i++)
                {
                    battleBoard.BoardCells[i, battleship.Position.Y] = new BoardCell {
                        IsEmpty = false, IsHit = false
                    };
                }
            }

            if (orientation.Equals(Orientation.Vertical))
            {
                for (var i = battleship.Position.Y; i <= endCoordinate; i++)
                {
                    battleBoard.BoardCells[battleship.Position.X, i] = new BoardCell {
                        IsEmpty = false, IsHit = false
                    };
                }
            }

            cacheService.CreateOrUpdateCache(Constants.CacheKeys.IS_SHIP_PLACED_ON_BOARD, true);
            cacheService.CreateOrUpdateCache(Constants.CacheKeys.BATTLE_BOARD, battleBoard);
            return(string.Empty);
        }
        public IActionResult PlaceShipOnBoard(BattleshipRequest battleship)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var response = trackerService.PlaceShipOnBoard(battleship);

            return(Ok(response));
        }