示例#1
0
        public ShipPlacements PlaceShip(ShipRequest request)
        {
            if (_currentShipIndex > 4)
            {
                throw new Exception("You can not add another ship, 5 is the limit!");
            }

            if (!IsValidCoordinate(request.Coordinate))
            {
                return(ShipPlacements.NotEnoughSpace);
            }

            Battleships newShip = ShipCreation.CreateShip(request.ShipType);

            switch (request.Direction)
            {
            case ShipDirections.Down:
                return(PlaceShipDown(request.Coordinate, newShip));

            case ShipDirections.Up:
                return(PlaceShipUp(request.Coordinate, newShip));

            case ShipDirections.Left:
                return(PlaceShipLeft(request.Coordinate, newShip));

            default:
                return(PlaceShipRight(request.Coordinate, newShip));
            }
        }
示例#2
0
        private ShipPlacements PlaceShipDown(Coordinate coordinate, Battleships newShip)
        {
            // y coordinate gets bigger
            int positionIndex = 0;
            int maxX          = coordinate.XCoordinate + newShip.PanelPositions.Length;

            for (int i = coordinate.XCoordinate; i < maxX; i++)
            {
                var currentCoordinate = new Coordinate(i, coordinate.YCoordinate);

                if (!IsValidCoordinate(currentCoordinate))
                {
                    return(Enums.ShipPlacements.NotEnoughSpace);
                }

                if (OverlapsAnotherShip(currentCoordinate))
                {
                    return(Enums.ShipPlacements.Overlap);
                }

                newShip.PanelPositions[positionIndex] = currentCoordinate;
                positionIndex++;
            }

            AddShipToBoard(newShip);
            return(Enums.ShipPlacements.Ok);
        }
示例#3
0
        private ShipPlacements PlaceShipLeft(Coordinate coordinate, Battleships newShip)
        {
            // y coordinate gets smaller
            int positionIndex = 0;
            int minY          = coordinate.YCoordinate - newShip.PanelPositions.Length;

            for (int i = coordinate.YCoordinate; i > minY; i--)
            {
                var currentCoordinate = new Coordinate(coordinate.XCoordinate, i);

                if (!IsValidCoordinate(currentCoordinate))
                {
                    return(Enums.ShipPlacements.NotEnoughSpace);
                }

                if (OverlapsAnotherShip(currentCoordinate))
                {
                    return(Enums.ShipPlacements.Overlap);
                }

                newShip.PanelPositions[positionIndex] = currentCoordinate;
                positionIndex++;
            }

            AddShipToBoard(newShip);
            return(Enums.ShipPlacements.Ok);
        }
示例#4
0
 private void AddShipToBoard(Battleships newShip)
 {
     Ships[_currentShipIndex] = newShip;
     _currentShipIndex++;
 }