I am designing a Minesweeper-like game (with modified rules), and I want to prevent player from guessing. My goal is: The generated board is with few revealed squares, and p
Disclaimer: This may or may not be entirely correlated, but it's something neat I noticed, and might be useful to others trying to find the answer.
In minesweeper, there's a neat little thing I found when looking at different minesweeper boards: In the game, when generating a board, all non-mine squares have their value set to the number of neighboring mines. However, you can apply the same thing, but in reverse: All mines add 1 to the value of each neighboring space. This makes it possible to think of mines less like mines, and more like layered 3x3 squares. To demonstrate, here's a board, stripped of any neighboring mine counts:
....x
.x...
x..x.
..x..
x...x
If we use the normal generation method, setting the value of each non-mine tile to the number of neighboring mine tiles, we get:
1111x
2x222
x33x1
23x32
x212x
So what's the issue with using this method? Well, it has to do with just how many times we're checking for a mine. Let's see:
1111x - took 23 checks
2x222 - took 31 checks
x33x1 - took 26 checks
23x32 - took 31 checks
x212x - took 20 checks
Ouch. That comes in at a total of 131 checks. Now, let's try the square method:
1111x - took 8 checks
2x222 - took 13 checks
x33x1 - took 18 checks
23x32 - took 13 checks
x212x - took 11 checks
This method is much faster, with a total of only 63 checks, less than half of the naïve method.
These "squares" can also be utilized when solving boards. For example:
0000
?110
??10
???0
In this example, we can clearly see a corner, and therefore a square, with a mine in the center (+ and - are queued opens and flags):
0000
?110
?-10
???0
We can also expand the corner, since there isn't another square on top of it:
0000
+110
+-10
?++0
However, we cannot expand the last ? in the same way until we uncover all the queued tiles. Just as an example, I'm going to use some twos:
0000
1110
2x10
?210
Now we can see another corner, and it turns out that it intersects a mine. This can be hard to spot, but this is a corner.
One thing to watch out for, though, is something like this:
00000
01110
12x10
x?210
2x100
In this scenario, there are 3 squares. The 4x4 region in the top right matches the previous scenario, but don't get fooled: the twos from earlier are part of two separate squares in this one, and the unknown tile happens to be a three. If we had placed a mine there, the twos would be complete, and we would have lost to misjudgment, trying to uncover a tile that seems safe.
TL;DR: Mines can be thought of as layered 3x3 squares, which can save time when generating and/or solving.
It's well-known solving minesweeper is NP-complete.
This is true but perhaps not as relevant as you think. The proposed algorithm is something like "repeatedly generate random boards until the computer can solve one". NP-hardness is a property of the worst case, but here we're really interested in the average-case hardness. If an unusually hard board is generated, we can time out the solver and restart with a new board.
Also, even if there were an oracle to distinguish good boards from bad, would you really want the user to have to solve a hard problem in order to avoid guessing? A less talented computer solver might bias the choice toward fairer boards.
The implementation of Minesweeper in Simon Tatham's Portable Puzzle Collection is guessing-free. (It's also MIT licensed, so you're free to copy his implementation if you so desire.)