优化映射算法,添加进度存取功能

master-class
MrB 11 months ago
parent 055a2c4521
commit cda2ac2efb

@ -4,10 +4,10 @@
Version:1.0
Description:None
"""
import pickle
import random
from PySide6.QtCore import QTimer
from PySide6.QtCore import QTimer, Qt
from PySide6.QtWidgets import *
from PySide6.QtGui import QIcon
from win.win import Ui_Form as Form1
@ -39,13 +39,20 @@ class Form(Form1):
self.setupUi(self)
self.total_seconds = 0
# self.test()
self.konami_code = [65, 66, 65, 66]
# self.lll =[]
self.konami_index = 0
self.pushButton.clicked.connect(self.B_back)
self.B_S.clicked.connect(self.translate)
self.B_S.clicked.connect(self.B_S_fun)
self.B_save.clicked.connect(self.B_save_fun)
self.B_read.clicked.connect(self.B_read_fun)
self.timer = QTimer()
self.timer.setInterval(1000)
self.timer.timeout.connect(self.update_time)
self.init_bt()
def init_bt(self):
self.B0_0.clicked.connect(self.put_num)
self.B0_1.clicked.connect(self.put_num)
self.B0_2.clicked.connect(self.put_num)
@ -128,6 +135,24 @@ class Form(Form1):
self.B8_7.clicked.connect(self.put_num)
self.B8_8.clicked.connect(self.put_num)
# 重写窗口按键事件
def keyPressEvent(self, event):
global sudo
key = event.key()
if key == self.konami_code[self.konami_index]:
self.konami_index += 1
if self.konami_index == len(self.konami_code):
sudo.print_sudo()
self.konami_index = 0
else:
self.konami_index = 0
# self.lll.append(key)
# print(self.lll)
def update_time(self):
self.total_seconds += 1
@ -138,35 +163,79 @@ class Form(Form1):
time_str = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
self.lcdNumber.display(time_str)
def B_save_fun(self):
global sudo,cnt,del_cnt
# 保存已填处,第一位用于记录cnt,第二位记录delcnt第三位记录time
answer_list = [cnt,del_cnt, self.total_seconds]
with open('as.pickle', 'wb') as f:
# 使用pickle的dump函数将对象写入文件
pickle.dump(answer_list, f)
with open('sudo.pickle', 'wb') as f:
# 使用pickle的dump函数将对象写入文件
pickle.dump(sudo, f)
print(f"已保存{answer_list[0]}个有效答案")
def B_read_fun(self):
global sudo, cnt, subject_with_answer,del_cnt
answer_list = None
with open('as.pickle', 'rb') as f:
# 使用pickle的load函数从文件中读取对象
answer_list = pickle.load(f)
with open('sudo.pickle', 'rb') as f:
# 使用pickle的load函数从文件中读取对象
sudo = pickle.load(f)
# 配置计数器和计时器
cnt = answer_list[0]
del_cnt = answer_list[1]
print(cnt)
stack.clear()
self.total_seconds = answer_list[2]
self.timer.start()
# 映射题目并配置答案进度
self.translate(sudo.subject)
subject_with_answer = sudo.subject_with_answer
i = 1
self.translate(subject_with_answer,1)
def B_back(self):
global stack, cnt
global stack, cnt, subject_with_answer
try:
top = stack.pop()
info = top.split(",", 1)
b = self.findChild(QPushButton, info[0])
if info[1] == ' ':
info[1] = 0
b.setText(' ')
cnt = cnt - 1
else:
b.setText(info[1])
subject[info[0][1], info[0][3]] = int(info[1])
subject_with_answer[int(info[0][1]), int(info[0][3])] = int(info[1])
except Exception:
print("Stack Empty")
except Exception as e:
print(e)
def check_sudo(self):
global cnt, del_cnt, subject
global cnt, del_cnt, subject_with_answer
f = 1
for row_index in range(9):
for col_index in range(9):
num = subject[row_index, col_index]
num = subject_with_answer[row_index, col_index]
# 获取该格子对应的行、列、九宫格
sudo_row = sd.get_row(subject, row_index)
sudo_col = sd.get_col(subject, col_index)
sudo_block = sd.get_block(subject, row_index, col_index)
sudo_row = sd.get_row(subject_with_answer, row_index)
sudo_col = sd.get_col(subject_with_answer, col_index)
sudo_block = sd.get_block(subject_with_answer, row_index, col_index)
# print(num)
# print(sudo_row)
# print(sudo_col)
@ -191,7 +260,7 @@ class Form(Form1):
QMessageBox.information(self, '提示', '失败')
def put_num(self):
global cnt, del_cnt, subject, stack
global cnt, del_cnt, subject_with_answer, stack
b = self.sender()
x = int(b.objectName()[1])
@ -202,41 +271,41 @@ class Form(Form1):
cnt = cnt + 1
stack.append(f'B{x}_{y},{old_text}')
print(stack)
# print(stack)
if self.R_1.isChecked():
b.setText("1")
subject[x, y] = 1
subject_with_answer[x, y] = 1
if self.R_2.isChecked():
b.setText("2")
subject[x, y] = 2
subject_with_answer[x, y] = 2
if self.R_3.isChecked():
b.setText("3")
subject[x, y] = 3
subject_with_answer[x, y] = 3
if self.R_4.isChecked():
b.setText("4")
subject[x, y] = 4
subject_with_answer[x, y] = 4
if self.R_5.isChecked():
b.setText("5")
subject[x, y] = 5
subject_with_answer[x, y] = 5
if self.R_6.isChecked():
b.setText("6")
subject[x, y] = 6
subject_with_answer[x, y] = 6
if self.R_7.isChecked():
b.setText("7")
subject[x, y] = 7
subject_with_answer[x, y] = 7
if self.R_8.isChecked():
b.setText("8")
subject[x, y] = 8
subject_with_answer[x, y] = 8
if self.R_9.isChecked():
b.setText("9")
subject[x, y] = 9
subject_with_answer[x, y] = 9
if cnt == del_cnt:
self.check_sudo()
def translate(self):
global cnt, del_cnt, subject, stack
def B_S_fun(self):
global cnt, del_cnt, stack, sudo, subject_with_answer
# 清零
cnt = 0
@ -252,14 +321,18 @@ class Form(Form1):
if self.R_h.isChecked():
level = 3
# 这里可以用多线程
sudo = sd.Sudo()
subject = sudo.createSubjectByLevel(level)
# 打印参考答案
subject_with_answer = sudo.createSubjectByLevel(level).subject_with_answer
del_cnt = sudo.get_del_cnt()
# 打印参考答案
sudo.print_sudo()
# print(sudo)
self.translate(sudo.subject)
def translate(self, nparray, mode=0):
# 将np数组内容映射到按钮上
for row_index, row in enumerate(subject):
for row_index, row in enumerate(nparray):
row_list = list(map(str, row.tolist()))
row_list = replace_element(row_list, '0', ' ')
for single_index, single in enumerate(row_list):
@ -268,16 +341,19 @@ class Form(Form1):
# 将值映射到按钮上
b.setText(single)
# 禁用空值按钮
if single != ' ':
b.setDisabled(True)
else:
b.setDisabled(False)
if mode == 0:
if single != ' ':
b.setDisabled(True)
else:
b.setDisabled(False)
if __name__ == '__main__':
cnt = 0
del_cnt = 0
stack = []
subject_with_answer = None
sudo = None
app = QApplication()
win = Form()
win.setWindowIcon(QIcon('src/sudo32.png'))

@ -5,30 +5,27 @@ import numpy as np
class Sudo:
def __init__(self):
self.sudo = np.zeros((9, 9), dtype=int)
self.base_sudo = np.zeros((9, 9), dtype=int)
self.base_sudo_list = []
self.base_sudo_list_index = 0
self.base_sudo_list_length = 0
self.base_sudo_list_index_list = []
self.base_sudo_list_index_list_length = 0
self.subject = None
self.subject_with_answer = None
self.del_cnt = 0
def get_del_cnt(self):
return self.del_cnt
def createSubjectByLevel(self, level):
max_clear_count = 64
min_clear_count = 14
each_level_count = (max_clear_count - min_clear_count) / 5
each_level_count = (max_clear_count - min_clear_count) // 3
# 难度对应删除数
level_start = min_clear_count + (level - 1) * each_level_count
self.del_cnt = random.randrange(level_start, level_start + each_level_count)
self.create_base_sudo()
self.random_sudo(50)
self.get_sudo_subject(self.del_cnt)
return self.subject
self.__random_sudo(50)
self.__get_sudo_subject(self.del_cnt)
self.subject_with_answer = self.subject.copy()
return self
def create_base_sudo(self):
# 9*9的二维矩阵每个格子默认值为0
@ -54,7 +51,6 @@ class Sudo:
num = num % 9 + 1
return self.sudo
def get_row(self, row):
return self.sudo[row, :]
@ -76,7 +72,7 @@ class Sudo:
row_str = ' '.join(row)
print(row_str.replace('0', ' '))
def random_sudo(self, times):
def __random_sudo(self, times):
for _ in range(times):
# 随机交换两行
rand_row_base = random.randrange(3) * 3 # 从036 随机取一个
@ -92,7 +88,7 @@ class Sudo:
col_2 = rand_col_base + rand_cols[1]
self.sudo[:, [col_1, col_2]] = self.sudo[:, [col_2, col_1]]
def get_sudo_subject(self, del_nums):
def __get_sudo_subject(self, del_nums):
self.subject = self.sudo.copy()
# 随机擦除从0到80随机取要删除的个数
@ -121,72 +117,3 @@ def get_block(sudo, row, col):
row_start = row // 3 * 3
col_start = col // 3 * 3
return sudo[row_start: row_start + 3, col_start: col_start + 3]
#
#
# # 打印数独
# def print_sudo(sudo):
# for row_index, row in enumerate(sudo):
# if row_index % 3 == 0 and row_index != 0:
# print('-' * (9 + 8 + 4))
# row = list(map(str, row.tolist()))
# row.insert(6, '|')
# row.insert(3, '|')
# row_str = ' '.join(row)
# print(row_str.replace('0', ' '))
#
# def create_base_sudo():
# # 9*9的二维矩阵每个格子默认值为0
# sudo = np.zeros((9, 9), dtype=int)
# # 随机生成起始的基数(1 ~ 9)
# num = random.randrange(9) + 1
#
# # 遍历从左到右,从上到下逐个遍历
# for row_index in range(9):
# for col_index in range(9):
# # 获取该格子对应的行、列、九宫格
# sudo_row = get_row(sudo, row_index)
# sudo_col = get_col(sudo, col_index)
# sudo_block = get_block(sudo, row_index, col_index)
#
# # 如果该数字已经存在于对应的行、列、九宫格
# # 则继续判断下一个候选数字,直到没有重复
# while num in sudo_row or num in sudo_col or num in sudo_block:
# num = num % 9 + 1
#
# # 赋值
# sudo[row_index, col_index] = num
# num = num % 9 + 1
# return sudo
#
#
# def random_sudo(sudo, times):
# for _ in range(times):
# # 随机交换两行
# rand_row_base = random.randrange(3) * 3 # 从036 随机取一个
# rand_rows = random.sample(range(3), 2) # 从 012中随机取两个数
# row_1 = rand_row_base + rand_rows[0]
# row_2 = rand_row_base + rand_rows[1]
# sudo[[row_1, row_2], :] = sudo[[row_2, row_1], :]
#
# # 随机交换两列
# rand_col_base = random.randrange(3) * 3
# rand_cols = random.sample(range(3), 2)
# col_1 = rand_col_base + rand_cols[0]
# col_2 = rand_col_base + rand_cols[1]
# sudo[:, [col_1, col_2]] = sudo[:, [col_2, col_1]]
#
#
# def get_sudo_subject(sudo, del_nums):
# subject = sudo.copy()
#
# # 随机擦除从0到80随机取要删除的个数
# clears = random.sample(range(81), del_nums)
# for clear_index in clears:
# # 把0到80的坐标转化成行和列索引
# # 这样就不会重复删除同一个格子的数字
# row_index = clear_index // 9
# col_index = clear_index % 9
# subject[row_index, col_index] = 0
# return subject
#

@ -3,7 +3,7 @@
################################################################################
## Form generated from reading UI file 'win.ui'
##
## Created by: Qt User Interface Compiler version 6.6.0
## Created by: Qt User Interface Compiler version 6.6.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
@ -30,7 +30,7 @@ class Ui_Form(QWidget):
self.horizontalLayout.setObjectName(u"horizontalLayout")
self.gridLayout_3 = QGridLayout()
self.gridLayout_3.setObjectName(u"gridLayout_3")
self.verticalSpacer = QSpacerItem(20, 10, QSizePolicy.Minimum, QSizePolicy.Fixed)
self.verticalSpacer = QSpacerItem(20, 10, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Fixed)
self.gridLayout_3.addItem(self.verticalSpacer, 3, 2, 1, 1)
@ -349,7 +349,7 @@ class Ui_Form(QWidget):
self.gridLayout_3.addLayout(self.gridLayout, 0, 0, 1, 1)
self.verticalSpacer_2 = QSpacerItem(20, 10, QSizePolicy.Minimum, QSizePolicy.Fixed)
self.verticalSpacer_2 = QSpacerItem(20, 10, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Fixed)
self.gridLayout_3.addItem(self.verticalSpacer_2, 1, 2, 1, 1)
@ -873,7 +873,7 @@ class Ui_Form(QWidget):
self.gridLayout_3.addLayout(self.gridLayout_12, 4, 4, 1, 1)
self.horizontalSpacer = QSpacerItem(10, 20, QSizePolicy.Fixed, QSizePolicy.Minimum)
self.horizontalSpacer = QSpacerItem(10, 20, QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Minimum)
self.gridLayout_3.addItem(self.horizontalSpacer, 2, 3, 1, 1)
@ -981,7 +981,7 @@ class Ui_Form(QWidget):
self.gridLayout_3.addLayout(self.gridLayout_6, 0, 4, 1, 1)
self.horizontalSpacer_2 = QSpacerItem(10, 20, QSizePolicy.Fixed, QSizePolicy.Minimum)
self.horizontalSpacer_2 = QSpacerItem(10, 20, QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Minimum)
self.gridLayout_3.addItem(self.horizontalSpacer_2, 2, 1, 1, 1)
@ -1025,7 +1025,7 @@ class Ui_Form(QWidget):
self.verticalLayout.addLayout(self.verticalLayout_2)
self.verticalSpacer_4 = QSpacerItem(20, 18, QSizePolicy.Minimum, QSizePolicy.Fixed)
self.verticalSpacer_4 = QSpacerItem(20, 18, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Fixed)
self.verticalLayout.addItem(self.verticalSpacer_4)
@ -1041,7 +1041,19 @@ class Ui_Form(QWidget):
self.verticalLayout.addWidget(self.B_S)
self.verticalSpacer_3 = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
self.B_save = QPushButton(Form)
self.B_save.setObjectName(u"B_save")
self.B_save.setMinimumSize(QSize(0, 30))
self.verticalLayout.addWidget(self.B_save)
self.B_read = QPushButton(Form)
self.B_read.setObjectName(u"B_read")
self.B_read.setMinimumSize(QSize(0, 30))
self.verticalLayout.addWidget(self.B_read)
self.verticalSpacer_3 = QSpacerItem(20, 40, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding)
self.verticalLayout.addItem(self.verticalSpacer_3)
@ -1246,6 +1258,8 @@ class Ui_Form(QWidget):
self.R_m.setText(QCoreApplication.translate("Form", u"\u4e2d\u7ea7", None))
self.R_h.setText(QCoreApplication.translate("Form", u"\u9ad8\u7ea7", None))
self.B_S.setText(QCoreApplication.translate("Form", u"\u5f00\u59cb", None))
self.B_save.setText(QCoreApplication.translate("Form", u"\u4fdd\u5b58\u8fdb\u5ea6", None))
self.B_read.setText(QCoreApplication.translate("Form", u"\u8bfb\u53d6\u8fdb\u5ea6", None))
self.label_2.setText(QCoreApplication.translate("Form", u"\u9009\u62e9\u586b\u5165\u6570\u5b57", None))
self.R_1.setText(QCoreApplication.translate("Form", u"1", None))
self.R_2.setText(QCoreApplication.translate("Form", u"2", None))

@ -3388,6 +3388,32 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="B_save">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="text">
<string>保存进度</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="B_read">
<property name="minimumSize">
<size>
<width>0</width>
<height>30</height>
</size>
</property>
<property name="text">
<string>读取进度</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_3">
<property name="orientation">
@ -3626,7 +3652,7 @@
<resources/>
<connections/>
<buttongroups>
<buttongroup name="buttonGroup"/>
<buttongroup name="buttonGroup_2"/>
<buttongroup name="buttonGroup"/>
</buttongroups>
</ui>

Loading…
Cancel
Save