开发者

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

开发者 https://www.devze.com 2023-04-10 10:09 出处:网络
I am working on a Minesweeper which I have pretty much complete. The only thi开发者_如何学Pythonng missing is the detection of winning. What would be the best way to implement this? I know its going

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

The only thi开发者_如何学Pythonng 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!


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消