Title: Combine solver tools in the minesweeper game made with Python and tkinter
My two earlier posts described two minesweeper solving tools.
Those tools use two methods to solve the puzzle. The mnu_flag_required_mines method flags cells that must be mines. The mnu_expose_neighbors method exposes cells that cannot be mines. Both of the methods return True if they flag or expose any cells.
This new program uses the following code to alternate between those two tools as long as they make progress.
def mnu_1_plus_2(self):
'''Repeatedly flag cells that must be mines.'''
# Loop over the board looking for cells that require mines.
while self.mnu_expose_neighbors() or self.mnu_flag_required_mines():
pass
This while loop's condition calls both of the solver methods so the loop continues as long as either of them made any progress. The loop's body doesn't do anything (it just contains a pass statement); it's the calls in the condition test that perform the work as a side effect.
That's all there is to it.
Conclusion
Alternating these two tools is remarkably effective. The picture at the top of this post was the games state after using the program's "ten random moves" tool followed by the new alternating tool strategy. Of course, it doesn't always do that well by itself.
There are still a few other strategies that you need to finish the puzzle and I hope to implement them at some point, but for now I'm going to leave this program alone while I work on other things. Feel free to work on them if you like.
One thing that this example showed me is that the program often gets to a point where you cannot use logic to get any further. Sometimes you can use other strategies to get further, but sometimes you just need to guess. When you're solving the puzzle manually, it's harder to not notice that you died because of a guess. It's easy to assume that you made a mistake, but with this program it's pretty obvious when you get stuck.
Looking online, I see all sorts of claims that are just not true. One person played more than 1,500 games with around a 30% success rate. Another person claimed that you can always find a 99% of the time, which is definitely untrue. I'll get back to this eventually and see if I can get a better estimate.
Meanwhile, download the example to experiment with it and to see additional details.
|