# A* applied to minesweeper game

This topic is 1715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to make the minesweeper solver. As you know there are 2 ways to determine which fields in minefield are safe to open, or to determine which fields are mined and you need to flag it. First way to determine is trivial and we have something like this:

if (number of mines around X – current number of discovered mines around X) = number of unopened fields around X

then

All unopened fields around X are mined

if (number of mines around X == current number of discovered mines around X)

then

All unopened fields around X are NOT mined

But my question is: What about situation when we can't find any mined or safe field and we need to look at more than 1 field?

For example this situation. We can't determine anything using previous method. So i need a help with algorithm for these cases.

I have to use A* algorithm to make this. That is why i need all possible safe states for next step in algorithm. When i find all possible safe states i will add them to the end of current shortest path and depending on heuristic function algorithm will choose next field that needs to be opened.

##### Share on other sites

I don't see why you need A* for this... is it really a graph search?

And click the square above the 2 next to the 2 flags... it's safe ;) EDIT: As are all the uncovered squares next to the 1s

EDIT2: I'm guessing you need to know HOW to identify those spaces as flagged... I still don't see how it involves A* though... you can probably just brute-force it, or encode in some simple rules for cases like that... The problem arises when you have to guess - you need to assign probabilities for each selection and pick the most safe option.

##### Share on other sites

What now? I am using A* because i have to find the shortest path to find all mined fields. When i find all mined fields algorithm is finished. Shortest path in this case means to find all mined fields but to open minimal number of fields.

##### Share on other sites

I'm not sure if you can use A* since there is hidden information. What is the heuristic?

##### Share on other sites

I still didn't think about heuristic. For now i want to complete this part about finding all possible safe fields.

##### Share on other sites

Well, I'd suggest looking at the places with only 1 safe spot first (i.e. N-1 mines for N unknown spaces), starting with the smallest N (so the 2 in the picture next to 1s would fit the bill). Then you can examine all the combinations (there are N of them) looking for a contradiction.

After that it sounds like it is going to be a recursive problem... have you done any googling?

##### Share on other sites

Don't assume A* is your answer. A* is essentially Dijkstra's algorithm with a heuristic applied, and if you don't have a good heuristic to use, then skip it and just use Dijkstra's. But even here, Dijkstra's isn't really what you want, because you don't really want a graph search for an optimal path.

Minesweeper is NP-Complete, so you're not going to solve it with any polynomial time algorithm (assuming P != NP) (both A* and Dijkstra's are polynomial time algorithms, and as such are not powerful enough to solve this problem).

Edited by Cornstalks

##### Share on other sites

Presumably the OP wants to find a way to solve minesweeper using the least number of clicks. I think that is going to be a hard problem, unless you already know the locations of all the mines, then you could graph search it, but that isn't how the game is played... (EDIT: The game also cheats for you on the first move, it is never a mine... presumably it moves the mine elsewhere in that case - on Windows anyway).

You can get a situation where you have to make a guess, in that case you need to work out the guess least likely to reveal a mine but that is a probability question rather than a graph search one. Sometimes you end up with a 50-50 win/lose choice though... so it can be just a coin flip.

##### Share on other sites

There are some rules for that what you said. I have to make this for my homework but there are 2 rules:

1) I will get already opened minefield at start

2) Those test cases will be made without a need to guess anything. So i am sure there won't be 50/50 cases. I will always be able to find safe fields.

##### Share on other sites

There's a big difference between "opened minefield" and "locations of all mines known at the start"... if you don't know the locations of ALL the mines (i.e. you have missing information) I can't see a way to use a graph search...

Every click removes exactly 1 blank space unless there are no adjacent mines (when it does a flood fill of all 0 neighbouring mine squares)... but there is no way to know which squares will be 0s in advance...

##### Share on other sites

Of course i won't have access to position of all mines. My solver will act like a human. At start i will have opened fields. Then  i will use some logic to determine all possible safe fields and depending on some heuristic algorithm will choose what path it should follow and when algorithm choose what is next click new minefield state will be made. Of course new minefield state is generated but that function will make a teacher. Obviously, heuristic will be more important, although i have no idea what should i use for heuristic function. Also i HAVE to use A*. It is not my choice.

Edited by Backward

##### Share on other sites

I'm saying it doesn't matter which order you remove safe squares, since you always remove just 1 each time unless you get lucky and remove one with no neighbouring mines... unless you look for squares to remove which may have no neighbouring mines first.

What you do want to do is look at the cases with least number of safe or unsafe squares first though (so all 2's with 3 unopened neighbours before 2's with 4 unopened neighbours), you need a method to flag a set of squares as having a mine and then evaluate the board to see if it causes a contradiction. You need to try all combinations of flagged squares and if you don't find a contradiction look somewhere else.

To generate the set of possible mined squares you need to generate combinations (e.g. I have 3 squares to check and 2 mines, possibilities are {1, 2}, {1, 3}, {2, 3}), you need to know about combinations and permutations to do that...

##### Share on other sites

Also i HAVE to use A*. It is not my choice.

A* is an algorithm to find a path in a graph. Your problem is not that of finding a path in a graph. So unless you can explain how to convert this problem into finding a path in some graph, I don't think you'll succeed.

##### Share on other sites

I solved first part. Can you give me any idea about heuristic? For example i found 10 safe fields, and now what rule should i use to choose best possible field to open? I was thinking to try something like this. For every opened field with mines around it (fields 1 - 8) i will calculate for every unopened field around it possibility to be mined. For example if i have field with number 2, i will see how many unopened fields i have around field with number 2, and possibility for every unopened field will be 2/number of unopened fields. If there is already another possibility calculated for some field, i will choose bigger possibility. And i will repeat this algorithm until there is no change. What do you think about this?

##### Share on other sites
A simple situation in which your proposal doesn't work very well is one in which all the bombs are accounted for as neighbors of numbers shown. In that case, you are better off opening a field that is not a neighbor of a number shown, which your algorithm would never do.

I can think of an interesting algorithm to pick the next field to open. Generate random distributions of the bombs whose location you don't know and check what square has a bomb in it in the smallest fraction of those distributions. Generating the random distributions without bias can be tricky. The naive algorithm that is guaranteed to be unbiased is to generate completely random distributions and then checking if they are compatible with the known numbers, discarding distributions that don't match everything. Since this will probably result in a tiny fraction of distributions being accepted, you may want to do something like accepting a distribution "locally" if it matches all the numbers within some distance to a particular field.

##### Share on other sites

A simple situation in which your proposal doesn't work very well is one in which all the bombs are accounted for as neighbors of numbers shown. In that case, you are better off opening a field that is not a neighbor of a number shown, which your algorithm would never do.

I can think of an interesting algorithm to pick the next field to open. Generate random distributions of the bombs whose location you don't know and check what square has a bomb in it in the smallest fraction of those distributions. Generating the random distributions without bias can be tricky. The naive algorithm that is guaranteed to be unbiased is to generate completely random distributions and then checking if they are compatible with the known numbers, discarding distributions that don't match everything. Since this will probably result in a tiny fraction of distributions being accepted, you may want to do something like accepting a distribution "locally" if it matches all the numbers within some distance to a particular field.

If you have a time, please try to make graphics explanation of your algorithm.

##### Share on other sites

If you have a time, please try to make graphics explanation of your algorithm.

I am not going to post graphics, but I can show you some crude code:
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
/*
Convention:
-3 : outside
-2 : flag
-1 : unknown
>=0 : That many neighbors are bombs
*/

char knowns[12][12] = {
{-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3},
{-3,  0,  1, -1, -1, -1, -1, -1, -1, -1, -1, -3},
{-3,  0,  2, -1, -1, -1, -1, -1, -1, -1, -1, -3},
{-3,  1,  3, -2, -1,  4, -1, -1, -1, -1, -1, -3},
{-3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -3},
{-3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -3},
{-3, -1, -1,  1, -1, -1, -1, -1, -1, -1, -1, -3},
{-3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -3},
{-3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -3},
{-3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -3},
{-3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -3},
{-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3}
};

int bombs_unaccounted_for = 19;

int main() {
bool bomb_locations[12][12]={{0}};
int danger[12][12]={{0}};

std::vector<int> unknowns;
for (int i=0; i<12; ++i) {
for (int j=0; j<12; ++j) {
if (knowns[j] == -1)
unknowns.push_back(i*12+j);
bomb_locations[j] = (knowns[j] == -2);
}
}
for (int count=0; count < 10000; ) {
// Generate bomb distribution
bool bl_copy[12][12];
std::memcpy(bl_copy, bomb_locations, sizeof(bl_copy));
for (int i=0; i<bombs_unaccounted_for; ++i) {
int j = i + (std::rand() % (unknowns.size()-i));
std::swap(unknowns, unknowns[j]);
int b = unknowns;
bl_copy[b/12] = true;
}

// Verify consistency
for (int i=1; i<11; ++i) {
for (int j=1; j<11; ++j) {
if (knowns[j] >= 0) {
int c = 0;
for (int di=-1; di<=1; di++)
for (int dj=-1; dj<=1; dj++)
c += bl_copy[i+di][j+dj];
if (knowns[j] != c)
goto NOT_CONSISTENT;
}
}
}

std::cout << "count=" << ++count << '\n';
// Accumulate danger values
for (int i=0; i<12; ++i) {
for (int j=0; j<12; ++j) {
danger[j] += bl_copy[j];
std::cout << (knowns[j]==-1 ? danger[j] : -1) << ' ';
}
std::cout << '\n';
}
NOT_CONSISTENT:;
}
}

After running it for a while, it produces this:
count=10000
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 1718 1682 1692 1649 1713 1728 1746 1709 -1
-1 -1 -1 8282 4904 5001 4985 1708 1708 1736 1718 -1
-1 -1 -1 -1 5059 -1 5072 1702 1724 1721 1712 -1
-1 5023 4977 1718 4949 5068 4962 1707 1641 1652 1751 -1
-1 1640 1207 1246 1283 1669 1701 1662 1689 1693 1758 -1
-1 1671 1264 -1 1188 1714 1657 1663 1722 1672 1678 -1
-1 1661 1222 1333 1257 1659 1689 1658 1687 1697 1730 -1
-1 1615 1652 1803 1785 1762 1770 1590 1643 1628 1649 -1
-1 1716 1675 1669 1634 1556 1706 1655 1621 1694 1701 -1
-1 1711 1719 1671 1746 1617 1728 1659 1675 1738 1725 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

There is one spot marked "1188". That is the unknown spot that seems to have the lowest chance of being a bomb, so that's what I would open next.

EDIT: If there were any spots marked "10000", it's a pretty sure bet that there is a bomb there, so you can just mark it. Edited by Álvaro

##### Share on other sites

But isn't this algorithm to find a field with lowest possibility to be mined? I already found fields which are safe for sure. And i already found fields which are mined for sure. Now i want choose one field of all not mined fields i found, and it has to be a field which will open the biggest area around it and enables me to find the highest number of mines in next step.

##### Share on other sites
Yes, that algorithm does try to minimize the probability of blowing up in the next move. Isn't that what you wanted?

Let's assume you have correctly identified fields that are safe for sure. What if there aren't any? That's the problem I was trying to solve, which seems to me like a more important aspect of minesweeper AI than trying to minimize the number of clicks or something like that.

##### Share on other sites

OK it is for a case that at some moment of the game i will HAVE to guess what field to open because i didn't find any. But, what if i know that i will find more than 1 safe field? I already said that one fact i can use in this task is that test cases for this algorithm will always be solvable WITHOUT guessing. So i will always be able to find at least 1 safe field. And now what i want is which field i should choose to open? If i found 6 safe fields  how to choose a field which will enables my algorithm to find highest number of mines in next step.

For example until this moment algorithm found 3 mines and they are flagged.

Also 6 safe fields were found.

For example if i open first of 6 safe fields, i will be able to flag 2 mined fields.

if i open second safe field i will be able to find 3 mines etc etc etc...

if i open for example 5th safe field i will be able to find 6 mines in next step.

So, i will choose to open 5th field because it is a field which will leads me to find the highest number of mines in next step.But how to find that 5th safe field is field i have to open?

Edited by Backward

##### Share on other sites
You can still use a randomized algorithm of the style of the one I posted. Instead of measuring the probability of each field containing a mine, you can compute some other metric (the expected amount of information obtained by clicking on that field is probably about the best you can do, but it's a bit tricky).

The fact that a test case can be solved without guessing doesn't mean that the two very basic rules you posted are enough to do all the deductions. For instance:
0 0 0 1 - -
0 0 0 1 - -
1 1 1 2 x -
- - - - - -
- - - - - -
Mines left: 4
You can deduce that the field mark x' is safe, but your basic rules won't find that out. My algorithm, however, will have a nice 0 in
that spot.

Another example:
- - - - - -
- - - - - -
- - - 4 - -
- - - - - -
- - - - - -
Mines left: 4
In this case, anything not adjacent to the 4' is safe. Edited by Álvaro

##### Share on other sites

I wrote another algorithm which works with sets and i found all combinations like Paradigm Shifter wrote in his post in this topic and i found some intersections and finally algorithm generated all mined and all safe fields. So, there is no problem to detect safe and mined fields even in your examples.

(? ? ? 1 0 0 0 0 0)
(? ? 1 1 0 0 0 1 1)
(? ? 1 0 0 0 0 1 ?)
(? ? 1 0 0 0 0 1 ?)
(? ? 1 0 0 0 1 1 ?)
(? ? 1 0 0 0 1 ? ?)
(? ? 1 0 0 0 1 ? ?)
(? ? 1 1 2 2 1 ? ?)
(? ? ? ? ? ? ? ? ?)

?  S  *  1  0  0  0  0  0
?  S  1  1  0  0  0  1  1
?  S  1  0  0  0  0  1  *
?  *   1  0  0  0  0  1  S
?  S  1  0  0  0  1  1  S
?  S  1  0  0  0  1   *  S
?  *   1  0  0  0  1  S  ?
?  S  1  1  2  2  1  S  ?
?  S  S  S  *   *  S  S  ?

First minefield is starting minefield for my algorithm and second algorithm is what i got after my algorithm was finished. As you can see i found everything i could find and now i have to open next "S" field. But which one should i choose to open :) ? For example in this case which one would you choose? There is no rule. Maybe i can check which "S" field has the least number of flagged fields in 5x5 area around that "S" field???

##### Share on other sites

(the expected amount of information obtained by clicking on that field is probably about the best you can do, but it's a bit tricky).

What do you mean?

##### Share on other sites

(the expected amount of information obtained by clicking on that field is probably about the best you can do, but it's a bit tricky).

What do you mean?

The minesweeper problem is figuring out where all the mines are. If there are 20 mines in a 10x10 field, there are initially 535983370403809600000 possible arrangements of mines, and the objective is to eliminate them as quickly as possible. Every time you click on a field, you either blow up or you find out more information about the board. As you gain information, the number of possibilities is reduced.

The amount of hidden information left on the board is the base-2 logarithm of the number of arrangements that are compatible with what is currently known, and the result is measured in bits (about 68.86075 bits in the example). When you click on a field you reduce the number of possibilities, and you can compute the amount of hidden information left. The difference between the amount of hidden information before and after the click is the number of bits of information that the click gave you. For instance, if clicking on a field cuts the number of arrangements in half, it gave you 1 bit of information; if it cuts it down by a factor of 8, it gave you 3 bits.

Now consider your safe fields. You don't know how much information clicking on each one of them is going to give you, but you can compute the expected value (a.k.a. "entropy") for each move and pick the maximum.

Estimating the entropy of each move is a bit tricky, but I think it's doable. You would have to generate lots of random arrangements that satisfy the conditions (as I did in the program I posted, or perhaps being smarter about it) and then look at what you would learn if you clicked on a particular field (i.e., what number would pop up there, and if it's 0 what other parts of the board would be opened and what numbers they would have). After you accumulate a whole bunch of these, you will have the random arrangements classified by what you would learn, and from the size of each class you can estimate the entropy. I think the formula is something like this:
entropy = sum over classes (log2(class_size / number_of_samples) * class_size) / number_of_samples
Edited by Álvaro

##### Share on other sites

hahaha This looks like cheating method...If you remember of any other method write here.