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

46 replies to this topic

### #21Álvaro  Members

Posted 18 April 2013 - 09:01 AM

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, 18 April 2013 - 09:01 AM.

### #22Backward  Members

Posted 18 April 2013 - 09:32 AM

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

### #23Backward  Members

Posted 18 April 2013 - 01:47 PM

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

### #24Álvaro  Members

Posted 18 April 2013 - 02:33 PM

(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, 18 April 2013 - 02:36 PM.

### #25Backward  Members

Posted 20 April 2013 - 03:28 AM

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

### #26Backward  Members

Posted 06 May 2013 - 04:17 PM

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.

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

### #27Álvaro  Members

Posted 06 May 2013 - 07:46 PM

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.

### #28Backward  Members

Posted 06 May 2013 - 11:17 PM

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[i][j] == -1)
unknowns.push_back(i*12+j);
bomb_locations[i][j] = (knowns[i][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?

### #29Dragonsoulj  Members

Posted 07 May 2013 - 01:58 AM

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

### #30Álvaro  Members

Posted 07 May 2013 - 04:54 AM

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[i][j] == -1)
unknowns.push_back(i*12+j);
bomb_locations[i][j] = (knowns[i][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.

Posted 07 May 2013 - 05:48 AM

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

On the version that ships with Windows? I never have... Are you sure it wasn't a minesweeper clone?

EDIT: And a quick test with a 10x10 board and the maximum number of mines (looks like 81, I tried 99 but it set it to 81) seems to confirm that the first click is always safe (played about 5 games).

Edited by Paradigm Shifter, 07 May 2013 - 05:51 AM.

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

### #32Backward  Members

Posted 07 May 2013 - 02:31 PM

bl_copy[b/12][b] = true;


I think these indexes are bad. Should it be bl_copy [(b - b %12)/12] [b%12] ?

### #33Álvaro  Members

Posted 07 May 2013 - 06:42 PM

bl_copy[b/12][b] = true;


I think these indexes are bad. Should it be bl_copy [(b - b )/12] [b] ?

Hmmm... It should be bl_copy[bl/12][b%12]. I don't know what happened there... Editor ate my modulus operator?

EDIT: Yes! The editor eats modulus operators!

Edited by Álvaro, 07 May 2013 - 06:43 PM.

### #34Backward  Members

Posted 08 May 2013 - 02:29 PM

For bigger tables i don't get results... Is it possible to optimize it somehow?

### #35Backward  Members

Posted 09 May 2013 - 01:54 AM

Can i use any other algorithm to find probabilities? Does anyone know which one gives best results for all table sizes 9x9, 16x16 and 30*16?

Edited by Backward, 09 May 2013 - 01:54 AM.

### #36Álvaro  Members

Posted 09 May 2013 - 05:29 AM

Can you post an example of a position where the naive algorithm I posted doesn't produce any results? I am sure I can think of some refinements to make the algorithm more practical, and having some example to test on would help.

### #37Backward  Members

Posted 09 May 2013 - 06:09 AM



{-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3},
{-3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-3},
{-3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-3},
{-3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-3},
{-3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-3},
{-3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-3},
{-3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-3},
{-3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-3},
{-3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-3},
{-3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-3},
{-3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-3},
{-3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-3},
{-3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-3},
{-3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-2,-1,-1,-1,-1,-3},
{-3,-1,-1,-1,-1,-1,-1,-2,-1,-2, 1, 1, 1, 1, 1,-1,-1,-3},
{-3,-1,-1,-1,-1,-1,-1, 2, 2, 1, 1, 0, 0, 0, 1,-1,-1,-3},
{-3,-1,-1,-1,-1,-1,-1, 1, 0, 0, 0, 0, 0, 0, 1,-1,-1,-3},
{-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3}


This is the table i got when i opened one field. I found 3 mines and they are flagged. I put this table in your algorithm and nothing happens. Table has 40 mines (16x16).

Edited by Backward, 09 May 2013 - 06:30 AM.

### #38Álvaro  Members

Posted 09 May 2013 - 07:33 AM

After adapting my code to a board of size 16x16 with 37 bombs left to be found, it produced this after less than 4 seconds:
  -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1
-1 1565 1567 1565 1619 1614 1552 1621 1567 1625 1652 1588 1620 1549 1631 1614 1591   -1
-1 1593 1551 1621 1566 1549 1592 1624 1614 1504 1585 1624 1587 1567 1643 1568 1536   -1
-1 1679 1596 1583 1599 1594 1607 1565 1577 1519 1623 1648 1680 1601 1561 1559 1554   -1
-1 1599 1551 1612 1533 1579 1612 1594 1590 1564 1568 1594 1577 1564 1549 1612 1587   -1
-1 1548 1622 1603 1644 1644 1632 1535 1639 1630 1577 1565 1570 1565 1623 1621 1636   -1
-1 1597 1594 1589 1594 1646 1574 1637 1618 1597 1629 1566 1566 1595 1521 1682 1595   -1
-1 1660 1589 1587 1640 1543 1578 1546 1616 1559 1554 1604 1580 1601 1554 1528 1555   -1
-1 1487 1564 1579 1654 1592 1583 1551 1606 1585 1574 1677 1572 1590 1620 1629 1597   -1
-1 1589 1584 1585 1605 1565 1592 1532 1636 1494 1617 1616 1530 1656 1612 1597 1625   -1
-1 1568 1569 1638 1608 1690 1597 1658 1529 1580 1588 1558 1575 1644 1624 1533 1560   -1
-1 1608 1587 1646 1595 1604 1588 1630 1643 1594 1613 1638 1603 1603 1537 1581 1603   -1
-1 1617 1584 1591 1601 1573 1638 1610 1542 1558 1603 1599 1550 1655 1578 1599 1603   -1
-1 1543 1595 1570 1567 1522 1609 1565 1561    0    0    0   -1    0    0 1638 1503   -1
-1 1507 1555 1615 1560 1615    0   -1    0   -1   -1   -1   -1   -1   -1    0 1632   -1
-1 1515 1614 1625 1533 1616 5022   -1   -1   -1   -1   -1   -1   -1   -1 8362 1581   -1
-1 1570 1593 1573 1596 1626 4978   -1   -1   -1   -1   -1   -1   -1   -1 1638 1571   -1
-1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1

EDIT: Better formatting.

Edited by Álvaro, 09 May 2013 - 07:35 AM.

### #39Backward  Members

Posted 09 May 2013 - 08:42 AM

In your algorithm if i understood well, you put randomly all rest mines and then you check is it possible. But there could be many situations when state is not consistent for example there is a square 2 but there are 3 mines around it. Why don't we find all possible consistent combinations and then check for every square how many times there was a mine? It will be just a kind of special case of your algorithm.

Edited by Backward, 09 May 2013 - 10:11 AM.

### #40Álvaro  Members

Posted 09 May 2013 - 02:00 PM

I am not sure I understand your suggestion. The brute-force nature of what I proposed will make it so that in many instances only a small fraction of the random distributions tried will be consistent with the known information. It is very reasonable to try to increase this fraction to make the algorithm practical, but most simple ways to that would bias the distributions so the measured probabilities might not be correct.

It can probably be done correctly, perhaps with some variation of the Metropolis algorithm, but I expect this will be tricky.

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.