1

I am working on a Minesweeper which I have pretty much complete.

The only thing missing is the detection of winning. What would be the best way to implement this? I know its going to be part of the actionlistener that detects clicks, at some point the last click should detect a winner.

Could anyone give me some ideas thanks!

aioobe
  • 413,195
  • 112
  • 811
  • 826
user541597
  • 4,247
  • 11
  • 59
  • 87

2 Answers2

5

The player has won if

numUnopenedCells == numBombs

(where a cell is unopened if it is in its initial state, or flagged as a mine).

  • If numUnopenedCells > numBombs then the player has unopened cells which are not bombs (i.e. some work left to do)
  • If numUnopenedCells < numBombs then the player has necessarily "opened" a bomb cell and already lost.

I know its going to be part of the actionlistener that detects clicks, at some point the last click should detect a winner.

Yes, this snippet would be executed directly or indirectly by the action listener. I'd suggest you have a model of the game state, and in the openCell(int x, int y) method you check the above, and take the appropriate action.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • +1 -- I just deleted my answer -- I saw what you meant in the end though. – hvgotcodes Oct 04 '11 at 20:47
  • in response to your last comment, minesweeper sometimes forces the user to guess. In that 3 cell situation, there is no way to know which cell has the mine. Eventually you have to guess. – hvgotcodes Oct 04 '11 at 20:48
  • might need to do parseInt() as well to force the values to both be integers. – Breedly Oct 06 '14 at 22:52
  • I've been looking this for quite awhile regarding a technical mechanics of the game. So far I haven't found any except for the tutorials on how to play the game. – Neon Warge Jun 22 '15 at 09:10
  • It's quite simple really: 1) Place N random mines on the field, 2) When a user clicks a cell, if it's a bomb, it's game over, if it's not, then show the number of bombs in the neighboring cells. 3) Special case: If that number is 0, you typically display it as blank, and do a flood fill on the entire area of neighboring 0-cells. – aioobe Jun 22 '15 at 12:12
1

If the opened fields are #(all fields) - #(bomb fields).

javanna
  • 59,145
  • 14
  • 144
  • 125
Tobias
  • 9,170
  • 3
  • 24
  • 30