自动扫雷一般分为两种,一种是读取内存数据,而另一种是通过分析图片获得数据,并通过模拟鼠标操作,这里我用的是第二种方式。

// 2018.8.10更新

代码已上传至GitHub

https://github.com/chestnut-egg/GoMine

一、准备工作

1.扫雷游戏

我是win10,没有默认的扫雷,所以去扫雷网下载

http://www.saolei.net/BBS/

2.python 3

我的版本是 python 3.6.1

3.python的第三方库

win32api,win32gui,win32con,Pillow,numpy,opencv
可通过 pip install --upgrade SomePackage 来进行安装
注意:有的版本是下载pywin32,但是有的要把pywin32升级到最高并自动下载了pypiwin32,具体情况每个python版本可能都略有不同

我给出我的第三方库和版本仅供参考

二、关键代码组成

1.找到游戏窗口与坐标

#扫雷游戏窗口
class_name = "TMain"
title_name = "Minesweeper Arbiter "
hwnd = win32gui.FindWindow(class_name, title_name) #窗口坐标
left =
top =
right =
bottom = if hwnd:
print("找到窗口")
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
#win32gui.SetForegroundWindow(hwnd)
print("窗口坐标:")
print(str(left)+' '+str(right)+' '+str(top)+' '+str(bottom))
else:
print("未找到窗口")

2.锁定并抓取雷区图像

#锁定雷区坐标
#去除周围功能按钮以及多余的界面
#具体的像素值是通过QQ的截图来判断的
left +=
top +=
right -=
bottom -= #抓取雷区图像
rect = (left, top, right, bottom)
img = ImageGrab.grab().crop(rect)

3.各图像的RGBA值

#数字1-8 周围雷数
#0 未被打开
#ed 被打开 空白
#hongqi 红旗
#boom 普通雷
#boom_red 踩中的雷
rgba_ed = [(, (, , )), (, (, , ))]
rgba_hongqi = [(, (, , )), (, (, , )), (, (, , )), (, (, , )), (, (, , ))]
rgba_0 = [(, (, , )), (, (, , )), (, (, , ))]
rgba_1 = [(, (, , )), (, (, , )), (, (, , ))]
rgba_2 = [(, (, , )), (, (, , )), (, (, , ))]
rgba_3 = [(, (, , )), (, (, , )), (, (, , ))]
rgba_4 = [(, (, , )), (, (, , )), (, (, , ))]
rgba_5 = [(, (, , )), (, (, , )), (, (, , ))]
rgba_6 = [(, (, , )), (, (, , )), (, (, , ))]
rgba_8 = [(, (, , )), (, (, , ))]
rgba_boom = [(, (, , )), (, (, , )), (, (, , )), (, (, , ))]
rgba_boom_red = [(, (, , )), (, (, , )), (, (, , )), (, (, , ))]

4.扫描雷区图像保存至一个二维数组map

#扫描雷区图像
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 + ) * block_width, (y + ) * block_height))
if this_image.getcolors() == rgba_0:
map[y][x] =
elif this_image.getcolors() == rgba_1:
map[y][x] =
elif this_image.getcolors() == rgba_2:
map[y][x] =
elif this_image.getcolors() == rgba_3:
map[y][x] =
elif this_image.getcolors() == rgba_4:
map[y][x] =
elif this_image.getcolors() == rgba_5:
map[y][x] =
elif this_image.getcolors() == rgba_6:
map[y][x] =
elif this_image.getcolors() == rgba_8:
map[y][x] =
elif this_image.getcolors() == rgba_ed:
map[y][x] = -
elif this_image.getcolors() == rgba_hongqi:
map[y][x] = -
elif this_image.getcolors() == rgba_boom or this_image.getcolors() == rgba_boom_red:
global gameover
gameover =
break
#sys.exit()
else:
print("无法识别图像")
print("坐标")
print((y,x))
print("颜色")
print(this_image.getcolors())
sys.exit()
#print(map)

5.扫雷算法

这里我采用的最基础的算法

1.首先点出一个点

2.扫描所有数字,如果周围空白+插旗==数字,则空白均有雷,右键点击空白插旗

3.扫描所有数字,如果周围插旗==数字,则空白均没有雷,左键点击空白

4.循环2、3,如果没有符合条件的,则随机点击一个白块

#插旗
def banner():
showmap()
for y in range(blocks_y):
for x in range(blocks_x):
if <= map[y][x] and map[y][x] <= :
boom_number = map[y][x]
block_white =
block_qi =
for yy in range(y-,y+):
for xx in range(x-,x+):
if <= yy and <= xx and yy < blocks_y and xx < blocks_x:
if not (yy == y and xx == x):if map[yy][xx] == :
block_white +=
elif map[yy][xx] == -:
block_qi += if boom_number == block_white + block_qi:for yy in range(y - , y + ):
for xx in range(x - , x + ):
if <= yy and <= xx and yy < blocks_y and xx < blocks_x:
if not (yy == y and xx == x):
if map[yy][xx] == :
win32api.SetCursorPos([left+xx*block_width, top+yy*block_height])
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, , , , )
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, , , , )
showmap() #点击白块
def dig():
showmap()
iscluck =
for y in range(blocks_y):
for x in range(blocks_x):
if <= map[y][x] and map[y][x] <= :
boom_number = map[y][x]
block_white =
block_qi =
for yy in range(y - , y + ):
for xx in range(x - , x + ):
if <= yy and <= xx and yy < blocks_y and xx < blocks_x:
if not (yy == y and xx == x):
if map[yy][xx] == :
block_white +=
elif map[yy][xx] == -:
block_qi += if boom_number == block_qi and block_white > :for yy in range(y - , y + ):
for xx in range(x - , x + ):
if <= yy and <= xx and yy < blocks_y and xx < blocks_x:
if not(yy == y and xx == x):
if map[yy][xx] == :
win32api.SetCursorPos([left + xx * block_width, top + yy * block_height])
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, , , , )
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, , , , )
iscluck =
if iscluck == :
luck() #随机点击
def luck():
fl =
while(fl):
random_x = random.randint(, blocks_x - )
random_y = random.randint(, blocks_y - )
if(map[random_y][random_x] == ):
win32api.SetCursorPos([left + random_x * block_width, top + random_y * block_height])
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, , , , )
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, , , , )
fl =
def gogo():
win32api.SetCursorPos([left, top])
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
showmap()
global gameover
while(1):
if(gameover == 0):
banner()
banner()
dig()
else:
gameover = 0
win32api.keybd_event(113, 0, 0, 0)
win32api.SetCursorPos([left, top])
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
showmap()
 

这个算法在初级和中级通过率都不错,但是在高级成功率惨不忍睹,主要是没有考虑逻辑组合以及白块是雷的概率问题,可以对这两个点进行改进,提高成功率

自动扫雷 python的更多相关文章

  1. 利用Python实现自动扫雷

    自动扫雷一般分为两种,一种是读取内存数据,而另一种是通过分析图片获得数据,并通过模拟鼠标操作,这里我用的是第二种方式. 一.准备工作 我的版本是 python 3.6.1python的第三方库:win ...

  2. 1秒内通关扫雷?他创造属于自己的世界记录!Python实现自动扫雷

    五一劳动节假期,我们一起来玩扫雷吧.用Python+OpenCV实现了自动扫雷,突破世界记录,我们先来看一下效果吧. 中级 - 0.74秒 3BV/S=60.81 相信许多人很早就知道有扫雷这么一款经 ...

  3. [Dynamic Language] 用Sphinx自动生成python代码注释文档

    用Sphinx自动生成python代码注释文档 pip install -U sphinx 安装好了之后,对Python代码的文档,一般使用sphinx-apidoc来自动生成:查看帮助mac-abe ...

  4. 如何使用python来模拟鼠标点击(将通过实例自动化模拟在360浏览器中自动搜索"python")

    一.准备工作: 安装pywin32,后面开发需要pywin32的支持,否则无法完成与windows层面相关的操作. pywin32的具体安装及注意事项: 1.整体开发环境: 基于windows7操作系 ...

  5. PyCharm配置autopep8,自动格式化Python代码

    1. 关于PEP 8 PEP 8,Style Guide for Python Code,是Python官方推出编码约定,主要是为了保证 Python 编码的风格一致,提高代码的可读性. 官网地址:h ...

  6. nginx tomcat 自动部署python脚本【转】

    #!/usr/bin/env python #--coding:utf8-- import sys,subprocess,os,datetime,paramiko,re local_path='/ho ...

  7. 文件参数化-utp框架之根据yaml文件自动生成python文件+utp运行用例

    根据yaml文件自动生成python文件 utp框架: bin目录:存放执行文件(run.py) cases目录:存放生成的用例的python文件(该目录下的文件为根据data目录下的测试用例生成的p ...

  8. SAE部署Python-让云端自动运行Python代码

    之前写过模拟登录新浪微博的帖子,然而我并没有去爬过微博的数据,觉得有点浪费,于是就想写一个代码来发微博.写完之后觉得如果能自动发微博就好了,但是我又不可能24小时开始(晚上12点后还会断网),也没有v ...

  9. windows 10 如何设定计划任务自动执行 python 脚本?

    我用 python 写了一些脚本,有一些是爬虫脚本,比如爬取知乎特定话题的热门问题,有一些是定期的统计分析脚本,输出统计结果到文档中.之前我都是手动执行这些脚本,现在我希望如何这些脚本能自动定时执行. ...

随机推荐

  1. [PHP]$_SERVER参数详情

    来源:PHP中$_SERVER的详细参数与说明 $_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document root相关.$_SERVER['argv'] #传递给该脚 ...

  2. Springboot:logback日志管理(九)

    Springboot默认使用的日志框架就是logback 创建自定义的logback-spring.xml放在resources类目录下即可 logback-spring.xml: <?xml ...

  3. Python flask 构建可扩展的restful ap

    Flask-RESTful是flask的扩展,增加了对快速构建REST API的支持. Flask-RESTful通过最少的设置鼓励最佳的实践. pip install flask-restfulFl ...

  4. 2019-2020-1 20199328《Linux内核原理与分析》第三周作业

    加载内核 这里可以看出有些东西隔一段时间就会打印出来 查看mymain.c 开头的一些语句不再描述 每10000次循环打印一次 这里还是针对的mymain.c文件,这里我们可以根据自己的计算机对频率进 ...

  5. visual stdio 2012快捷键

    为什么80%的码农都做不了架构师?>>>   VS2012变化的快捷键:注释::VS2010是(Ctrl+E,C),VS2012是(Ctrl+K, Ctrl+C),实际操作,按住Ct ...

  6. DM 源码阅读系列文章(六)relay log 的实现

    2019独角兽企业重金招聘Python工程师标准>>> 作者:张学程 本文为 DM 源码阅读系列文章的第六篇,在 上篇文章 中我们介绍了 binlog replication 处理单 ...

  7. Cobbler自动装机试验

    Cobbler自动装机简介:Cobbler是一个使用Python开发的开源项目,通过将部署系统所涉及的所有服务集中在一起,来提供一个全自动的批量快速建立Linux系统的网络安装环境.Cobbler提供 ...

  8. 怎么查看当前的git分支是基于哪个分支创建的?

    2019独角兽企业重金招聘Python工程师标准>>> Question: 比如从 branch A 切出一个 branch B 然后对branch B做了一系列的操作 然后忘记了b ...

  9. jenkins 安装成功之后配置

    一.基础配置介绍 jenkins安装成功之后如果某些东西配置的话,可能部署的时候一堆的问题,所以单独总结一下,让大家少走弯路 二.准备工作 1.需要准备一台服务器,大家可以在网上买,个人学习的话还是建 ...

  10. Java——Java是什么一门什么语言

    解释型语言 源代码不能直接翻译成机器语言,而是先翻译成中间代码,再由解释器对中间代码进行解释运行: 程序不需要编译,程序在运行时才翻译成机器语言,每执行一次都要翻译一次: 解释性语言代表:Python ...