Hi everyone!
So, I hereby hope to show that I've made progress in coding java (largely due to the help of this great community). I want to thank everyone for dealing with me (especially James). :)
The goal:
The code below is supposed to seed a layout of boats into a 2D array. This is part 1 of an excersize I've given myself - create code to seed boats horrizontally and vertically, boats must not collide, boats must not be out of bounds.
The issue:
Usually the layout comes out just fine. But sometimes, some boats are missing in length (the length 4 boat becomes a length 3 boat). This doesn't happen only on borders, but in the middle too.
I've tried seeding with a single length 5 boat, and it still happens.
The code:
package potapanjebrodova;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class PotapanjeBrodova {
int[] ships = {4, 3, 3, 2, 2, 2};
char[][] table = new char[11][11];
Random R = new Random();
public static void main(String[] args) {
PotapanjeBrodova PB = new PotapanjeBrodova();
PB.makeEmpty();
PB.seeding();
PB.printTable();
}
public void seeding() {
List<Integer> locationI = new ArrayList<>();
List<Integer> locationJ = new ArrayList<>();
boolean reappearingI = false;
boolean ponavljanjeJ = false;
int positionI;
int positionJ;
int direction;
int shipLength;
for (int i = 0; i < this.ships.length; i++) {
direction = this.R.nextInt(2);
shipLength = this.ships[i];
// this DO WHILE will generate unique seeding coords for each ship
do {
positionI = this.R.nextInt(10) + 1;
positionJ = this.R.nextInt(10) + 1;
for (int j = 0; j < locationI.toArray().length; j++) {
if (locationI.toArray()[j] == positionI) {
reappearingI = true;
} else {
reappearingI = false;
}
}
for (int j = 0; j < locationJ.toArray().length; j++) {
if (locationJ.toArray()[j] == positionI) {
ponavljanjeJ = true;
} else {
ponavljanjeJ = false;
}
}
} while (this.table[positionI][positionJ] != '_' && (reappearingI == false || ponavljanjeJ == false));
do {
switch (direction) {
case 0:
this.table[positionI][positionJ] = 'O';
locationI.add(positionI); // to prevent seeding in the same spot
if (positionI + this.ships[i] >= this.table.length) {
positionI--; // if end of table, change direction
} else {
positionI++;
}
break;
case 1:
this.table[positionI][positionJ] = 'O';
locationJ.add(positionJ); // to prevent seeding in the same spot
if (positionJ + this.ships[i] >= this.table.length) {
positionJ--; // if end of table, change direction
} else {
positionJ++;
}
break;
}
shipLength--;
} while (shipLength != 0);
}
}
public void makeEmpty() {
for (int i = 0; i < this.table.length; i++) {
for (int j = 0; j < this.table.length; j++) {
this.table[i][j] = '_';
}
}
}
public void printTable() {
for (int i = 0; i < this.table.length; i++) {
for (int j = 0; j < this.table.length; j++) {
System.out.print(" " + table[i][j]);
if (j == this.table.length - 1) {
System.out.println();
}
}
}
System.out.println("X-X-X-X-X-X-X-X-X-X-X-X");
}
}
PS
I'd like to hear comments on the way I code, what are my strong points, what should I improve, am I using bad logic?
Thanks,
Pob