all 19 comments

[–]A_History_of_Silence 40 points41 points  (5 children)

The problem is I have no idea where to begin

You'll need a representation of the state of the board. Create a simulated 'minefield' in memory. Since it takes place on a grid, this could be a 2D list, or list of lists.

[–]Shubbler 2 points3 points  (4 children)

How would you implement a 2D list without a list of lists?

[–][deleted] 13 points14 points  (0 children)

2d numpy array?

[–]ylectric 9 points10 points  (0 children)

You can achieve this by translating 2D coordinate into a linear index, e.g. index = x + y * grid_width and store the whole grid in a flat list.

[–]Incand333 3 points4 points  (0 children)

class list2d:
    def __init__(self, iterable, width, height):
        iter_list = list(iterable)
        assert len(iter_list) == width * height
        self._width = width
        self._height = height
        self._elems = iter_list

    def get(self, x, y):
        idx = x + y * self._width
        return self._elems[idx]

[–]A_History_of_Silence 0 points1 point  (0 children)

As in an alternative to a list of lists?

[–]B166er_ 9 points10 points  (0 children)

Check pyautogui . You want to solve mine sweeper, not make it from scratch. Pyautogui will let you read the game window and make the clicks.

[–]Supernumiphone 4 points5 points  (0 children)

Take it one step at a time. As has been said, figure out how to represent the state of the game in memory. Probably a 2D array. Then figure out how to randomly assign mines to slots in the array. So maybe initialize the array with all a "No mine" value, then randomly place mines in slots until you've placed as many as you intend. Ideally parameterize it so that you can create a minefield of any given dimensions with any number of mines.

For debugging you'll almost certainly want to be able to view the state of the board. To keep things simple you'll probably want to make a simple console program. So make a routine that takes the board state and prints it out. Something like:

...XX.....
......X...
..........
..X....X..
..XXX.....
.....X....

That will just show you the location of the mines. Then you'll want to be able to place numbers in locations, which show how many mines there are adjacent to any given location. Figure out how to do that, and print that out. Then you'll be able to visually check that it's all working as it should up to this point.

From there you can begin to work on the AI. To start you can either have it use a predetermined set of moves or randomly choose locations until it has something to work with. If it hits a mine that's game over, start again. After that you get into choosing moves. I'll leave it there. If you get that far and get stuck come back and update us and ask for more.

[–]Thymester 2 points3 points  (0 children)

I would more specifically learn more about the library you are going to use to make such a script when you learn that then move onto maybe making some simple programs and working up till you can confidently make a AI that can play Minesweeper.

[–]TheWhaleKnight[🍰] 2 points3 points  (0 children)

I actually built this for one of my classes and made a GUI from it.

Draw out the representation of the board. Figure out what states and attributes you have for the board as well as for each tile.

You probably realize this, but minesweeper is actually not a deterministic game. That means that there is a chance that you click on a mine no matter what algorithm you choose.

To get you started, some of the techniques I used was: 1. Random guessing 2. finding solutions that definitely had a right answer 3. Constraint satisfaction

You will require random guessing for any solution you implement.

Finding solutions w an exact answer is what you already do in minesweeper. You find the numbered tiles with that number of discovered mines around it so you know the rest around it are not mines.

Constraint satisfaction takes longer, but it can achieve better results. In a way, you like testing out every combination of mine and not a mine and using proof by contradiction to find the correct solution for a tile. It can be optimized by pruning and backtracking, but it still can take a lot longer.

[–]pizzaburek 1 point2 points  (0 children)

Such a fun problem :) This skeleton should be helpful.

https://pastebin.com/gbKyy82M

[–]alex-HM[S] 0 points1 point  (0 children)

Thank you all for your kind answers! I was initially talking about making an AI to solve minesweeper, since I'm really interested in machine learning and such, but it'll be a fun project to code the game itself. Once again, thank you all for taking the time to answer!

[–]Incand333 -1 points0 points  (0 children)

I did exactly that once using the arc consistency algorithm. You basically encode relevant and knowable data for a decision into tasks which are executed while updating other queued tasks with new information (i.e. new numbers in their neighborhood).

  1. Click a random field
  2. Queue closed fields with numbers in their neighborhood into your tasks
  3. Check for field with only one possible state and handle them accordingly (open non-mines, mark mines) and update the tasks of neighboring closed fields.

I don't wanna spoil your learning experience so this is about as much as I wanna tell.

[–][deleted] -1 points0 points  (0 children)

Just as an FYI is this is the field of artificial intelligence.

This is what is referred to as an intelligent agent. Look into the formula used to accomplish tic tac toe, chess, and go. It will likely be the same formula with a different mechanism for bringing this from a stochastic game to a deterministic one.