A* applied to minesweeper game

Started by
45 comments, last by Backward 10 years, 11 months ago
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.
Advertisement

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

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

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

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.

Can you write pseudocode for this algorithm? Can i use this to discover zero fields?

Can you write pseudocode for this algorithm? Can i use this to discover zero fields?

I actually posted code... Is that not enough? Is it too hard to read?

Yes, you can use this to discover zero fields and bombs too, although there might be more direct ways to do that.

Can you write pseudocode for this algorithm? Can i use this to discover zero fields?

I actually posted code... Is that not enough? Is it too hard to read?

Yes, you can use this to discover zero fields and bombs too, although there might be more direct ways to do that.


 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);

I understood just this. Other parts with random values i didn't understand.

although there might be more direct ways to do that.

Like what?

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

Uh...I've hit a mine on the first move. Just throwing that one out there.



Can you write pseudocode for this algorithm? Can i use this to discover zero fields?

I actually posted code... Is that not enough? Is it too hard to read?

Yes, you can use this to discover zero fields and bombs too, although there might be more direct ways to do that.



 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);
I understood just this. Other parts with random values i didn't understand.


1. Fill up a vector with the unknown locations and marking the known bomb locations. (This is the part you understood.)
2. Generate a random distribution of bombs (bl_copy) by making a copy of the known bomb locations and randomly distributing the remaining bombs (the way I think of the algorithm to do that is to have a deck whose cards are unknown locations, shuffle it and deal the required number of cards).
3. Verify that the generated random distribution of bombs is consistent with all the known counts. Go back to 2 if it is not.
4. Accumulate danger values in the locations where there are bombs in the random distribution.
5. If we don't have enough examples (10,000 in my code), go back to 2.


although there might be more direct ways to do that.

Like what?


Restrict yourself to unknowns that are adjacent to knowns. Assume one of them is a bomb and try to find a consistent distribution of all the other bombs. Assume it is not a bomb, and do the same thing. If one of these searches for consistency fails, you know if the unknown is a bomb or not.

The consistency search can be implemented using backtracking.

You can probably speed things up a lot by identifying some common cases (e.g., if we have already located the correct number of bombs adjacent to a known square, the other adjacent squares are not bombs) before attempting all the searches for consistency.

This topic is closed to new replies.

Advertisement