跳到正文

个人记录

自动扫雷机

ミク约 14 分钟
原文
文章信息与版本

文章信息

发布
2020年7月17日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 14 分钟

版本

  1. v1当前

退役OIer闲来无聊做了个自动扫雷机,目前为V1.0版本,将来可能更新。

运行限制: Windows7版本扫雷,高级难度,显示器分辨率1920*1080,全屏游玩。(将会随更新逐步取消以上限制)

目前此程序只能根据数雷法和最简单推理进行扫雷,但仍能在95%以上的高级局(无解局除外)中成功。

代码如下:

C++
#include<windows.h>
#include<algorithm>
#include<wingdi.h>
#include<cstdio>
using namespace std;
char type[18][32],cnt[18][32],mi[18][32],px[13],py[13];//8->雷,9->未探索
bool already[18][32],z[18][32],pp[18][32],cl[18][32];
COLORREF clr[18][32];
HDC hDC;
inline void lclick(int x,int y) {
	if (cl[x][y]) return; cl[x][y] = true; x--; y--;
	double X = 193.0 + 51.1333 * y + 25.5, Y = 128.0 + 51.1875 * x + 26;
	SetCursorPos((int)X, (int)Y);
	Sleep(60);
	mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
	Sleep(30);
	mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
	return;
}
inline int judge(int x, int y) {
	int X = 193.0 + 51.1333 * (y - 1) + 0.5, Y = 128.0 + 51.1875 * (x - 1) + 0.5;
	if (::GetPixel(hDC, (int)(193.0 + 51.1333 * (y - 1) + 25.5), (int)(128.0 + 51.1875 * (x - 1) + 26)) == clr[x][y]) return 9;
	else {
		COLORREF hd;
		hd = ::GetPixel(hDC, X + 35, Y + 17);
		if (GetRValue(hd) < 60 && GetGValue(hd) > 50 && GetBValue(hd) < 30) return 2;
		hd = ::GetPixel(hDC, X + 33, Y + 14);
		if (GetRValue(hd) > 150 && GetGValue(hd) < 40 && GetBValue(hd) < 40) return 3;
		hd = ::GetPixel(hDC, X + 33, Y + 32);
		if (GetRValue(hd) < 30 && GetGValue(hd) < 30 && GetBValue(hd) > 80) return 4;
		hd = ::GetPixel(hDC, X + 34, Y + 31);
		if (GetRValue(hd) < 150 && GetRValue(hd) > 100 && GetGValue(hd) < 30 && GetBValue(hd) < 30) return 5;
		hd = ::GetPixel(hDC, X + 37, Y + 30);
		if (GetRValue(hd) < 30 && GetGValue(hd) > 70 && GetBValue(hd) > 70) return 6;
		hd = ::GetPixel(hDC, X + 27, Y + 24);
		if (GetRValue(hd) > 30 && GetRValue(hd) < 150 && GetGValue(hd) > 30 && GetBValue(hd) > 100) return 1;
		return 0;
	}
}
inline void lable(int x, int y) {
	already[x][y] = true;
	if (type[x - 1][y] > 7) type[x - 1][y] = 8;
	if (type[x + 1][y] > 7) type[x + 1][y] = 8;
	if (type[x][y + 1] > 7) type[x][y + 1] = 8;
	if (type[x][y - 1] > 7) type[x][y - 1] = 8;
	if (type[x + 1][y + 1] > 7) type[x + 1][y + 1] = 8;
	if (type[x - 1][y + 1] > 7) type[x - 1][y + 1] = 8;
	if (type[x + 1][y - 1] > 7) type[x + 1][y - 1] = 8;
	if (type[x - 1][y - 1] > 7) type[x - 1][y - 1] = 8;
	return;
}
inline void clear(int x, int y) {
	already[x][y] = true;
	if (type[x - 1][y] == 9) lclick(x - 1, y);
	if (type[x - 1][y - 1] == 9) lclick(x - 1, y - 1);
	if (type[x - 1][y + 1] == 9) lclick(x - 1, y + 1);
	if (type[x][y - 1] == 9) lclick(x, y - 1);
	if (type[x][y + 1] == 9) lclick(x, y + 1);
	if (type[x + 1][y] == 9) lclick(x + 1, y);
	if (type[x + 1][y - 1] == 9) lclick(x + 1, y - 1);
	if (type[x + 1][y + 1] == 9) lclick(x + 1, y + 1);
	return;
}
inline void dfs(int x, int y) {
	if (z[x][y]) return; z[x][y] = true; if (type[x][y] == 8) return;
	if (type[x][y] == 9) { type[x][y] = judge(x, y); if (type[x][y] == 9) return; }
	if (x < 16) dfs(x + 1, y);
	if (x > 1) dfs(x - 1, y);
	if (y < 30) dfs(x, y + 1);
	if (y > 1) dfs(x, y - 1);
	if (x < 16 && y < 30) dfs(x + 1, y + 1);
	if (x < 16 && y>1) dfs(x + 1, y - 1);
	if (x > 1 && y < 30) dfs(x - 1, y + 1);
	if (x > 1 && y > 1) dfs(x - 1, y - 1);
}
inline bool situ1(int a, int b, int c, int d, int e) {
	if (cnt[c][d] == 1) { swap(a, c); swap(b, d); }
	if (cnt[c][d] == 1 && cnt[a][b] + e - 1 == mi[a][b]) { swap(a, c); swap(b, d); }
	if (cnt[c][d] + e - 1 != mi[c][d]) return false;
	for (int i = -1; i != 2; ++i)
		for (int j = -1; j != 2; ++j) {
			if (type[a + i][b + j] == 9 && !pp[a + i][b + j]) lclick(a + i, b + j);
			if (type[c + i][d + j] == 9 && !pp[c + i][d + j]) type[c + i][d + j] = 8;
		}
	return true;
}
inline void spe() {
	for (int i = 1; i != 17; ++i) 
		for (int j = 1; j != 31; ++j) {
			if (!type[i][j] || type[i][j] > 7||already[i][j]) continue;
			cnt[i][j] = type[i][j]; mi[i][j] = 0;
			if (type[i][j - 1] == 8) --cnt[i][j];
			if (type[i - 1][j] == 8) --cnt[i][j];
			if (type[i][j + 1] == 8) --cnt[i][j];
			if (type[i - 1][j - 1] == 8) --cnt[i][j];
			if (type[i - 1][j + 1] == 8) --cnt[i][j];
			if (type[i + 1][j - 1] == 8) --cnt[i][j];
			if (type[i + 1][j] == 8) --cnt[i][j];
			if (type[i + 1][j + 1] == 8) --cnt[i][j];
			if (type[i][j - 1] == 9) ++mi[i][j];
			if (type[i - 1][j] == 9) ++mi[i][j];
			if (type[i][j + 1] == 9) ++mi[i][j];
			if (type[i - 1][j - 1] == 9) ++mi[i][j];
			if (type[i - 1][j + 1] == 9) ++mi[i][j];
			if (type[i + 1][j - 1] == 9) ++mi[i][j];
			if (type[i + 1][j] == 9) ++mi[i][j];
			if (type[i + 1][j + 1] == 9) ++mi[i][j];
		}
	int cc = 0;
	for (int x = 1; x != 17; ++x)
		for (int y = 1; y != 31; ++y) {
			if (!type[x][y] || type[x][y] > 7 || already[x][y]) continue;
			for (int i = x; i < x + 3; ++i)
				for (int j = y; j < y + 3; ++j) {
					if (i > 16 || j > 30 || (i == x && y == j) || !type[i][j] || type[i][j] > 7 || already[i][j]) continue;
					for (int k = 1; k <= cc; ++k) pp[px[k]][py[k]] = false;
					cc = 0;
					for (int k = x - 1; k <= i + 1; ++k)
						for (int p = y - 1; p <= j + 1; ++p)
							if (type[k][p] == 9 && k - x < 2 && i - k < 2 && p - y < 2 && j - p < 2) { ++cc; px[cc] = k; py[cc] = p; pp[k][p] = true; }
					if(cnt[x][y]==1||cnt[i][j]==1) if (situ1(x, y, i, j, cc)) continue;
					//situ2(x, y, i, j);
				}
		}
}
inline void Gh() {
	memset(z, 0, sizeof(z));
	bool cter = false;
	dfs(8, 15);
	for(int i = 1; i != 17; ++i)
		for (int j = 1; j != 31; ++j) {
			if (type[i][j] == 9) cter = true;
			if (type[i][j] > 7 || !type[i][j]) continue;
			int ct = 0;
			if (type[i - 1][j] > 7) ++ct;
			if (type[i + 1][j] > 7) ++ct;
			if (type[i][j + 1] > 7) ++ct;
			if (type[i][j - 1] > 7) ++ct;
			if (type[i + 1][j + 1] > 7) ++ct;
			if (type[i - 1][j + 1] > 7) ++ct;
			if (type[i + 1][j - 1] > 7) ++ct;
			if (type[i - 1][j - 1] > 7) ++ct;
			if (ct == type[i][j]) lable(i, j);
		}
	if (!cter) { ::ReleaseDC(NULL, hDC); exit(0); }
	for (int i = 1; i != 17; ++i)
		for (int j = 1; j != 31; ++j) {
			if (already[i][j] || !type[i][j] || type[i][j] > 7) continue;
			int  ct2 = 0;
			if (type[i - 1][j] == 8) ++ct2;
			if (type[i + 1][j] == 8) ++ct2;
			if (type[i][j + 1] == 8) ++ct2;
			if (type[i][j - 1] == 8) ++ct2;
			if (type[i + 1][j + 1] == 8) ++ct2;
			if (type[i - 1][j + 1] == 8) ++ct2;
			if (type[i + 1][j - 1] == 8) ++ct2;
			if (type[i - 1][j - 1] == 8) ++ct2;
			if (ct2 == type[i][j]) clear(i, j);
		}
	spe();
}
int main()
{
	Sleep(5000);
	hDC = ::GetDC(NULL);
	for (int i = 1; i != 17; ++i)
		for (int j = 1; j != 31; ++j)
			 clr[i][j]= ::GetPixel(hDC,(int)(193.0 + 51.1333 * (j - 1) + 25.5),(int)(128.0 + 51.1875 * (i - 1) + 26));
	for (int i = 1; i != 17; ++i)
		for (int j = 1; j != 31; ++j)
			type[i][j] = 9;
	lclick(8,15);
	for(int ii=1;;++ii) {
		::ReleaseDC(NULL, hDC);
		SetCursorPos(0, 0);
		Sleep(100);
		hDC = ::GetDC(NULL);
		Gh();
	}
	return 0;
}

注:程序开始运行后,有5秒钟时间将焦点放到扫雷上。请将的鼠标放到屏幕边缘,并不再移动鼠标。由于鼠标自带颜色,会影响程序对格子类型的判断,因此程序中每隔不固定的一段时间会将鼠标移至屏幕左上角,关闭程序可能有困难(一般来说可以用Atl+Tab,Alt+F4完成)。函数lclick中Sleep的时长可根据本机配置自行变更。如果时长过长会导致程序运行缓慢,时长过短可能会导致某些本应被点开的格子没有未能被点开。

原文