I have not been able to find any guide that fully explains this. They're all like "durr durr click this one cause its a mine" like how the fuck am I supposed to know?
strategy - How do I approach a "stuck" game of minesweeper? - Puzzling Stack Exchange
Minesweeper solving algorithm - Stack Overflow
What is the optimal starting strategy in Minesweeper? - Puzzling Stack Exchange
Aztecs Minigame, Minesweeper, and how to beat it.
I completed my first run through yesterday and after practicing on the mini game created by MooingCatFoE (found on YouTube FoE) I did very well. I had never played Minesweep before so this was a great help. I was able to gather enough rewards to open 3 expansions which really helped me complete the settlement quickly. The strategy explained helped me understand which tiles were likely to hide a reward. Bottom line - practice increases your odds of success.
More on reddit.comVideos
Generating the grid is simple. There are a couple simple algorithms that you need when executing the player's move, to determine which squares to open up and whether they have lost or won.
Generating the grid
The simplest algorithm is to place all of the mines randomly. (Make sure you don't overlap them!)
Problem: The player's first click might be a mine.
Improvement: Delay the generation of the grid until the user clicks on the first square, and don't put any mines in that square.
Problem: The player's first click might reveal a non-zero number, and they will be forced to click randomly until something opens up.
Improvement: Don't generate any mines in the (up to) eight squares around the first click, either.
Problem: The player might be forced to guess at some point, making this a sad excuse for a logic puzzle.
Improvement: Run the solver alongside the generator, making sure that the puzzle has a unique solution. This takes some cleverness, and isn't done in most variants.
Another, less common way to resolve ambiguities is to detect when the player knows they are choosing between equally likely possibilities and "collapse the waveform" into the position they decided on. I have never seen this in action, but it would be kind of fun.
Playing the game
Besides marking flags, the player can make two kinds of moves to attempt to uncover squares:
Single guess: The player clicks on a square with unknown state and no flag. Reveal the square, see if the player died, and put a number in it. If the square contains a 0, repeat this recursively for all the surrounding squares. This should be in a dedicated function, to separate it from the GUI's event handler, to make the recursion easy, and because it's reused in the multiguess.
Multiguess: The player clicks on a square that is uncovered and known to be safe. If the number of flags surrounding equals the number in this square, we open up the unflagged squares using the same procedure as above.
Winning the game
If the number of squares that are covered up is the same as the number of mines, then the player has won, even if they haven't placed a flag on every square.
When the player loses, it is customary to mark any incorrect guesses that they made, the remaining mines, and the mine that they stepped on.
As Henri mentioned, the correct way of solving minesweeper is with mathematics, specifically Linear Algebra Matrix mathematics for the deterministic part. I have a whole post here that:
- explains how that method works
- has working code that you can compile and run on any platform
- contains the code to make the game and a solver too
You can see that all here: https://massaioli.wordpress.com/2013/01/12/solving-minesweeper-with-matricies/
I would recommend reading through that and then having a good thought about it. The probabilistic part of Minsweeper can be solved using statistics too but I don't have a good plan for that yet. However, other people have looked into it too.
It is better to start in the corner in Minesweeper. The reason for this is simple: corners are the most likely place for logically unsolvable positions. The more corners you remove, the greater the chance you'll be able to solve the puzzle, regardless of mine density. Since your starting move can't be a mine, starting there reduces the chance of being stuck on a corner by 25%.
The reason corners are the most likely place for unsolvable positions is simple: you have very few clues there. Note that this will also tend to generate smaller starting cleared areas when you start in the corner, but this isn't generally a hindrance.
You are additionally not much more likely to have a faster game if you start in the corner. The reason for this is simple: until you run out of options, you're always executing further options. If a region would have been cleared by clicking in the middle of the puzzle, it will be cleared when you reach it normally. In this sense, you wouldn't actually be saving time by clearing the middle region, because you'll inevitably clear it anyway.
Surprisingly it is not best to play in the corner but near a corner. The corners do not give you enough information to proceed. Near the corner helps eliminate one possible 50% guess.
I have written a solver for MineSweeper and the odds of winning are posted below based on the size of the puzzle. The coordinates such as (x,y) assumes the upper left corner is (0,0) and the array references are puzzle[y][x]. I have run millions of samples and the results are much better than I originally posted.
| Size | Pos | Percent | Estimated deviation | new results |
|---|---|---|---|---|
| small | 2,2 | 96.955% | .04% | 97.18% |
| small | 2,1 | 96.919% | .04% | |
| small | 3,2 | 96.886% | .04% | |
| small | 3,3 | 96.671% | .04% | |
| medium | 3,2 | 87.683% | .08% | |
| medium | 2,2 | 87.618% | .08% | 89.20% |
| medium | 3,3 | 87.541% | .08% | |
| expert | 3,3 | 46.916% | .11% | 53.96% |
| expert | 3,2 | 46.833% | .11% |
I posted the source source code at https://github.com/EdLogg/MineSweeper.
I got my ideas and many suggestions from David Hill using Reddit name BinaryChop. Our conversation is at https://www.reddit.com/r/Minesweeper/comments/8b3b30/odds_of_winning_at_minesweeper/