1

I am working on a minesweeper program in Java. I have my bombs distributed throughout the field, and I have my actionlisteners responding to clicks and mouselistener, responding to right clicks. I also have each square that is clicked check to see how many bombs are adjacent to it and print the number on the square just like in the game.

The only part I don't understand is how minesweeper opens up field when clicking a square whether it be a number or a blank square. Please help me understand how this works.

NullUserException
  • 83,810
  • 28
  • 209
  • 234
user541597
  • 4,247
  • 11
  • 59
  • 87

2 Answers2

4

The only part I don't understand is how minesweeper opens up field when clicking a square whether it be a number or a blank square.

If any of its neighboring squares has a mine, it will show a number with the number of mines around it.

It's blank if there are no mines around it (ie: it would show the number 0 if it had to). When it's blank it also recursively opens all its neighbors (eg: opens all neighbors and their neighbors if they are blank too, and so forth).

And if it's a mine you lose of course. An example:

X 2 . .
X 2 . .
2 2 1 .
1 X 1 .

(let X denote a mine).

If you open any of the squares with marked as . (blank), automatically expand all of them and the numbers next to them:

- 2 . .
- 2 . .
- - 1 .
- - 1 .

(let - denote a hidden square).

NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • @user541597: null user is right, that you want to recursively open up the surrounding cells if they are next to a blank cell (a cell with no mined neighbors), 1+ to him. For an example of this and model-view class structure, please have a look at my code here: [minesweeper example code](http://stackoverflow.com/questions/7006029/minesweeper-action-events/7016492#7016492) – Hovercraft Full Of Eels Sep 30 '11 at 16:29
  • so if i click a square it will open up all the squares around it that are a number or a blank square only if none of the adjacent squares are not a mine correct? And from there it performs recursions only on the blank boxes not the numbered boxes? – user541597 Oct 01 '11 at 23:08
2

If it's a bomb, you lose.

If it's a number then it just reveals that number.

If it's a null square, that is to say, one with no adjacent bombs, then it is a blank square and upon being revealed the game reveals all other squares in contact with it that are blank ( this process continues until all squares which are adjacent to the newly created null field are ones which are them selves adjacent to at least one bomb ( that is to say, have a number ))

zellio
  • 31,308
  • 1
  • 42
  • 61