简体   繁体   English

检查数组元素java的周围单元格

[英]checking surrounding cells of an array element java

I need to make a simple(standart) minesweeper for my project. 我需要为我的项目制作一个简单的(标准)扫雷器。 I want to check the surrounding elements of a random(or specific) element of a 2d array. 我想检查2d数组的random(或特定)元素的周围元素。

lets say I have an array like that 可以说我有一个像这样的数组

boolean board[5][5]

I want to check the surrounding elements of 我想检查一下周围的元素

board[0][0]

I wrote that: 我写道:

    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;
}

the problem is I think it only checks " \\ " way (do not know how to put it in words) 问题是我认为它只检查“ \\”方式(不知道如何用文字表达)

ex: 例如:

(output # means uncovered numbers mean surrounding mines) (输出#表示未发现数字表示周围的地雷)

uncover 4-2 发现4-2

######
######
0#####
#0####
##0###     <-- unlocking this one
###0##

or 要么

uncover 0-0 揭露0-0

0#####
#0####
##0###
######     <-- there is a mine at check spot
######
######

the code itself is not important (I know I made it a bit complicated to explain myself) 代码本身并不重要(我知道我自己解释起来有点复杂)

all I need is a working surrounding checking for loop (or anything) 我需要做的就是在工作中检查循环(或其他任何东西)

thanks in advance 提前致谢

Problem solved simple stupid mistake int he = h - 1 + i; 问题解决了简单的愚蠢错误int he = h-1 + i; int wi = w - 1 + i; int wi = w-1 + i; <--- i should be j thanks <---我应该成为j谢谢

I think your mistake is in this 2 lines: 我认为您的错误在于以下两行:

int he = h - 1 + i;
int wi = w - 1 + i;

you add to the height AND to the width the value of i. 您将i的值添加到高度和宽度上。 So you just check every of the 3 diagonal fields 3 times. 因此,您只需检查3个对角线字段中的每一个3次。 I think it should be 我认为应该

int he = h - 1 + i;
int wi = w - 1 + j;

so you really iterate through all 9 possible fields and not just through the diagonale. 因此您实际上遍历了所有9个可能的字段,而不仅仅是遍历对角线。

Hope that helps! 希望有帮助!

I'm going to write my answer as though answering a homework question 我要写我的答案,就像在回答作业问题一样

now what you really ought to do is, for each cell in your array, there are 8 other cells to check. 现在,您真正应该做的是,对于数组中的每个单元,还需要检查其他8个单元。 so something like: 所以像这样:

count += board[i-1][j-1] ? 1 : 0;
count += board[i-1][j] ? 1 : 0;
count += board[i-1][j+1] ? 1 : 0;
count += board[i][j-1] ? 1 : 0;
etc.

be sure to include some mechanism to prevent you from accessing elements outside the bounds of the array. 确保包含某种机制,以防止您访问数组范围之外的元素。

also, if you were wondering what the ? 另外,如果您想知道什么? and : mean, look up the Ternary Operator It's useful in all sorts of situations. and :表示查找三元运算符。它在各种情况下都很有用。

By the way, you might be better off explicitly checking each of the 8 possible surrounding cells. 顺便说一句,您最好显式地检查8个可能的周围单元格中的每一个。 It's entirely possible that the JVM might automatically unroll your loop, but I don't see why you wouldn't just check them explicitly. JVM完全有可能自动展开循环,但是我不明白为什么您不直接检查它们。 It will be faster, and easier to understand. 它将更快,更容易理解。

boolean upleft = board[row-1][column-1];
boolean up = board[row-1][column];
boolean upright = board[row-1][column+1];
boolean left = board[row][column-1];
boolean right = board[row][column+1];
boolean downleft = board[row+1][column-1];
boolean dow  = board[row+1][column];
boolean downright = board[row+1][column+1];

You'll of course have to do bounds checking if your on the edge of the board, but it makes more sense as to what is going on. 当然,您必须进行边界检查,以确保自己是否在棋盘边缘,但是对于正在发生的事情更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM