简体   繁体   English

FloodFill-扫雷车,需要解释

[英]FloodFill - Minesweeper, Explanation needed

I'm trying to make a Minesweeper-like game in Java and I've got most of it to work but something I need help with is FloodFill - http://en.wikipedia.org/wiki/Flood_fill . 我正在尝试用Java制作类似于《扫雷》的游戏,并且大部分游戏都可以使用,但我需要帮助的是FloodFill- http://en.wikipedia.org/wiki/Flood_fill Not really help ... but can someone explain how it works? 并没有真正的帮助...但是有人可以解释它的工作原理吗? I've looked online but i don't really understand the explanation so i though it would be easier to ask here. 我在网上看过,但我不太理解其中的解释,因此尽管在这里问起来会更容易。

In my Minesweeper I have: 在扫雷艇中,我有:

JButton[] btn = new JButton[100]//buttons being clicked and displaying the values/bombs
int[] mines = new int[100];//int array holding the values for each button.

The grid is a 10x10 grid so say the button you clicked was btn[14], 网格是10x10网格,因此假设您单击的按钮是btn [14],

btn[4]  // north of btn[14](14-10)
btn[24] // south of btn[14](14+10)
btn[13] //  west of btn[14](14-1)
btn[15] //  east of btn[14](14+1)

So back to the question, could someone explain it to me? 回到问题,有人可以向我解释吗?

EDIT: I changed my code to be 2D so instead of the above one it is now 编辑:我将代码更改为2D,所以现在不是上面的代码了

btn[1][4]//row one, column 4

When the button is clicked, i want it to check a variable called mines[][] which has the values and if the value is equal to 0(around the initial clicked) it changes the BG 当单击按钮时,我希望它检查一个名为mines [] []的变量,该变量具有值,并且如果该值等于0(在最初单击的位置附近),它将更改BG

btn[x][y].setBackground(Color.GRAY);

Its a recursive algorithm. 它是一种递归算法。 You start at some start position in a 2D Grid [x,y], then look in all directions and fill them if you can. 您可以从2D网格[x,y]的某个起始位置开始,然后朝各个方向查看,并尽可能填充它们。 If (x,y) can't be filled, return. 如果(x,y)无法填充,请返回。

void floodFill( int x, int y ) {
   if ( btn( x, y ) isFillable ) {
       fillBtn(x,y);
       floodFill( x+1, y );
       floodFill( x-1, y );
       floodFill( x, y-1 );
       floodFill( x, y+1 );
   } else {
       return;
   }
}

(ommited check for boundaries of grid) (省略了对网格边界的检查)

I guess ur mainly asking about floodfill. 我想您主要是在询问洪水。 Actually it is a simple and common recursive algorithm. 实际上,这是一种简单且通用的递归算法。 It can solve whatever your data structure is 1D or 2D. 它可以解决您的数据结构是1D还是2D的问题。 For 2D version, @RMoeller has given the answer. 对于2D版本,@ RMoeller给出了答案。 For your previous 1D declaration, it is also similar like this: 对于您以前的1D声明,它也类似如下:

void floodFill( int pos ) {
   if ( btn( pos ) isFillable ) {
       fillBtn(pos);
       floodFill( pos+1 );
       floodFill( pos-1 );
       floodFill( pos+10 );
       floodFill( pos-10 );
   } else {
       return;
   }
}

One thing u should remember is, floodfill, and almost all recursive algorithm, needs to check the boundary. 您应该记住的一件事是,泛洪和几乎所有递归算法都需要检查边界。 Otherwise u may get into an infinite loop or get a wrong result. 否则,您可能会陷入无限循环或得到错误的结果。 In above example(1D version), u should check whether: pos >= 1 && pos <= 100 Similar to 2D version which is to check: x >= 1 && x <= 10 && y>=1 && y <=10 在上面的示例(1D版本)中,u应该检查:pos> = 1 && pos <= 100类似于要检查的2D版本:x> = 1 && x <= 10 && y> = 1 && y <= 10

Hope this helps. 希望这可以帮助。

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

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