This is how it started. My gay friend and I played Minesweeper together, and today he came to tell me that he had passed the game!

All kinds of show off, as shown below!

So, I developed an automatic minesweeping program in Python! I pretended to be pushy with him, and suddenly called my brother and asked me to teach him

All right, enough nonsense! This article is used for entertainment, welcome big brothers to give advice!

Get into the business


Automatic minesweeping usually comes in two forms, one is to read data from memory, and the other is to analyze the data from images and simulate mouse operations. I used the second method here.

First, preparation

Minesweeper game: I am Win10, there is no default minesweeper, so go to download minesweeper network

Python 3: My version is Python 3.6.1

Python third-party libraries: win32api win32gui, win32con, Pillow, numpy, opencv

I give my third party library and version for reference only

Two, key code composition

1. Find the game window and coordinates

2. Lock and capture the image of minefield

3. RGBA value of each image

4. Scan the minefield image and save it to a TWO-DIMENSIONAL array map

1, Python installation package 2, programmer e-books: 3. Three copies of python project source code: (Python cracking WiFi password, VIP video permission cracking, crawler automation) 4. PPT teaching plan for basic learning 5. 30) Free choice 6. Sharing of career planning for programmers 7. Making money in spare time 5. Demining algorithm

I’m going to use the most basic algorithm here

Let’s start with a dot

Scan all numbers, if the surrounding blank + insert flag == number, then blank are thunder, right click blank insert flag

Scan all numbers, if the flag around == number, then there is no thunder blank, left click blank

Loops 2 and 3, if none match the criteria, click a random white block

This algorithm has a good pass rate in the primary and intermediate levels, but a terrible success rate in the advanced level, mainly because it does not consider the logical combination and the probability of white blocks being thunder. These two points can be improved to improve the success rate

How’s that? Welcome big guy to give advice!