Followers 0

# A* applied to minesweeper game

## 46 posts in this topic

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?

0

##### Share on other sites

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

##### Share on other sites

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?

0

##### Share on other sites

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

0

##### Share on other sites

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

##### Share on other sites

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

0

##### Share on other sites
bl_copy[b/12][b] = true;


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

0

##### Share on other sites

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
0

##### Share on other sites

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

0

##### Share on other sites

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
0

##### Share on other sites
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.
0

##### Share on other sites


{-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
0

##### Share on other sites
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
0

##### Share on other sites

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
0

##### Share on other sites
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.
0

##### Share on other sites

I was thinking about finding all possible combination for squares with number.

? ? ? ? ?
? ? 2 ? ?
? 2 2 1 1
? ? 1 0 0
? ? 1 0 0

In this example i will find all possible locations for every number. For example square 2 in second row.

? X ? ? ?
? X 2 ? ?
? 2 2 1 1
? ? 1 0 0
? ? 1 0 0

? ? X ? ?
? X 2 ? ?
? 2 2 1 1
? ? 1 0 0
? ? 1 0 0

? ? ? X ?
? X 2 ? ?
? 2 2 1 1
? ? 1 0 0
? ? 1 0 0

? ? ? ? ?
? X 2 X ?
? 2 2 1 1
? ? 1 0 0
? ? 1 0 0

? X X ? ?
? ? 2 ? ?
? 2 2 1 1
? ? 1 0 0
? ? 1 0 0

? X ? X ?
? ? 2 ? ?
? 2 2 1 1
? ? 1 0 0
? ? 1 0 0

? X ? ? ?
? ? 2 X ?
? 2 2 1 1
? ? 1 0 0
? ? 1 0 0

? ? X X ?
? ? 2 ? ?
? 2 2 1 1
? ? 1 0 0
? ? 1 0 0

? ? X ? ?
? ? 2 X ?
? 2 2 1 1
? ? 1 0 0
? ? 1 0 0

? ? ? X ?
? ? 2 X ?
? 2 2 1 1
? ? 1 0 0
? ? 1 0 0

There are 10 possible locations for mines around this square. If we find possible locations for all squares with number, then we can calculate probability without guessing and also we can be sure that all possible consistent combinations are found and here in your algorithm we do random distribution and we can't be sure for bigger tables that all consistent possibilities were computed.

0

##### Share on other sites
You don't know if all those 10 distributions of two bombs around that "2" are equally likely or not.
0

##### Share on other sites

You don't know if all those 10 distributions of two bombs around that "2" are equally likely or not.

What do you mean? I didn't understand you. If these 10 distributions are all possible for square "2", and if we find for rest of squares all possible distributions, we can make all combinations between all squares with numbers and only possible combinations will be found.

0

##### Share on other sites
Oh, perhaps I didn't understand exactly what you proposed. Would you mind fleshing out your algorithm so I can understand better?
0

##### Share on other sites

ok, look at those 10 combinations.

? B C D ?
? A 2 E ?
? 2 2 1 1
? ? 1 0 0
? ? 1 0 0

These 10 possibilities are: ab,ac,ad,ae,bc,bd,be,cd,ce,de. Now we can do same thing for rest of numbers and if we pair each possibility for each square with possibilities from another squares and do intersect, we will get all possible positions of mines and not mines. I was thinking about applying this algorithm first and then we can apply your algorithm. Maybe it can give better results.

0

##### Share on other sites
The "pair each possibility for each square with possibilities from another squares and do intersect" part sounds ill defined to me. If you can describe an algorithm to do that, that would be great. Otherwise I can't tell if it would work without bias or not.
0

##### Share on other sites

The "pair each possibility for each square with possibilities from another squares and do intersect" part sounds ill defined to me. If you can describe an algorithm to do that, that would be great. Otherwise I can't tell if it would work without bias or not.

0

## Create an account

Register a new account

Followers 0

• 28
• 13
• 11
• 31
• 20