自動掃雷一般分為兩種,一種是讀取記憶體資料,而另一種是通過分析圖片獲得資料,並通過模擬滑鼠操作,這裡我用的是第二種方式。
一、準備工作
1.掃雷遊戲
我是win10,沒有預設的掃雷,所以去掃雷網下載
http://www.saolei.net/BBS/

推薦下我自己建立的Python學習交流群960410445,這是Python學習交流的地方,不管你是小白還是大牛,小編都歡迎,不定期分享乾貨,包括我整理的一份適合零基礎學習Python的資料和入門教程。
2.python 3
我的版本是 python 3.6.1
3.python的第三方庫
win32api,win32gui,win32con,Pillow,numpy,opencv
可通過 pip install --upgrade SomePackage 來進行安裝
注意:有的版本是下載pywin32,但是有的要把pywin32升級到最高並自動下載了pypiwin32,具體情況每個python版本可能都略有不同
我給出我的第三方庫和版本僅供參考

二、關鍵程式碼組成
1.找到遊戲視窗與座標

2.鎖定並抓取雷區影象

3.各影象的RGBA值

4.掃描雷區影象儲存至一個二維陣列map

5.掃雷演算法
這裡我採用的最基礎的演算法
1.首先點出一個點
2.掃描所有數字,如果周圍空白+插旗==數字,則空白均有雷,右鍵點選空白插旗
3.掃描所有數字,如果周圍插旗==數字,則空白均沒有雷,左鍵點選空白
4.迴圈2、3,如果沒有符合條件的,則隨機點選一個白塊
插旗

點選白塊

隨機點選
