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

// 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. CVE 2019-0708 漏洞复现+

    PART 1 参考链接:https://blog.csdn.net/qq_42184699/article/details/90754333 漏洞介绍: 当未经身份验证的攻击者使用 RDP 连接到目标 ...

  2. 关于对vue-router的优化(详尽版)

    这两天总结了关于vue-router优化的几点技法,做个笔记 在基于vue的移动端app中,通过vue-router可以便捷的进入某一路由或回退到上一路由,但是若不对vue-router做相关优化处理 ...

  3. leetcode-0617 合并二叉树

    题目地址https://leetcode-cn.com/problems/merge-two-binary-trees/ 1.递归解法 递归的话我们首先需要递归的终止条件,对于本题而言,递归的终止条件 ...

  4. Python代码覆盖率分析工具Coverage

    简介 在测试中,为了度量产品质量,代码覆盖率被作为一种测试结果的评判依据,在Python代码中用来分析代码覆盖率的工具当属Coverage.代码覆盖率是由特定的测试套件覆盖被测源代码的程度来度量,Co ...

  5. java8 流式编程

    为什么需要流式操作 集合API是Java API中最重要的部分.基本上每一个java程序都离不开集合.尽管很重要,但是现有的集合处理在很多方面都无法满足需要. 一个原因是,许多其他的语言或者类库以声明 ...

  6. 2019-2020-1 20199310《Linux内核原理与分析》第五周作业

    1.问题描述 在前面的文章中,已经了解了Linux内核源代码的目录结构,并在Oracle VM VirtualBox的Linux环境中构造一个简单的操作系统MenuOS,本文将学习系统调用的相关理论知 ...

  7. WANNACRY病毒中的加密技术分析

    WANNACRY病毒中的加密技术分析 2019/11/6 16:56:46 分析WANNACRY病毒中的加解密技术的应用.分析内容包括但不限于:对称密码技术和公钥密码技术的作用:受害者支付赎金后就会恢 ...

  8. 疫情之下微软收入猛增15%!远程办公产品Teams日活达7500万

    当地时间 2020 年 4 月 29 日,微软公布了截止 2020 年 3 月 31 日的 2020 财年第三季度财报. 这是微软首次在财报中显示新冠疫情的影响——疫情之下,远程办公.远程教育和游戏场 ...

  9. 深度解析国内首个云原生数据库POLARDB的“王者荣耀”

    随着移动互联网.电子商务的高速发展,被使用最多的企业级开源数据系统MySQL面临着巨大挑战--为迎接"双11"的高并发要提前做好分库分表;用户不断激增要将读写分离才能应对每天上亿次 ...

  10. jmeter的教学视频

    转载于:https://www.cnblogs.com/ios9/p/9769058.html