Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to realize automatic minesweeping notebook

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use Python to achieve automatic minesweeping small script, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

I. preparatory work

1. The Minesweeper game

I am win10, there is no default minesweeping, so go to the minesweeping network to download

Http://www.saolei.net/BBS/

2.python 3

My version is python 3.6.1

The third-party library of 3.python

Win32api,win32gui,win32con,Pillow,numpy,opencv

Can be installed through pip install-- upgrade SomePackage

Note: some versions download pywin32, but others upgrade pywin32 to the highest level and download pypiwin32 automatically. Each python version may be slightly different.

I give my third-party library and version for reference only.

Second, the composition of key codes

1. Find the game window and coordinates

# Minesweeper Game window class_name = "TMain" title_name = "Minesweeper Arbiter" hwnd = win32gui.FindWindow (class_name, title_name) # window coordinates left = 0 top = 0 right = 0 bottom = 0 if hwnd: print ("find window") left, top, right Bottom = win32gui.GetWindowRect (hwnd) # win32gui.SetForegroundWindow (hwnd) print ("window coordinates:") print (str (left) +'+ str (right) +'+ str (top) +'+ str (bottom)) else: print (window not found)

two。 Lock and capture the minefield image

# Lock minefield coordinates # remove surrounding function buttons and redundant interface # specific pixel values are judged by the screenshot of QQ left + = 15 top + = 101 right-= 15 bottom-= 42 # capture minefield image rect = (left, top, right, bottom) img = ImageGrab.grab (). Crop (rect)

3. RGBA value of each image

# numbers 1-8 number of mines # 0 not opened # ed opened blank # hongqi Red Flag # boom ordinary mine # boom_red stepped on mine rgba_ed = [(225,192,192,192), (31, (128,128,128)] rgba_hongqi = [(54, (255,255,255), (175,255,0,0), (109,192,192,192)), (54) (128,128,128), (22, (0,0,0))] rgba_0 = [(54, (255,255,255), (148,( 192,192,192)), (54, (128,128,128)] rgba_1 = [(185,122,122,192), (31, (128,128,128)), (40, (0,0,255))] rgba_2 = [(160,192)] rgba_2 192,192), (31, (128,128,128)), (65, (0128,0))] rgba_3 = [(62,255,0,0), (163,192,192,192), (31, (128,128,192))] rgba_4 = [(169,128,122,192)), (31, (128,128,128)), (56, (0,0)) ] rgba_5 = [(70, (128,0,0)), (155,( 192,192,192)), (31, (128,122,192))] rgba_6 = [(153,192,192,192)), (31, (128,128,128)), (72, (0128,128))] rgba_8 = [(149,192,192,192)), (107,128,128,128) ] rgba_boom = [(4, (255,255,255)), (144,192,192,192), (31, (128,128,128)), (77, (0,0,0))] rgba_boom_red = [(4,255,255,255), (144,255,0,0)), (31, (128,128,128)), (77, (0,0,0))]

4. The scanned minefield image is saved to a two-dimensional array map

# scan minefield image def showmap (): img = ImageGrab.grab () .crop (rect) for y in range (blocks_y): for x in range (blocks_x): this_image = img.crop ((x * block_width, y * block_height, (x + 1) * block_width) (y + 1) * block_height) if this_image.getcolors () = = rgba_0: map [y] [x] = 0 elif this_image.getcolors () = rgba_1: map [y] [x] = 1 elif this_image.getcolors () = rgba_2: map [y] [X] = 2 elif this_image.getcolors () = = rgba_3: map [y] [x] = 3 elif this_image.getcolors () = = rgba_4: map [y] [x] = 4 elif this_image.getcolors () = rgba_5: map [y] [x] = 5 Elif this_image.getcolors () = = rgba_6: map [y] [x] = 6 elif this_image.getcolors () = = rgba_8: map [y] [x] = 8 elif this_image.getcolors () = rgba_ed: map [y] [x] =-1 elif this_image.getcolors () = = rgba_hongqi: map [y] [x] =-4 elif this_image.getcolors () = = rgba_boom or this_image.getcolors () = = rgba_boom_red: global gameover gameover = 1 break # sys.exit (0) else: Print ("unrecognized image") print ("coordinates") print ((y) X) print ("color") print (this_image.getcolors ()) sys.exit (0) # print (map)

5. Minesweeping algorithm

The most basic algorithm I use here

1. First point out a point.

two。 Scan all the numbers, if the surrounding blank + planting flag = = number, then the blank is thunderous, right-click on the blank flag

3. Scan all the numbers, if the flag is planted around the number, there is no thunder in the blank, left click on the blank

4. Loops 2 and 3, if no one meets the criteria, randomly click on a white block

# Flag planting def banner (): showmap () for y in range (blocks_y): for x in range (blocks_x): if 1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report