Beispiel #1
0
        public void PlaceShip(BaseShip ship, int rowIndex, int colIndex, ShipPlacementMode mode = ShipPlacementMode.All)
        {
            bool isEnoughPlaceInRightColumnDirection = colIndex + ship.Squares <= GameSettings.GridArrayMaxSize;
            bool isEnoughPlaceInLeftColumnDirection  = colIndex - ship.Squares >= 0;
            bool isEnoughPlaceInTopRowDirection      = rowIndex - ship.Squares >= 0;
            bool isEnoughPlaceInBottomRowDirection   = rowIndex + ship.Squares <= GameSettings.GridArrayMaxSize;
            List <GridLocation> currentShipPlacement = new List <GridLocation>();

            if (isEnoughPlaceInRightColumnDirection && (mode == ShipPlacementMode.Horizontally || mode == ShipPlacementMode.All))
            {
                for (int currentColIndex = colIndex; currentColIndex < (colIndex + ship.Squares); currentColIndex++)
                {
                    PlaceShip(ship, rowIndex, currentColIndex, currentShipPlacement);
                    if (!ship.IsPlaced)
                    {
                        break;
                    }
                }
            }
            if (!ship.IsPlaced && isEnoughPlaceInLeftColumnDirection && (mode == ShipPlacementMode.Horizontally || mode == ShipPlacementMode.All))
            {
                for (int currentColIndex = colIndex; currentColIndex > (colIndex - ship.Squares); currentColIndex--)
                {
                    PlaceShip(ship, rowIndex, currentColIndex, currentShipPlacement);
                    if (!ship.IsPlaced)
                    {
                        break;
                    }
                }
            }
            if (!ship.IsPlaced && isEnoughPlaceInTopRowDirection && (mode == ShipPlacementMode.Vertically || mode == ShipPlacementMode.All))
            {
                for (int currentRowIndex = rowIndex; currentRowIndex > (rowIndex - ship.Squares); currentRowIndex--)
                {
                    PlaceShip(ship, currentRowIndex, colIndex, currentShipPlacement);
                    if (!ship.IsPlaced)
                    {
                        break;
                    }
                }
            }
            if (!ship.IsPlaced && isEnoughPlaceInBottomRowDirection && (mode == ShipPlacementMode.Vertically || mode == ShipPlacementMode.All))
            {
                for (int currentRowIndex = rowIndex; currentRowIndex < (rowIndex + ship.Squares); currentRowIndex++)
                {
                    PlaceShip(ship, currentRowIndex, colIndex, currentShipPlacement);
                    if (!ship.IsPlaced)
                    {
                        break;
                    }
                }
            }
            currentShipPlacement = null;
        }
Beispiel #2
0
        public void PlaceShip(BaseShip ship)
        {
            int tryCurrent = 0;
            int tryMax     = 10;

            while (tryCurrent <= tryMax && !ship.IsPlaced)
            {
                Random            random = new Random();
                ShipPlacementMode mode   = GetMode(tryCurrent, random);
                int columnPosition       = random.Next(0, GameSettings.GridSize);
                int rowPosition          = GetRowPosition(tryCurrent, random, columnPosition);
                if (rowPosition >= 0)
                {
                    this.PlaceShip(ship, rowPosition, columnPosition, mode);
                }
                tryCurrent++;
            }
        }