Skip to content

Commit 0eda7f9

Browse files
committed
feat: em...
1 parent bde1a0e commit 0eda7f9

3 files changed

Lines changed: 33 additions & 134 deletions

File tree

src/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import sys
44
import mainWindowGUI as mainWindowGUI
55
import mineSweeperGUI as mineSweeperGUI
6+
from win32gui import EnumWindows, GetWindowText, FindWindow
7+
import ctypes
68

79

810
if __name__ == "__main__":
@@ -11,6 +13,14 @@
1113
ui = mineSweeperGUI.MineSweeperGUI(mainWindow, sys.argv)
1214
ui.mainWindow.show()
1315
ui.mainWindow.game_setting_path = ui.game_setting_path
16+
17+
18+
SetWindowDisplayAffinity = ctypes.windll.user32.SetWindowDisplayAffinity
19+
SetWindowDisplayAffinity.argtypes = ctypes.wintypes.HWND, ctypes.wintypes.DWORD
20+
SetWindowDisplayAffinity.restype = ctypes.wintypes.BOOL
21+
ui.hwnd = FindWindow(None, "元扫雷")
22+
23+
1424
sys.exit(app.exec_())
1525
...
1626

src/mineSweeperGUI.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
# from PyQt5.QtWidgets import QApplication
1717
from country_name import country_name
1818
import metaminesweeper_checksum
19+
from win32gui import EnumWindows, GetWindowText, FindWindow
20+
import ctypes
1921

2022
class MineSweeperGUI(superGUI.Ui_MainWindow):
2123
def __init__(self, MainWindow, args):
@@ -87,7 +89,7 @@ def __init__(self, MainWindow, args):
8789
# 'joking':正在游戏状态,游戏中看过概率计算结果,游戏结果不是official的。
8890
# 'fail':游戏失败,踩雷了。
8991
# 'win':游戏成功。
90-
92+
9193

9294

9395
# 相对路径
@@ -107,6 +109,8 @@ def layMine(self, i, j):
107109
xx = self.row
108110
yy = self.column
109111
num = self.mineNum
112+
# 0,4, 5, 6, 7, 8, 9, 10代表:标准0、win74、竞速无猜5、强无猜6、
113+
# 弱无猜7、准无猜8、强可猜9、弱可猜10
110114
if self.gameMode == 5 or self.gameMode == 6 or self.gameMode == 9:
111115
# 根据模式生成局面
112116
Board, _ = mm.laymine_solvable(self.board_constraint,
@@ -143,8 +147,6 @@ def ai(self, i, j):
143147
# 0,4, 5, 6, 7, 8, 9, 10代表:标准、win7、
144148
# 竞速无猜、强无猜、弱无猜、准无猜、强可猜、弱可猜
145149
# 根据模式处理一次点击的全部流程
146-
# 返回的最后一个值是一个flag,无论是不是雷,都代表是否失败,True为失败
147-
# 该函数维护boardofGame,具体为标雷
148150
# (i,j)一定是未打开状态
149151
if self.gameMode == 0 or self.gameMode == 4 or self.gameMode == 5:
150152
return
@@ -175,6 +177,7 @@ def ai(self, i, j):
175177
return
176178
elif self.gameMode == 9 or self.gameMode == 10:
177179
if self.label.ms_board.board[i][j] == -1:
180+
# 可猜调整的核心逻辑
178181
board, flag = mm.enumerateChangeBoard(self.label.ms_board.board,
179182
self.label.ms_board.game_board, i, j)
180183
self.label.ms_board.board = board
@@ -203,6 +206,15 @@ def mineAreaLeftRelease(self, i, j):
203206
# 正式埋雷开始
204207
self.layMine(i // self.pixSize, j // self.pixSize)
205208
self.game_state = 'playing'
209+
210+
self.SetWindowDisplayAffinity = ctypes.windll.user32.SetWindowDisplayAffinity
211+
self.SetWindowDisplayAffinity.argtypes = ctypes.wintypes.HWND, ctypes.wintypes.DWORD
212+
self.SetWindowDisplayAffinity.restype = ctypes.wintypes.BOOL
213+
if not self.SetWindowDisplayAffinity(self.hwnd, 0x00000011):
214+
raise ctypes.WinError()
215+
# print('failed', ctypes.get_last_error())
216+
# raise ctypes.WinError()
217+
206218
# 核实用的时间,防变速齿轮
207219
self.start_time_unix_2 = QtCore.QDateTime.currentDateTime().\
208220
toMSecsSinceEpoch()
@@ -453,6 +465,8 @@ def gameRestart(self, e = None): # 画界面,但是不埋雷,改数据而
453465

454466

455467
def gameFinished(self): # 游戏结束画残局,改状态
468+
if not self.SetWindowDisplayAffinity(self.hwnd, 0x00000000):
469+
raise ctypes.WinError()
456470
if self.label.ms_board.game_board_state == 3 and self.end_then_flag:
457471
self.label.ms_board.win_then_flag_all_mine()
458472
elif self.label.ms_board.game_board_state == 4:
@@ -491,6 +505,7 @@ def checksum_module_ok(self):
491505

492506
def save_evf_file(self):
493507
# 搜集本局各种信息,存成evf文件
508+
# 调试的时候不会自动存录像,见checksum_module_ok
494509
self.label.ms_board.is_fair = self.is_fair()
495510
self.label.ms_board.is_offical = self.is_official()
496511
# if self.label.ms_board.is_fair and self.label.ms_board.is_offical:

src/minesweeper_master.py

Lines changed: 5 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# author : Wang Jianing(18201)
22
from random import shuffle, choice
33
# from random import randint, seed, sample
4-
# from itertools import combinations
4+
from typing import List
55
# import time
66
from safe_eval import safe_eval
77
import configparser
@@ -11,7 +11,8 @@
1111

1212
# OutputEnable = 0
1313
# seedNum = 60223
14-
EnuLimit = 40
14+
# EnuLimit = 40
15+
EnuLimit = 4
1516

1617
def choose_3BV(board_constraint, attempt_times_limit, params):
1718
# def choose_3BV_laymine(laymine):
@@ -175,81 +176,7 @@ def refreshBoard2(Board , BoardofGame, ClickedPoses):
175176
ClickedPoses.append((m,n))
176177
return BoardofGame
177178

178-
# def SolveDirect(MatrixA, Matrixx, Matrixb, BoardofGame):
179-
# # 考虑只一个方程判雷,比如3个方格,雷数也是正好是3,等等
180-
# # 返回MatrixA, Matrixx, Matrixb, BoardofGame, NotMine, flag
181-
# # flag=1表明有所动作,比如标雷或发现NotMine
182-
# # NotMine存储非雷的位置
183-
# # 注意:处理结束后的矩阵可能有重复的行(?)
184-
# flag = 0
185-
# NotMine = []
186-
# MatrixColumn = len(Matrixx)
187-
# MatrixRow = len(Matrixb)
188-
# for i in range(MatrixRow-1, -1, -1):#第一轮循环,找是雷的位置
189-
# if sum(MatrixA[i][:]) == Matrixb[i]:
190-
# flag = 1
191-
# for k in range(MatrixColumn-1, -1, -1):
192-
# if MatrixA[i][k] == 1:
193-
# m,n = Matrixx[k]
194-
# BoardofGame[m][n] = 11#在游戏局面中标雷
195-
# Matrixx.pop(k)
196-
# for t in range(0,MatrixRow):
197-
# if MatrixA[t][k] == 0:
198-
# MatrixA[t].pop(k)
199-
# else:
200-
# MatrixA[t].pop(k)
201-
# Matrixb[t] -= 1
202-
# MatrixColumn -= 1
203-
# MatrixA.pop(i)
204-
# Matrixb.pop(i)
205-
# MatrixRow -= 1
206-
# for i in range(MatrixRow-1, -1, -1):# 第二轮循环,找不是雷的位置
207-
# if Matrixb[i]==0:
208-
# flag = 1
209-
# for k in range(MatrixColumn-1, -1, -1):
210-
# if MatrixA[i][k] == 1 and Matrixx[k] not in NotMine:
211-
# NotMine.append(Matrixx[k])
212-
213-
# return BoardofGame, NotMine, flag
214-
215-
# SolveMinus = ms_toollib.py_SolveMinus
216-
# SolveMinus(MatrixA, Matrixx, Matrixb, BoardofGame)
217-
# 用减法和集合的包含关系判雷
218-
# 返回BoardofGame, NotMine, flag
219-
# 如果发现IsMine而没有发现NotMine,会再用单集合找一遍。如果发现NotMine,则不调用方法1
220-
# 因此,若方法2没有发现NotMine,则方法1也不可能发现NotMine;
221-
# 若方法2发现NotMine,则方法1还可能发现NotMine
222-
# 注意:处理结束后的矩阵可能有重复的行
223-
224-
# SolveEnumerate = ms_toollib.py_SolveEnumerate
225-
# SolveEnumerate(MatrixA, Matrixx, Matrixb, BoardofGame, enuLimit=30)
226-
#枚举法判雷
227-
228-
# calPossibility = ms_toollib.py_cal_possibility
229-
230-
# calPossibility_onboard = ms_toollib.py_cal_possibility_onboard
231-
232-
def refreshMatrixWithNotMine(MatrixA,Matrixx,Matrixb,NotMine):
233-
# 用非雷刷新三个矩阵,同时删掉全为0的行
234-
# 拟弃用。不合理。
235-
# MatrixA,Matrixx,Matrixb = refreshMatrixWithNotMine(MatrixA,Matrixx,Matrixb,NotMine)
236-
MatrixRow = len(Matrixb)
237-
# MatrixColumn = len(Matrixx)
238-
NotMineRel = []
239-
for j in NotMine:
240-
NotMineRel.append(Matrixx.index(j))
241-
NotMineRel.sort(reverse=True)
242-
for m in NotMineRel:
243-
Matrixx.pop(m)
244-
for i in range(MatrixRow-1, -1, -1):
245-
if sum(MatrixA[i][:]) == 0:
246-
MatrixA.pop(i)
247-
Matrixb.pop(i)
248-
continue
249-
for m in NotMineRel:
250-
MatrixA[i].pop(m)
251-
252-
return MatrixA, Matrixx, Matrixb
179+
253180

254181
def Victory(BoardofGame,Board):
255182
# 判断当前是否获胜
@@ -264,7 +191,7 @@ def Victory(BoardofGame,Board):
264191
return 1
265192

266193

267-
def enumerateChangeBoard(board, BoardofGame, xx, yy):
194+
def enumerateChangeBoard(board, BoardofGame, xx, yy) -> (List[List[int]], bool):
268195
# 等可能给出全部可能情况中(i,j)不为雷的随机一种情况,此时(i,j)一定是未打开状态;
269196
# 但若超过最大枚举长度,
270197
# 将返回非随机的某一种可能的情况(暂未实现),若剩余位置数少于雷数,则返回原图
@@ -518,59 +445,6 @@ def debug_ms_board(ms_board):
518445

519446

520447

521-
# layMineSolvable = ms_toollib.layMineSolvable
522-
# layMineSolvable = ms_toollib.py_layMineSolvable_thread
523-
# layMineSolvable(Row, Column, MineNum, X0, Y0, Min3BV = 0, Max3BV = 1e6,
524-
# MaxTimes = 1e6, enuLimit = 30)
525-
# 3BV下限、上限,最大尝试次数,返回是否成功。
526-
# 若不成功返回最后生成的局面(不一定无猜),默认尝试十万次
527-
# 工具箱里的这两个函数,一个是单线程,一个是多线程
528-
# 性能对比:(16, 16, 72) -> 10.49, 32.02
529-
# (16, 30, 99) -> 2.98, 2.66
530-
# layMineSolvable = debug_laymine
531-
532-
533-
# calOp = ms_toollib.py_calOp # 输入列表的局面,计算空,0的8连通域数
534-
# calOp(Board)
535-
536-
# cal3BV = ms_toollib.py_cal3BV
537-
# 计算3BV,接受列表
538-
# def cal3BV(Board):
539-
540-
# def isJudgeable(BoardofGame, EnuLimit=30):
541-
# # 返回此时是否存在可判的格子,如果还能判返回True
542-
# # 这个过程中,如果AI见到的游戏局面中有未标的雷,会标雷,但仍然可能有漏标的
543-
# # flag = 0
544-
# MatrixA, Matrixx, Matrixb = refreshMatrix(BoardofGame)
545-
# BoardofGame, NotMine, flag = SolveDirect(MatrixA, Matrixx, Matrixb, BoardofGame)
546-
# if not NotMine:
547-
# MatrixA, Matrixx, Matrixb = refreshMatrix(BoardofGame)
548-
# BoardofGame, NotMine, flag = SolveMinus(MatrixA, Matrixx, Matrixb, BoardofGame)
549-
# if not NotMine:
550-
# Matrix_as, Matrix_xs, Matrix_bs, _ = refresh_matrixs(BoardofGame)
551-
# BoardofGame, NotMine, flag = SolveEnumerate(Matrix_as, Matrix_xs, Matrix_bs, BoardofGame, EnuLimit)
552-
# if not NotMine:
553-
# return BoardofGame, False
554-
# return BoardofGame, True
555-
556-
# def xyisJudgeable(BoardofGame, x, y, EnuLimit=30):
557-
# # (x,y)是否必然安全,如果必然安全,返回True
558-
# MatrixA, Matrixx, Matrixb = refreshMatrix(BoardofGame)
559-
560-
# if (x,y) not in Matrixx: # 内部的非雷按理无法判出
561-
# return False
562-
563-
# BoardofGame, NotMine, flag = SolveDirect(MatrixA, Matrixx, Matrixb, BoardofGame)
564-
# if (x,y) not in NotMine:
565-
# MatrixA, Matrixx, Matrixb = refreshMatrix(BoardofGame)
566-
# BoardofGame, NotMine, flag = SolveMinus(MatrixA, Matrixx, Matrixb, BoardofGame)
567-
# if (x,y) not in NotMine:
568-
# Matrix_as, Matrix_xs, Matrix_bs, _, _ = refresh_matrixs(BoardofGame) # result of refresh_matrixs has len of 5
569-
# BoardofGame, NotMine, flag = SolveEnumerate(Matrix_as, Matrix_xs, Matrix_bs, BoardofGame, EnuLimit)
570-
# if (x,y) not in NotMine:
571-
# return False
572-
# return True
573-
574448
def updata_ini(file_name: str, data):
575449
conf = configparser.ConfigParser()
576450
conf.read(file_name, encoding='utf-8')

0 commit comments

Comments
 (0)