Inspiration

I have loved playing minesweeper since I was a child and have always been fascinated by the logic involved in solving it. I thought it would fascinating to automate/enhance this classic game.

What it does

The application maps out the screen, and calculates the locations of each of the squares. Then, once the user presses the respective key, the application takes control of the mouse, and plays the game for the user, either in the form of one move (hint), or the entire game (speed run). The app reads and parses the screen through periodic screenshots, to figure out the state of the grid. Then, it applies a complex algorithm to determine the next valid move.

How I built it

I used Python to both parse the screen, as well as run the algorithm. The input/output was controlled by pyautogui, which can move/click with the mouse, as well as read the screen.

Challenges I ran into

It was challenging to optimize the code:

  1. The parsing had to be seriously optimized, as checking every pixel is very inefficient, but at the same time, you need to accurately determine the values of each cell. This was also made much more difficult by the fact that the animation of the game sometimes changed the color of cells.
  2. The algorithm had certain tradeoffs, as there were essentially 2 algorithms: one of them determined the mine-status of some of the squares quickly, while the other (depth first-search) had high time complexity but always found all the valid squares. I had to use tactics like searching connected components separately, applying the faster algorithm first, and pruning the search space which helped to improve the speed of the application.

Accomplishments that I'm proud of

I was able to add cool features such as giving hints for the cells closest to the user's mouse location. Also, it was incredibly satisfying to see the grid get solved very quickly, sometimes in seconds, with excellent accuracy.

Built With

Share this project:

Updates