user541597 user541597 - 20 days ago 9
Java Question

How to detect a "win" in a Minesweeper game?

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!

Answer

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.

Comments