基础代码

master
MrB 8 months ago
commit 45d196fbab

@ -0,0 +1,3 @@
for i in range(9):
for j in range(9):
print("self.B"+str(i)+"_"+str(j)+".clicked.connect(self.put_num)")

@ -0,0 +1,26 @@
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
import numpy as np
from PySide6.QtCore import *
from PySide6.QtGui import *
# Pyinstaller i src/sudo32.png -F main.py
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建两个按钮并设置名字
self.button1 = QPushButton("Button 1", self)
self.button1.setObjectName("button1")
self.button2 = QPushButton("Button 2", self)
self.button2.setObjectName("button2")
# 根据名字查找按钮控件并修改文本
button = self.findChild(QPushButton, "button1")
button.setText("aaa")
if __name__ == "__main__":
app = QApplication([])
window = MyWindow()
window.show()
app.exec_()

@ -0,0 +1,291 @@
"""
Name:Sudo Game
Date:2024.04.09
Version:1.0
Description:None
"""
import random
from PySide6.QtCore import QTimer
from PySide6.QtWidgets import *
from PySide6.QtGui import QIcon
from win.win import Ui_Form as Form1
import sudo as sd
def replace_element(arr, old_val, new_val):
return list(map(lambda x: new_val if x == old_val else x, arr))
def count_occurrences(lst, x, mode):
count = 0
if mode == 0:
for item in lst:
if item == x:
count += 1
else:
for row in lst:
for item in row:
if item == x:
count += 1
return count
class Form(Form1):
def __init__(self):
super().__init__()
self.setupUi(self)
self.total_seconds = 0
# self.test()
self.pushButton.clicked.connect(self.B_back)
self.B_S.clicked.connect(self.translate)
self.timer = QTimer()
self.timer.setInterval(1000)
self.timer.timeout.connect(self.update_time)
self.B0_0.clicked.connect(self.put_num)
self.B0_1.clicked.connect(self.put_num)
self.B0_2.clicked.connect(self.put_num)
self.B0_3.clicked.connect(self.put_num)
self.B0_4.clicked.connect(self.put_num)
self.B0_5.clicked.connect(self.put_num)
self.B0_6.clicked.connect(self.put_num)
self.B0_7.clicked.connect(self.put_num)
self.B0_8.clicked.connect(self.put_num)
self.B1_0.clicked.connect(self.put_num)
self.B1_1.clicked.connect(self.put_num)
self.B1_2.clicked.connect(self.put_num)
self.B1_3.clicked.connect(self.put_num)
self.B1_4.clicked.connect(self.put_num)
self.B1_5.clicked.connect(self.put_num)
self.B1_6.clicked.connect(self.put_num)
self.B1_7.clicked.connect(self.put_num)
self.B1_8.clicked.connect(self.put_num)
self.B2_0.clicked.connect(self.put_num)
self.B2_1.clicked.connect(self.put_num)
self.B2_2.clicked.connect(self.put_num)
self.B2_3.clicked.connect(self.put_num)
self.B2_4.clicked.connect(self.put_num)
self.B2_5.clicked.connect(self.put_num)
self.B2_6.clicked.connect(self.put_num)
self.B2_7.clicked.connect(self.put_num)
self.B2_8.clicked.connect(self.put_num)
self.B3_0.clicked.connect(self.put_num)
self.B3_1.clicked.connect(self.put_num)
self.B3_2.clicked.connect(self.put_num)
self.B3_3.clicked.connect(self.put_num)
self.B3_4.clicked.connect(self.put_num)
self.B3_5.clicked.connect(self.put_num)
self.B3_6.clicked.connect(self.put_num)
self.B3_7.clicked.connect(self.put_num)
self.B3_8.clicked.connect(self.put_num)
self.B4_0.clicked.connect(self.put_num)
self.B4_1.clicked.connect(self.put_num)
self.B4_2.clicked.connect(self.put_num)
self.B4_3.clicked.connect(self.put_num)
self.B4_4.clicked.connect(self.put_num)
self.B4_5.clicked.connect(self.put_num)
self.B4_6.clicked.connect(self.put_num)
self.B4_7.clicked.connect(self.put_num)
self.B4_8.clicked.connect(self.put_num)
self.B5_0.clicked.connect(self.put_num)
self.B5_1.clicked.connect(self.put_num)
self.B5_2.clicked.connect(self.put_num)
self.B5_3.clicked.connect(self.put_num)
self.B5_4.clicked.connect(self.put_num)
self.B5_5.clicked.connect(self.put_num)
self.B5_6.clicked.connect(self.put_num)
self.B5_7.clicked.connect(self.put_num)
self.B5_8.clicked.connect(self.put_num)
self.B6_0.clicked.connect(self.put_num)
self.B6_1.clicked.connect(self.put_num)
self.B6_2.clicked.connect(self.put_num)
self.B6_3.clicked.connect(self.put_num)
self.B6_4.clicked.connect(self.put_num)
self.B6_5.clicked.connect(self.put_num)
self.B6_6.clicked.connect(self.put_num)
self.B6_7.clicked.connect(self.put_num)
self.B6_8.clicked.connect(self.put_num)
self.B7_0.clicked.connect(self.put_num)
self.B7_1.clicked.connect(self.put_num)
self.B7_2.clicked.connect(self.put_num)
self.B7_3.clicked.connect(self.put_num)
self.B7_4.clicked.connect(self.put_num)
self.B7_5.clicked.connect(self.put_num)
self.B7_6.clicked.connect(self.put_num)
self.B7_7.clicked.connect(self.put_num)
self.B7_8.clicked.connect(self.put_num)
self.B8_0.clicked.connect(self.put_num)
self.B8_1.clicked.connect(self.put_num)
self.B8_2.clicked.connect(self.put_num)
self.B8_3.clicked.connect(self.put_num)
self.B8_4.clicked.connect(self.put_num)
self.B8_5.clicked.connect(self.put_num)
self.B8_6.clicked.connect(self.put_num)
self.B8_7.clicked.connect(self.put_num)
self.B8_8.clicked.connect(self.put_num)
def update_time(self):
self.total_seconds += 1
hours = self.total_seconds // 3600
minutes = (self.total_seconds % 3600) // 60
seconds = self.total_seconds % 60
time_str = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
self.lcdNumber.display(time_str)
def B_back(self):
global stack,cnt
try:
top = stack.pop()
info = top.split(",", 1)
b = self.findChild(QPushButton, info[0])
if info[1] == ' ':
b.setText(' ')
cnt = cnt - 1
else:
b.setText(info[1])
subject[info[0][1], info[0][3]] = int(info[1])
except Exception:
print("Stack Empty")
def check_sudo(self):
global cnt, del_cnt, subject
f = 1
for row_index in range(9):
for col_index in range(9):
num = subject[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)
# print(num)
# print(sudo_row)
# print(sudo_col)
# print(sudo_block)
cnt_oc1 = count_occurrences(sudo_row, num, 0)
cnt_oc2 = count_occurrences(sudo_col, num, 0)
cnt_oc3 = count_occurrences(sudo_block, num, 1)
if cnt_oc1 != 1 or cnt_oc2 != 1 or cnt_oc3 != 1:
f = 0
break
if f == 0:
break
if f == 1:
print("yes")
QMessageBox.information(self, '提示','成功')
self.timer.stop()
else:
print("no")
QMessageBox.information(self, '提示','失败')
def put_num(self):
global cnt, del_cnt, subject, stack
b = self.sender()
x = int(b.objectName()[1])
y = int(b.objectName()[3])
old_text = b.text()
if b.text() == ' ':
cnt = cnt + 1
stack.append(f'B{x}_{y},{old_text}')
print(stack)
if self.R_1.isChecked():
b.setText("1")
subject[x, y] = 1
if self.R_2.isChecked():
b.setText("2")
subject[x, y] = 2
if self.R_3.isChecked():
b.setText("3")
subject[x, y] = 3
if self.R_4.isChecked():
b.setText("4")
subject[x, y] = 4
if self.R_5.isChecked():
b.setText("5")
subject[x, y] = 5
if self.R_6.isChecked():
b.setText("6")
subject[x, y] = 6
if self.R_7.isChecked():
b.setText("7")
subject[x, y] = 7
if self.R_8.isChecked():
b.setText("8")
subject[x, y] = 8
if self.R_9.isChecked():
b.setText("9")
subject[x, y] = 9
if cnt == del_cnt:
self.check_sudo()
def translate(self):
global cnt, del_cnt, subject, stack
# 清零
cnt = 0
stack.clear()
self.total_seconds = 0
self.timer.start()
# 选择难度
if self.R_e.isChecked():
level = 1
if self.R_m.isChecked():
level = 2
if self.R_h.isChecked():
level = 3
max_clear_count = 64
min_clear_count = 14
each_level_count = (max_clear_count - min_clear_count) / 5
level_start = min_clear_count + (level - 1) * each_level_count
del_cnt = del_nums = random.randrange(level_start, level_start + each_level_count)
# 生成数独
sudo = sd.create_base_sudo()
sd.random_sudo(sudo, 50)
# 打印参考答案
sd.print_sudo(sudo)
# 出题
subject = sd.get_sudo_subject(sudo, del_nums)
for row_index, row in enumerate(subject):
row_list = list(map(str, row.tolist()))
row_list = replace_element(row_list, '0', ' ')
for single_index, single in enumerate(row_list):
b = self.findChild(QPushButton, 'B' + str(row_index) + '_' + str(single_index))
b.setText(single)
if single != ' ':
b.setDisabled(True)
else:
b.setDisabled(False)
if __name__ == '__main__':
cnt = 0
del_cnt = 0
stack = []
app = QApplication()
win = Form()
win.setWindowIcon(QIcon('src/sudo32.png'))
win.show()
app.exec()

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

@ -0,0 +1,116 @@
import random
import numpy as np
# 获取格子所在的行的全部格子
def get_row(sudo, row):
return sudo[row, :]
# 获取格子所在的列的全部格子
def get_col(sudo, col):
return sudo[:, col]
# 获取格子所在的九宫格的全部格子
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
if __name__ == '__main__':
max_clear_count = 64 # 最多清除个数
min_clear_count = 14 # 最少清除个数
# 设置难度等级1~55个等级入门、初级、熟练、精通、大神
level = 3
# 每个等级的个数
each_level_count = (max_clear_count - min_clear_count) / 5
# 该等级最小数
level_start = min_clear_count + (level - 1) * each_level_count
# 该等级范围内的随机数
del_nums = random.randrange(level_start, level_start + each_level_count)
# 生成基本盘
sudo = create_base_sudo()
# 生成终盘
random_sudo(sudo, 50)
# 获取数独题目
subject = get_sudo_subject(sudo, del_nums)
print('题目:')
print('=' * 26)
print_sudo(subject)
print('\n答案:')
print('=' * 26)
print_sudo(sudo)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save