# Backward

Member

23

1. ## A* applied to minesweeper game

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

3. ## A* applied to minesweeper game

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.
4. ## A* applied to minesweeper game

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.
5. ## A* applied to minesweeper game

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.
6. ## A* applied to minesweeper game

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.
7. ## A* applied to minesweeper game

{-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).
8. ## A* applied to minesweeper game

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?
9. ## A* applied to minesweeper game

For bigger tables i don't get results... Is it possible to optimize it somehow?
10. ## A* applied to minesweeper game

bl_copy[b/12] = true;   I think these indexes are bad. Should it be bl_copy [(b - b %12)/12] [b%12] ?
11. ## A* applied to minesweeper game

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.   Like what?
12. ## A* applied to minesweeper game

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?
13. ## A* applied to minesweeper game

hahaha This looks like cheating method...If you remember of any other method write here.
14. ## A* applied to minesweeper game

What do you mean?
15. ## A* applied to minesweeper game

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???
16. ## A* applied to minesweeper game

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?
17. ## A* applied to minesweeper game

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.
18. ## A* applied to minesweeper game

If you have a time, please try to make graphics explanation of your algorithm.
19. ## A* applied to minesweeper game

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?
20. ## A* applied to minesweeper game

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.
21. ## A* applied to minesweeper game

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.
22. ## A* applied to minesweeper game

I still didn't think about heuristic. For now i want to complete this part about finding all possible safe fields.
23. ## A* applied to minesweeper game

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.