This paper describes the implementation method of Python automatic minesweeper. Share to everyone for your reference. Specific as follows:
#pyWinmineCrack. py# coding:utf-8import win32guiimport win32processimport win32conimport win32apifrom ctypes Import *# The maximum number of rows in the minefield max_rows = 24max_columns = 30# The starting coordinates of the grid on the form and the width of each lattice mine_begin_x = 0xcmine_begin_y = 0x37mine_grid_width = 0x10mine_grid_height = 0x10# Border, no thunder, internal representation of thunder Mine_boarder = 0x10mine_safe = 0x0fmine_danger = 0x8f# "mined area" storage address in minesweeper Board_ ADDR = 0x1005340class Sminectrl (Structure): _fields_ = [("HWnd", C_uint), ("board", (C_byte * (Max_columns + 2)) * (Max_r OWS + 2)), ("Rows", C_byte), ("columns", c_byte)]kernel32 = Windll. LoadLibrary ("kernel32.dll") ReadProcessMemory = kernel32. Readprocessmemorywriteprocessmemory = kernel32. Writeprocessmemoryopenprocess = kernel32. Openprocessctrldata = Sminectrl () #找到扫雷程序并打开对应进程try: Ctrldata.hwnd = Win32gui. FindWindow ("Minesweeper", "Minesweeper") Except:win32api. MessageBox (0, "Please run the minesweeper first", "Error! ", Win32con. Mb_iconerror) exit (0) hreadid, ProcessID = win32process. GetWindowThreadProcessId (ctrldata.hwnd) hproc = OpenProcess (Win32con. Process_all_access, 0, ProcessID) #读Mined area Data Bytesread = C_ulong (0) readprocessmemory (hproc, Board_addr, ByRef (Ctrldata.board), SMineCtrl.board.size, ByRef ( Bytesread) if (bytesread.value! = SMineCtrl.board.size): str = "ReadProcessMemory error, only read", Bytesread.value, "sh Ould read ", SMineCtrl.board.size Win32API. MessageBox (0, str, "Error! ", Win32con. Mb_iconerror) exit () #获取本次程序雷区的实际大小ctrlData. rows = 0ctrldata.columns = 0for i in range (0, Max_columns + 2): if Mine_boarder = = Ctrldata.board[0]: Ctrldata.columns + = 1 Else:breakctrlData.columns-= 2 for I in range (1, max_rows + 1): If Mine_bo Arder! = ctrldata.board[1]: ctrldata.rows + = 1 else:break# analog mouse Click action for I in range (0, Ctrldata.rows): for J in range (0, Ctr Ldata.columns): if Mine_safe = = ctrldata.board[i + 1][j + 1]: Win32API. SendMessage (Ctrldata.hwnd, Win32con. Wm_lbuttondown, Win32con.mk_lbutton, Win32API. Makelong (mine_begin_x + mine_grid_width * j + mine_grid_width/2, mine_begin_y + mine_grid_height * i + MINE_GRID_HEIGH T/2)) Win32API. SendMessage (Ctrldata.HWnd, Win32con. Wm_lbuttonup,win32con.mk_lbutton, Win32API. Makelong (mine_begin_x + mine_grid_width * j + mine_grid_width/2, mine_begin_y + mine_grid_height * i + MINE_GRID_HEIGH T/2)) #搞定!win32api. MessageBox (0, "Done! "," Information ", Win32con. Mb_iconinformation)
Hopefully this article will help you with Python programming.