我需要爲我的項目製作一個簡單的(標準)掃雷。 我想檢查一個2d數組的隨機(或特定)元素的周圍元素。檢查數組元素周圍的單元格java
可以說我有這樣的
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; < ---我應該Ĵ 感謝
項目?這是'家庭作業'的代碼?如果是這樣,請添加一個作業標籤。哦,和「\」的方式被稱爲「對角線」 – TheZ 2012-07-10 21:52:12
但只有\方式對角線 /這也是一個對角線,這就是爲什麼我沒有說對角線:) – 2012-07-10 21:54:25
我會用東南(「\」)和南 - 西方(「/」)方向術語。 – user845279 2012-07-10 21:57:19