Christopher Hodgson Gregory Tyler Loftis Minesweeper: an

advertisement
Christopher Hodgson
Gregory Tyler Loftis
Minesweeper: an Analysis of Strategic Algorithms
Minesweeper is a game of logic and probability that, despite its simplicity, has been
blowing up computers for decades. Digital versions have been published with major operating
systems since the 1980s, and it has been a staple program of the Windows operating system for
the past few decades of releases. The game is played on a two-dimensional grid of X rows by Y
columns. All cells in the grid are initialized to a hidden state, where their value is hidden from
the player. Clicking on a cell reveals its value. Cells can either be a numeric value from 0-8
denoting the number of mines in adjacent cells, or they can be a mine. If the player clicks on a
mined cell, the game is immediately lost and must be restarted. The player can mark a cell they
deduce or believe to be a mine, and the game is won when every non-mine cell is revealed.
For the purposes of exploring the various strategic algorithms, we made use of a program
from Northeastern University called Programmer’s Minesweeper. This program lists four
possible actions a strategy can take at any given point:
Probe: The strategy can reveal a square, losing if it probes a mine.
Look: The strategy can look at an unrevealed square and see if it is marked or unmarked.
Also returns the value of a given square.
Mark: The strategy can mark a square as a mine
Unmark: The strategy can unmark a previously marked square.
Given these actions and the definition of the minesweeper game, the problem can be
stated mathematically as follows:
A square S={s|s in ST) where ST = {0, 1, 2, 3, 4, 5, 6, 7, 8, mine}
ST = the possible states of a square.
A board B={S[x,y]| x in [0,...,X], y in [0,...,Y]}
Initially the states of the squares on the board are hidden and can be revealed one at a
time.
The goal is to find G where G = {∀g in B|g ≠ mine}. The revealed squares are g.
We must determine a P(S[x,y]) function that will generate the probability of S[x,y]
belonging to G, using the squares that are already known to be in G.
With this definition in mind, we set out to explore algorithmic approaches to winning
minesweeper. The goal is to find the most effective (as in win rate) strategy for solving an
arbitrary minesweeper grid.
The first strategy to examine is called the Single Point Strategy. The Single Point
Strategy is a naïve approach to minesweeper that nevertheless mimics the playing style of a
beginner. This algorithm follows three distinct rules:
1. If the number of mines adjacent to the square is equal to the value in the square, then all
adjacent squares can be probed safely.
2. If the number of unknown adjacent squares + the number of marked adjacent squares is
equal to the value in the square, then all unknown adjacent squares can be marked safely.
3. If the strategy cannot find either of these situations, it probes a random square.
By examining these rules, it is clear that a minesweeper strategy consists of two portions:
how to handle squares that can be deduced with 100% probability to be mined or not mined, and
how to proceed when a solved logical deduction is not available. The weakness of the Single
Point Strategy is the fact that it has no method for proceeding without a complete logical
deduction, and instead probes a random point. As expected, this is the cause of failure in every
case, and the win rate for this strategy sharply declines as boards become more difficult to solve.
On complexity, the Single Point Strategy is an O(n) algorithm. Running time increases
linearly as the size of the board increases. This is due to the fact that when examining a single
point, the algorithm at most will examine 8 adjacent squares, making the examining step a
constant time step. Probing a random square is also constant time, meaning the only scaling
value is the number of squares to be examined, which increases with board size.
The second strategy we examine is called the Equation Strategy, which has similarities to
the Single Point Strategy; however it handles lack of information more elegantly. The Equation
Strategy described in pseudo-code:
Choose a starting point to add to probe set
while game not finished
if probe set is empty
choose an unprobed point to add probe set
for all points in probe set
apply single equation rule to point
apply equation difference rule to point
remove point from probe set
if mines remaining < some number
start comparing to global equation
The strategy chooses a set of unprobed points to examine, and for each unprobed point,
applies equations using information from surrounding cells to solve for the point, if possible.
Before understanding the rules used in the strategy, it is important to know what the equation
being used is. For the equations used the squares are given values. If the square would be out of
the bounds of the board, it is given the value 0. If a square has been probed and isn’t a mine, then
the value its value is 0. If a square has been marked to be a mine, then its value is 1. All
unprobed and unmarked squares are treated a variables.
The equation is as follows:
8
𝑐 = ∑ 𝑝𝑖
𝑖=1
c = the number of mines around the square being examined
p = the values of the adjacent points.
The summation of p values will always equal c.
The Single Equation Rule states that if c for and equation after isolating the variables to
one side of the equation, then the squares these variables represent are not mines and can be
probed. If c is equal to the number of variables after isolating the variables to one side of the
equation, then the squares these variables represent must be mines. Below is an example.
Single Equation Rule
Examine the Yellow Square





The square’s value is 2. We set the
solution of the equation equal to the
value in this square.
We then create an equation using the
adjacent square values, substituting
variables for unknown squares.
For our square, the equation becomes
2=x+1+0+0+0+0+0+0
After isolating the variables, the
equation becomes: 1 = x
Since the number of variables is equal to
the new c value, the square x represents
must be a mine
The Equation Difference Rule states that if you take the difference of the
equations of 2 adjacent squares and if the difference of the c’s are greater than 0, then apply the
Single Equation Rule the equation.
8
8
(𝑐1 − 𝑐2 ) = ∑ 𝑝1𝑖 − ∑ 𝑝2𝑖
𝑖=1
𝑖=1
On complexity, the Equation Strategy has a worst case running time of O(n2), and an
average case of O(n), where n is the number of spaces on the board that are not mines. The
reason why it has a worst case of n2 is due to the fact that depending on mine placement; it’s
possible that the strategy will constantly examine each square repeatedly without being able to
successfully apply equation rules. This leads to repeated examination of already examined
squares with little progress being made. At most the strategy will examine all n squares n times
as it tries to solve various configurations.
The third strategy is known as the Constraint Satisfaction Problem Strategy, or
CSPStrategy, put forth by Chris Studholme. In Studholme’s paper Minesweeper as a Constraint
Satisfaction Problem, he developed the CSPStrategy as a series of seven steps. Again, the
algorithm can be seen in two parts: how to proceed when a logical deduction can be made, and
how to proceed when not enough information for a deduction is available.
Upon examination of Studholme’s CSPStrategy code, we found that the implementation
was extremely similar to the Equation Strategy. However, CSPStrategy introduces many
unneeded variables and operates by building theoretical constraint boards. While CSPStrategy is
more effective than Single Point Strategy, our tests indicated that CSPStrategy has a lower
average winrate than the Equation Strategy while introducing more overhead. While it is
interesting to see a minesweeper game couched as a constraint satisfaction problem, we elected
not to examine CSPStrategy in extreme detail due to its inferiority to the Equation Strategy.
CSPStrategy’s running time is at worst O(n2) and on average O(n), the same as Equation
Strategy, and for similar reasons. CSPStrategy can become stuck in a long loop of attempting to
solve points unsuccessfully, again meaning it will examine all n points n times.
Our focus was to improve win rates for the strategies by improving methods of picking
points when not enough information is known for a deduction. There are three methods of
picking a square that we incorporated into the Equation Strategy.
The first method, Random Pick, is the one employed by Single Point Strategy. It has a
constant running time; however it predictably has the worst results. There exist very few
minesweeper configurations where a random guess is as good as examining the board structure
and deciding on probable mine spots.
The second method we have dubbed Probabilistic Pick yields more successful results.
It’s an O(n) picking method that uses three series of equations for different situations.
I. If no probed squares are nearby:
a. P(m) = unprobledSquares / unmarkedMines (the probability that a square has a
mine)
b. P(c) = unprobedCornerSquares/unprobedSquares (The probability that a square is
a corner)
c. P(e) = unprobedEdgeSquares/unprobedSquares (The probability that a square is an
edge)
d. P(cM) = P(c) * P(m) ( probability that a corner square is a mine)
e. P(eM) = P(e) * P(m) (the probability that a edge square is a mine)
II. If there is a nearby probed square
f.
P(s) = (c - nearbyMines)/nearbyUnprobedSquares (The probability of an adjacent
square contains a mine)
g. P(m) = Max(P(s1),...,P(sn)) (where s is a set of adjacent probed squares)
III. After calculating probabilities, probe the square with the least probability of being a
mine.
The third method would be an implementation of a Pattern Matching algorithm. We did
not attempt to implement this; however we discussed the idea that with enough training patterns,
the algorithm could solve a given configuration based on previous data from similar patterns.
The gate for this method would be the large number of patterns that would need to be generated
to train the program, and the fact that not every pattern has one single solution. We did
implement probabilistic choosing, and it improved the winning rate of the Equation Strategy by
an appreciable margin.
To gather data on a strategy’s effectiveness, we ran a thousand games with each strategy
on a 30x30 board, increasing the mine density with each game. The results, as mentioned before,
showed that the Equation Strategy has the highest win rate, followed by the CSPStrategy.
win rate 30x30 board
1.2
1
0.8
Equation Strategy
0.6
Single Point Strategy
CSP Strategy
0.4
0.2
0
0
100
200
300
400
500
It is of interest to note that the game defines three difficulty levels: Beginner, an 8x8 grid
with 10 mines; Intermediate, a 13x15 grid with 40 mines; and Expert, a 16x30 grid with 99
mines. Note that the mine density in beginner is 0.156, and the mine density in intermediate
is .205. The density only goes up to .206 on expert, the added difficulty coming instead from the
large board size. Win rates for all three algorithms started dropping dramatically at a mine
density of .222, which is evidence that even a human player would find such a board extremely
difficult to solve.
We also examined the win rates of implementing a probabilistic picking method for the
Equation Strategy, which improved the rate significantly.
Win Rate on 30x30 Board using Equation
Strategy
1.1
1
0.9
0.8
0.7
0.6
Probabilistic Pick
0.5
Random Pick
0.4
0.3
0.2
0.1
0
0
50
100
150
200
250
It is clear that the naïve approach of only looking at one square at a time is ineffective at
solving a minesweeper board. Many improvements can be made if an algorithm takes into
account probabilities given all available data, and perhaps even more improvements could be
made if single-solution patterns are used to train a strategy.
To conclude, we felt that the exploration of Minesweeper solving algorithms was
definitely material worthy of an algorithm design class. Since Minesweeper is an NP-hard
problem, there is a degree of elegance required when coming up with algorithms to come to a
solution. We examined three strategies, two in great detail, to decide their relative merits and
analyze implementation details. In addition to examining the strategies of others, we expanded
on a strategy by implementing our own improvement: namely, probabilistic picking for the
Equation Strategy. Our examination of Minesweeper led us to further understand what
constitutes a Constraint Satisfaction Problem and the approaches one takes to satisfying said
constraints. Perhaps with enough time, an algorithm will be put forth that wins nearly 100% of
games – there is no proof that this is impossible.
References
Fowler, Andrew and Young, Andrew. Minesweeper: A Statistical and Computational Analysis.
Published 5/1/2004.
http://www.minesweeper.info/articles/MinesweeperStatisticalComputationalAnalysis.pdf
Studholme, Chris. Minesweeper as a Constraint Satisfaction Problem. Published 4/16/2001.
http://www.minesweeper.info/articles/MinesweeperStatisticalComputationalAnalysis.pdf
Ramsdell, John D. Programmer’s Minesweeper (Java program).
http://www.ccs.neu.edu/home/ramsdell/pgms/
Download