The two 1 represent a ship, the two 2 represent a ship and the two 3 represent another ship. the boats have 2 cells of size and must be placed using random and without touching.
How can I get the boats to place themselves in the matrix without touching each other?
public void crearTablero() {
int numero = 0;
System.out.println(" 0 1 2 3 4 5 ");
System.out.println(" ------------------");
for(int i = 0; i < tablero1.length; i++) {
System.out.print(numero + " |");
numero++;
for(int j = 0; j < tablero1[i].length; j++) {
System.out.print(" " + tablero1 [i][j] + " ");
}
System.out.print("|");
System.out.println(" ");
}
System.out.println(" ------------------");
}
public void colocarBarcos() {
int barcos = 1;
do {
int orientacion = azar(3,1);
if(orientacion == 1) {
vertical(barcos);
}else{
horizontal(barcos);
}
barcos++;
}while(barcos < 4);
}
public void horizontal(int barco) {
int x;
int y;
do {
x = azar(6, 0);
y = azar(4, 1);
}while((comprobar(x, y) == false) || (comprobar(x, y+1) == false) || (comprobar(x, y-1) == false));
tablero1[x][y] = barco;
if(azar(3,1) == 1) {
tablero1[x][y+1] = barco;
}else {
tablero1[x][y-1] = barco;
}
}
public void vertical(int barco) {
int y;
int x;
do {
y = azar(6, 0);
x = azar(4, 1);
}while((comprobar(x, y) == false) || (comprobar(x+1, y) == false) || (comprobar(x-1, y) == false));
tablero1[x][y] = barco;
if(azar(3,1) == 1) {
tablero1[x+1][y] = barco;
}else {
tablero1[x-1][y] = barco;
}
}
public boolean comprobar(int posicion1, int posicion2) {
boolean comprobar = false;
if(tablero1[posicion1][posicion2] == 0) {
comprobar = true;
}
return comprobar;
}
public int azar(int maximo, int minimo) {
int azar = (int) (Math.random()*(maximo-minimo)+minimo);
return azar;
}