Battleships 游戏的二维数组中的舰船重叠问题,Java
Problem with ship overlapping in 2D-arrays for Battleships game, Java
我正在 Java 中创建一个简单的基于控制台的 Battleships,对于不熟悉该游戏的人,它是在 2D 网格上玩的,您可以水平或垂直放下不同大小的船只。在我的示例中,它是使用 2D 字符数组创建的 10x10 网格。船只不允许彼此重叠,它们不能共享相同的 1x1 网格。
我已经设法修复它们不能相互重叠,但我遇到的问题是如果一艘船在起始位置共享相同的列或行(取决于船是垂直放置还是水平放置) ,我无法放下它。
请参阅下面的示例图片以更好地理解。
'0' is "empty" slots, 'S' is current placed ships(3-sized). I can place (in this example) 4-sized ships at blue markers, but I can't place them like the red markers
public void placeShip(ShipType shipType, int posX, int posY, int shipSize, Placement placement) {
boolean success = true;
char tempChar = 'x';
if(shipType == ShipType.BATTLESHIP)
tempChar = 'B';
else if(shipType == ShipType.CARRIER)
tempChar = 'C';
else if(shipType == ShipType.DESTROYER)
tempChar = 'D';
else if(shipType == ShipType.SUBMARINE)
tempChar = 'S';
if(placement == Placement.HORIZONTAL) {
for(int i = 0; i < posX+shipSize; i++) {
for(int j = 0; j < posX+shipSize; j++) {
if(board[i][posX-1] != '0' || board[posY-1][i] != '0') {
System.out.println("Can't place down the ship ");
success = false;
break;
}
}
}
if(success) {
System.out.println("Got space");
for(int i = 0; i < shipSize; i++) {
board[posY-1][posX-1+i] = tempChar;
success = false;
}
}
}
if(placement == Placement.VERTICAL) {
for(int i = 0; i < posY+shipSize; i++) {
for(int j = 0; j < posX+shipSize; j++) {
if(board[posY-1][i] != '0' || board[i][posX-1] != '0') {
System.out.println("Can't place down the ship ");
success = false;
break;
}
}
}
if(success) {
System.out.println("Got space");
for(int i = 0; i < shipSize; i++) {
board[posY-1+i][posX-1] = tempChar;
success = false;
}
}
}
}
以上是我用来放置船只的代码,我在其中发送位置 X 和 Y 以及船只的大小和船只的方向。
您需要重新考虑用于检查是否可以放下飞船的循环。让我们将其中一个分解成简单的英语:
if(placement == Placement.HORIZONTAL) {
for(int i = 0; i < posX+shipSize; i++) {
for(int j = 0; j < posX+shipSize; j++) {
if(board[i][posX-1] != '0' || board[posY-1][i] != '0') {
System.out.println("Can't place down the ship ");
success = false;
break;
}
}
}
第一期:
for(int i = 0; i < posX+shipSize; i++)
为什么我们要从 0 迭代到 posX
+shipSize
?我们只需要检查 shipSize
个空格。所以你应该从 posX
循环到 posX
+shipSize
,或者从 0 循环到 shipSize
,而不是组合。
第二期:
尽管您的位置是水平或垂直的,但您是在嵌套 for 循环。这意味着您无缘无故地循环 shipSize
次。你只需要做一次。
第三期:
if(board[i][posX-1] != '0' || board[posY-1][i] != '0')
位置board[i][posX-1]
在此上下文中没有意义,并且与您的船的位置无关,因为 你总是从 i = 0
开始。所以无论你想把你的船放在哪里,如果你有一艘在同一行或同一列,我们就有问题了。 这就是导致您的问题的原因。 相反,它应该类似于:if(board[posY-1][posX-1] != '0')
。
最后,为什么不直接设置posX
和posY
的位置呢?所以你不必减去1而迷惑自己。
最终,你的新块应该看起来像这样 (但我不能确定,因为你没有 post 最小可重现的例子):
if(placement == Placement.HORIZONTAL) {
for(int i = 0; i < shipSize; i++) {
if(board[posY-1][posX-1+i] != '0') {
System.out.println("Can't place down the ship ");
success = false;
break;
}
}
// your code
我正在 Java 中创建一个简单的基于控制台的 Battleships,对于不熟悉该游戏的人,它是在 2D 网格上玩的,您可以水平或垂直放下不同大小的船只。在我的示例中,它是使用 2D 字符数组创建的 10x10 网格。船只不允许彼此重叠,它们不能共享相同的 1x1 网格。
我已经设法修复它们不能相互重叠,但我遇到的问题是如果一艘船在起始位置共享相同的列或行(取决于船是垂直放置还是水平放置) ,我无法放下它。
请参阅下面的示例图片以更好地理解。
'0' is "empty" slots, 'S' is current placed ships(3-sized). I can place (in this example) 4-sized ships at blue markers, but I can't place them like the red markers
public void placeShip(ShipType shipType, int posX, int posY, int shipSize, Placement placement) {
boolean success = true;
char tempChar = 'x';
if(shipType == ShipType.BATTLESHIP)
tempChar = 'B';
else if(shipType == ShipType.CARRIER)
tempChar = 'C';
else if(shipType == ShipType.DESTROYER)
tempChar = 'D';
else if(shipType == ShipType.SUBMARINE)
tempChar = 'S';
if(placement == Placement.HORIZONTAL) {
for(int i = 0; i < posX+shipSize; i++) {
for(int j = 0; j < posX+shipSize; j++) {
if(board[i][posX-1] != '0' || board[posY-1][i] != '0') {
System.out.println("Can't place down the ship ");
success = false;
break;
}
}
}
if(success) {
System.out.println("Got space");
for(int i = 0; i < shipSize; i++) {
board[posY-1][posX-1+i] = tempChar;
success = false;
}
}
}
if(placement == Placement.VERTICAL) {
for(int i = 0; i < posY+shipSize; i++) {
for(int j = 0; j < posX+shipSize; j++) {
if(board[posY-1][i] != '0' || board[i][posX-1] != '0') {
System.out.println("Can't place down the ship ");
success = false;
break;
}
}
}
if(success) {
System.out.println("Got space");
for(int i = 0; i < shipSize; i++) {
board[posY-1+i][posX-1] = tempChar;
success = false;
}
}
}
}
以上是我用来放置船只的代码,我在其中发送位置 X 和 Y 以及船只的大小和船只的方向。
您需要重新考虑用于检查是否可以放下飞船的循环。让我们将其中一个分解成简单的英语:
if(placement == Placement.HORIZONTAL) {
for(int i = 0; i < posX+shipSize; i++) {
for(int j = 0; j < posX+shipSize; j++) {
if(board[i][posX-1] != '0' || board[posY-1][i] != '0') {
System.out.println("Can't place down the ship ");
success = false;
break;
}
}
}
第一期:
for(int i = 0; i < posX+shipSize; i++)
为什么我们要从 0 迭代到 posX
+shipSize
?我们只需要检查 shipSize
个空格。所以你应该从 posX
循环到 posX
+shipSize
,或者从 0 循环到 shipSize
,而不是组合。
第二期:
尽管您的位置是水平或垂直的,但您是在嵌套 for 循环。这意味着您无缘无故地循环 shipSize
次。你只需要做一次。
第三期:
if(board[i][posX-1] != '0' || board[posY-1][i] != '0')
位置board[i][posX-1]
在此上下文中没有意义,并且与您的船的位置无关,因为 你总是从 i = 0
开始。所以无论你想把你的船放在哪里,如果你有一艘在同一行或同一列,我们就有问题了。 这就是导致您的问题的原因。 相反,它应该类似于:if(board[posY-1][posX-1] != '0')
。
最后,为什么不直接设置posX
和posY
的位置呢?所以你不必减去1而迷惑自己。
最终,你的新块应该看起来像这样 (但我不能确定,因为你没有 post 最小可重现的例子):
if(placement == Placement.HORIZONTAL) {
for(int i = 0; i < shipSize; i++) {
if(board[posY-1][posX-1+i] != '0') {
System.out.println("Can't place down the ship ");
success = false;
break;
}
}
// your code