我正在建造一艘基本的战舰,而且当我们的船只被放置在网格上时,我很难避免重叠。我知道如何检测船只是否重叠,但如果它超过圈数,我很难在船上重置船只。
public static void ship4(String[][] board) {
boolean over = true;
int x = getRandomNumberInRange(0, 16);
int y = getRandomNumberInRange(0, 1);
int g = getRandomNumberInRange(0, 16);
while (over == false) {
for (int i = 0; i < 4; i++) {
if (y < 1) { // if y coordinate is less than 1 the ship is placed horiziontally//
board[x][g + i] = "*";
over = true;
} else {
board[x + i][g] = "*"; //makes ships vertically//
over = true;
}
//checks to see if ship 4 contains the same strings as the other ships//
if (board[x][g] == "g" || board[x][g] == "t," || board[x][g] == "s") {
over = false;
}
}
}
}