How to make random placement of ships do not overlap with ships when placed in battleships

I am creating game battleships on a computer and wondering how I can get ships not to overlap with each other when randomly placing them. My code now looks like this:

public class BattleshipSetup {

    public static class Boat {
        int size;
    }
    public static class AircraftCarrier extends Boat {
        public AircraftCarrier() {
            size = 5;
        }
    }
    public static class Battleship extends Boat {
        public Battleship() {
            size = 4;
        }
    }
    public static class Destroyer extends Boat {
        public Destroyer() {
            size = 3;
        }
    }
    public static class Submarine extends Boat {
        public Submarine() {
            size = 3;
        }
    }
    public static class PatrolShip extends Boat {
        public PatrolShip() {
            size = 2;
        }
    }
    public static class GridSetup {
        int[][] grid = {{0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}};
        public void setGrid() {
            Boat[] ship;
            ship = new Boat[5];
            ship[0] = new AircraftCarrier();
            ship[1] = new Battleship();
            ship[2] = new Destroyer();
            ship[3] = new Submarine();
            ship[4] = new PatrolShip();
            for (int i = 0; i < 5; i++) {
                int way = (int) (Math.random() * 2);
                if (way == 0) {
                    int x = (int) (Math.random() * 7);
                    int y = (int) (Math.random() * (7 - ship[i].size));
                    for (int j = 0; j < ship[i].size; j++) {
                        grid[x][y + j] = i + 1;
                    }
                }
                if (way == 1) {
                    int x = (int) (Math.random() * (7 - ship[i].size));
                    int y = (int) (Math.random() * 7);
                    for (int j = 0; j < ship[i].size; j++) {
                        grid[x + j][y] = i + 1;
                    }
                }
            }
        }
        public int[][] getGrid() {
            return grid;
        }
    }
}`

      

Now the point is that sometimes when he places ships, he partially moves one ship over another, and this may not be possible.

+3


source to share


2 answers


I would use an algorithm like:

  • For each boat size, keep a list of possible grid locations.
  • Pick a random location from this list.
  • Go through each list for each ship size, removing (or invalid) overlaps.


This way, you are less likely to get your own suicide trying to get a valid seat as your board gets more crowded.

+3


source


Perhaps add a check before putting the ship in the grid (before grid[x][y + j] = i + 1;

) to see if the space was already taken. If so, restart the placement. Perhaps adding a boolean like this isPlaced

and stick your code in a while loop like:



boolean isPlaced = false;

while (!isPlaced) {
  ...
  isPlaced = true;
}

      

0


source







All Articles