Need help preventing that my ships will overlap with each other in a 2d array

Viewed 48

Im creating a battleship game for a school project and ive tried for very long to place my ships randomly and not overlap with each other. Ive tried various booleans and if statements but it doesnt seem to work.Its something wrong with the if statement I think and I need more code there.

My ships prints out ok with the size but sometimes they overlap.

Id appreciate if someone could help me out !:D

public class Game {

private int row;
private int col;

private Ship[][] board;
private ArrayList<Ship> ships;

private int shotsFired;
private int hitCount;
private int shipsSunk;
private int placedShips;
private int shipNumber;

public Game() {

    shotsFired = 0;
    hitCount = 0;
    shipsSunk = 0;
    placedShips = 0;
    shipNumber = 5;

    ships = new ArrayList<>();
    ships.add(new Battleship(5));
    ships.add(new Fighter(4));
    ships.add(new Destroyer(3));
    ships.add(new Cruiser(2));
    ships.add(new Submarine(1));
}

public void createBoard(Ship ship) {

    row = 10;
    col = row;

    board = new Ship[row][col];
    //creates the battle field, fills with empty ships
    for (int i = 0; i < board.length; i++) {
        Arrays.fill(board[i], null);
    }
    placeShips();
}

public void placeShips() {
    int occupied = 0;
    Random random = new Random();
    for (int i = 0; i < ships.size(); i++) {
        boolean direction = random.nextBoolean();
        if (direction) {
            int x = (int) (Math.random() * 10);
            int y = (int) (Math.random() * (10 - ships.get(i).getSize()));
            for (int j = 0; j < ships.get(i).getSize(); j++) {
                if (board[x][y + j] == null) {
                    board[x][y + j] = ships.get(i);
                }
            }
        }
        else {
            int x = (int) (Math.random() * (10 - ships.get(i).getSize()));
            int y = (int) (Math.random() * 10);
            for (int j = 0; j < ships.get(i).getSize(); j++) {
                if (board[x + j][y] == null) {
                    board[x + j][y] = ships.get(i);
                }
            }
        }
    }
}

enter image description here

1 Answers

You need to make a function that "reserves" the space for your next placement of a ship before actually putting it in.

So first, create a matrix of 2*2 for your Game class to store the starting and ending positions.

After that get an initial position and save it to the first array in the matrix, then probe the board for empty spaces in any of the four directions (as you are already doing in your code), and when you reach the final place save that position in your second array in the matrix. If you find an occupied space, just end the function and call it again until you get a result. Should look something like this:

...
position = null;
while (position == null) {
  position = getPosition(i); // Where i is the position you are using to take a ship from the list
}
...


public void getPosition(i) {
    int[][] positionArray = [];
    Random random = new Random();
    boolean direction = random.nextBoolean();
    if (direction) {
        int x = (int) (Math.random() * 10);
        int y = (int) (Math.random() * (10 - ships.get(i).getSize()));
        positionArray[0] = [x,y];
        for (int j = 0; j < ships.get(i).getSize(); j++) {
            if (board[x][y + j] != null) {
                return null;
            } else {
                y = y + 1;
            }
        }
        positionArray[1] = [x,y];
    }
    else {
        int x = (int) (Math.random() * (10 - ships.get(i).getSize()));
        int y = (int) (Math.random() * 10);
        positionArray[0] = [x,y];
        for (int j = 0; j < ships.get(i).getSize(); j++) {
            if (board[x + j][y] != null) {
                return null;
            } else {
                x = x + 1;
            }
        }
        positionArray[1] = [x,y];
    }
    return positionArray;
}

After that, you will need a bit of code to set the ship from the starting position to the final position but, as an ex-professor myself, I will let you solve that part. Hope that helped, have fun!

Note: I didn't test the code so I'm not 100% sure if the syntax is correct, so don't just copy and paste.

Related