Beispiel #1
0
        public ShipPlacement PlaceShip(PlaceShipRequest request)
        {
            if (_currentShipIndex > 4)
            {
                throw new Exception("You cannot add another ship, 5 is the limit!");
            }

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

            Ship newShip = ShipCreator.CreateShip(request.ShipType);

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

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

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

            default:
                return(PlaceShipRight(request.Coordinate, newShip));
            }
        }
Beispiel #2
0
        public ShipPlacement PlaceShip(PlaceShipRequest request)
        {
            if (_currentShipIndex > MAX_SHIPS - 1)
            {
                throw new Exception($"You can not add another ship, {MAX_SHIPS} is the limit!");
            }

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

            Ship newShip    = ShipCreator.CreateShip(request.ShipType);
            int  xIncrement = 0;
            int  yIncrement = 0;

            switch (request.Direction)
            {
            case ShipDirection.Down:
                yIncrement = 1;
                break;

            case ShipDirection.Up:
                yIncrement = -1;
                break;

            case ShipDirection.Left:
                xIncrement = -1;
                break;

            default:
                xIncrement = 1;
                break;
            }

            return(PlaceShip(request.Coordinate, newShip, xIncrement, yIncrement));
        }
Beispiel #3
0
        public ShipPlacement PlaceShip(PlaceShipRequest request, out Ship newShip)
        {
            newShip = null;
            if (_currentShipIndex > 7)
            {
                throw new Exception("You can not add another ship, 7 is the limit!");
            }

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

            Ship newTempShip = ShipCreator.CreateShip(request.ShipType, this);

            ShipPlacement Placement = newTempShip.SetShipPositions(request.Coordinate);

            if (Placement == ShipPlacement.Ok)
            {
                AddShipToBoard(newTempShip);
                newShip = newTempShip;
            }
            return(Placement);
        }