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.
A* applied to minesweeper game
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...
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.Also i HAVE to use A*. It is not my choice.
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?
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.
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.
I am not going to post graphics, but I can show you some crude code:If you have a time, please try to make graphics explanation of your algorithm.
#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.
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.
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.
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?