You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
292 lines
9.4 KiB
292 lines
9.4 KiB
"""
|
|
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()
|