• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Backward

A* applied to minesweeper game

46 posts in this topic

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?

10299095.png

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.

2

Share this post


Link to post
Share on other sites

I don't see why you need A* for this... is it really a graph search?

 

And click the square above the 2 next to the 2 flags... it's safe ;) EDIT: As are all the uncovered squares next to the 1s

 

EDIT2: I'm guessing you need to know HOW to identify those spaces as flagged... I still don't see how it involves A* though... you can probably just brute-force it, or encode in some simple rules for cases like that... The problem arises when you have to guess - you need to assign probabilities for each selection and pick the most safe option.

Edited by Paradigm Shifter
0

Share this post


Link to post
Share on other sites

76701232.png

 

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. 

0

Share this post


Link to post
Share on other sites

I still didn't think about heuristic. For now i want to complete this part about finding all possible safe fields.

0

Share this post


Link to post
Share on other sites

Well, I'd suggest looking at the places with only 1 safe spot first (i.e. N-1 mines for N unknown spaces), starting with the smallest N (so the 2 in the picture next to 1s would fit the bill). Then you can examine all the combinations (there are N of them) looking for a contradiction.

 

After that it sounds like it is going to be a recursive problem... have you done any googling?

0

Share this post


Link to post
Share on other sites

Presumably the OP wants to find a way to solve minesweeper using the least number of clicks. I think that is going to be a hard problem, unless you already know the locations of all the mines, then you could graph search it, but that isn't how the game is played... (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).

 

You can get a situation where you have to make a guess, in that case you need to work out the guess least likely to reveal a mine but that is a probability question rather than a graph search one. Sometimes you end up with a 50-50 win/lose choice though... so it can be just a coin flip.

Edited by Paradigm Shifter
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

There's a big difference between "opened minefield" and "locations of all mines known at the start"... if you don't know the locations of ALL the mines (i.e. you have missing information) I can't see a way to use a graph search...

 

Every click removes exactly 1 blank space unless there are no adjacent mines (when it does a flood fill of all 0 neighbouring mine squares)... but there is no way to know which squares will be 0s in advance...

Edited by Paradigm Shifter
0

Share this post


Link to post
Share on other sites

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.

Edited by Backward
0

Share this post


Link to post
Share on other sites

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

Edited by Paradigm Shifter
2

Share this post


Link to post
Share on other sites

Also i HAVE to use A*. It is not my choice.

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

Share this post


Link to post
Share on other sites

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? 

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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. 

0

Share this post


Link to post
Share on other sites

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. Edited by Álvaro
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
Yes, that algorithm does try to minimize the probability of blowing up in the next move. Isn't that what you wanted?

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

Share this post


Link to post
Share on other sites

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

Edited by Backward
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites


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

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0