Jump to content

  • Log In with Google      Sign In   
  • Create Account


A* applied to minesweeper game


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
46 replies to this topic

#1 Backward   Members   -  Reputation: 127

Like
2Likes
Like

Posted 12 April 2013 - 08:25 AM

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?

10299095.png

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.



Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5251

Like
0Likes
Like

Posted 12 April 2013 - 08:34 AM

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.


Edited by Paradigm Shifter, 12 April 2013 - 08:44 AM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#3 Backward   Members   -  Reputation: 127

Like
0Likes
Like

Posted 12 April 2013 - 08:49 AM

76701232.png

 

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. 



#4 Paradigm Shifter   Crossbones+   -  Reputation: 5251

Like
0Likes
Like

Posted 12 April 2013 - 08:55 AM

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


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#5 Backward   Members   -  Reputation: 127

Like
0Likes
Like

Posted 12 April 2013 - 09:00 AM

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



#6 Paradigm Shifter   Crossbones+   -  Reputation: 5251

Like
0Likes
Like

Posted 12 April 2013 - 09:06 AM

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?


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#7 Cornstalks   Crossbones+   -  Reputation: 6974

Like
4Likes
Like

Posted 12 April 2013 - 10:01 AM

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, 12 April 2013 - 10:09 AM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#8 Paradigm Shifter   Crossbones+   -  Reputation: 5251

Like
0Likes
Like

Posted 12 April 2013 - 10:10 AM

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.


Edited by Paradigm Shifter, 12 April 2013 - 10:13 AM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#9 Backward   Members   -  Reputation: 127

Like
0Likes
Like

Posted 12 April 2013 - 10:23 AM

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.



#10 Paradigm Shifter   Crossbones+   -  Reputation: 5251

Like
0Likes
Like

Posted 12 April 2013 - 10:28 AM

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...


Edited by Paradigm Shifter, 12 April 2013 - 10:28 AM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#11 Backward   Members   -  Reputation: 127

Like
0Likes
Like

Posted 12 April 2013 - 10:39 AM

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, 12 April 2013 - 10:45 AM.


#12 Paradigm Shifter   Crossbones+   -  Reputation: 5251

Like
2Likes
Like

Posted 12 April 2013 - 11:01 AM

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...


Edited by Paradigm Shifter, 12 April 2013 - 11:10 AM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#13 Álvaro   Crossbones+   -  Reputation: 12866

Like
0Likes
Like

Posted 12 April 2013 - 02:50 PM

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.

#14 Backward   Members   -  Reputation: 127

Like
0Likes
Like

Posted 17 April 2013 - 08:46 AM

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? 



#15 Álvaro   Crossbones+   -  Reputation: 12866

Like
0Likes
Like

Posted 17 April 2013 - 09:04 AM

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.

#16 Backward   Members   -  Reputation: 127

Like
0Likes
Like

Posted 17 April 2013 - 09:26 AM

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. 



#17 Álvaro   Crossbones+   -  Reputation: 12866

Like
0Likes
Like

Posted 17 April 2013 - 11:41 AM

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[i][j] == -1)
        unknowns.push_back(i*12+j);
      bomb_locations[i][j] = (knowns[i][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[i], unknowns[j]);
      int b = unknowns[i];
      bl_copy[b/12][b] = true;
    }      
    
    // Verify consistency
    for (int i=1; i<11; ++i) {
      for (int j=1; j<11; ++j) {
        if (knowns[i][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[i][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[i][j] += bl_copy[i][j];
        std::cout << (knowns[i][j]==-1 ? danger[i][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, 17 April 2013 - 11:44 AM.


#18 Backward   Members   -  Reputation: 127

Like
0Likes
Like

Posted 18 April 2013 - 06:36 AM

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.



#19 Álvaro   Crossbones+   -  Reputation: 12866

Like
0Likes
Like

Posted 18 April 2013 - 08:01 AM

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.

#20 Backward   Members   -  Reputation: 127

Like
0Likes
Like

Posted 18 April 2013 - 08:18 AM

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? biggrin.png


Edited by Backward, 18 April 2013 - 08:22 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS