我需要为我的项目制作一个简单(标准)的扫雷器。我想检查二维数组的随机(或特定)元素的周围元素。
假设我有一个这样的数组
boolean board[5][5]
我想检查周围的元素
board[0][0]
我写道:
public int numberChecker(int h, int w) {
int checker = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int he = h - 1 + i;
int wi = w - 1 + i; <---- this i should be j that was the issue
if (board.getHeight() > he && he >= 0
&& wi >= 0 && board.getWidth() > wi) {
if (boomChecker(he, wi)) {
if (w != (wi) && h != (he)) {
checker++;
}
}
}
}
}
return checker;
}
问题是我认为它只检查“\”方式(不知道如何表达)
前任:
(输出 # 表示未发现的数字表示周围的地雷)
揭开 4-2
######
######
0#####
#0####
##0### <-- unlocking this one
###0##
或者
揭开 0-0
0#####
#0####
##0###
###### <-- there is a mine at check spot
######
######
代码本身并不重要(我知道我解释自己有点复杂)
我需要的是一个工作环境检查循环(或任何东西)
提前致谢
问题解决了简单的愚蠢错误 int he = h - 1 + i; int wi = w - 1 + i; <--- 我应该是 j 谢谢