又是我。我正在尝试制作一个非常简单的战舰游戏。现在我想用至少一个空闲单元分隔船只。看图片
正如你所看到的,边缘完全没有运输,这使得可用的放置数量要小得多(或者更少?对不起我的英语不好)。
“字段”只是一个 int[10][10] 数组。
我有一个非常粗鲁的方法来检查是否可以放置船:(IS_WATER const = 0)
private static boolean checkIfShipAvailable(int x, int y, int dir, int length) {
int counter = 0;
switch(dir) {
case DIRECTION_RIGHT:
try {
if(field[x-1][y] == IS_WATER) counter++;
if(field[x-1][y-1] == IS_WATER) counter++;
if(field[x-1][y+1] == IS_WATER) counter++;
if(field[x][y-1] == IS_WATER) counter++;
if(field[x][y+1] == IS_WATER) counter++;
if(field[x+1][y-1] == IS_WATER) counter++;
if(field[x+1][y+1] == IS_WATER) counter++;
if(field[x+length-1][y-1] == IS_WATER) counter++;
if(field[x+length-1][y+1] == IS_WATER) counter++;
if(field[x+length][y] == IS_WATER) counter++;
if(field[x+length][y-1] == IS_WATER) counter++;
if(field[x+length][y+1] == IS_WATER) counter++;
} catch (IndexOutOfBoundsException e) {
counter++;
}
Log.d(TAG, "Direction: Right. Counter = " + counter);
if (counter == 12)
return true;
break;
case DIRECTION_DOWN:
try {
if(field[x-1][y-1] == IS_WATER) counter++ ;
if(field[x][y-1] == IS_WATER) counter++ ;
if(field[x+1][y-1] == IS_WATER) counter++ ;
if(field[x-1][y] == IS_WATER) counter++ ;
if(field[x+1][y] == IS_WATER) counter++ ;
if(field[x-1][y+1] == IS_WATER) counter++ ;
if(field[x+1][y+1] == IS_WATER) counter++ ;
if(field[x-1][y+length-1] == IS_WATER) counter++ ;
if(field[x+1][y+length-1] == IS_WATER) counter++ ;
if(field[x-1][y+length] == IS_WATER) counter++ ;
if(field[x][y+length] == IS_WATER) counter++ ;
if(field[x+1][y+length] == IS_WATER) counter++ ;
} catch (IndexOutOfBoundsException e) {
counter++;
}
Log.d(TAG, "Direction: Down. Counter = " + counter);
if (counter == 12)
return true;
break;
}
return false;
}
那是2-4个细胞船。对于 1 单元船:
private static boolean checkIfOneAvailable(int x, int y) {
int counter = 0;
try {
if(field[x-1][y-1] == IS_WATER) counter++ ;
if(field[x][y-1] == IS_WATER)counter++ ;
if(field[x+1][y-1] == IS_WATER) counter++ ;
if(field[x-1][y] == IS_WATER) counter++ ;
if(field[x+1][y] == IS_WATER) counter++ ;
if(field[x-1][y+1] == IS_WATER) counter++ ;
if(field[x+1][y+1] == IS_WATER) counter++ ;
if(field[x][y+1] == IS_WATER) counter++ ;
} catch (IndexOutOfBoundsException e) {
counter++;
}
if (counter == 8)
return true;
return false;
}
你能告诉我,错误在哪里,我怎样才能让船甚至放在边缘?