|
|
# -*- encoding: utf-8 -*-
|
|
|
'''
|
|
|
@File : opera.py
|
|
|
@License : (C)Copyright 2018-2022
|
|
|
|
|
|
@Modify Time @Author @Version @Desciption
|
|
|
------------ ------- -------- -----------
|
|
|
2022/12/22 16:42 zart20 1.0 None
|
|
|
'''
|
|
|
|
|
|
from data import *
|
|
|
from gameui import *
|
|
|
|
|
|
def listen_add(pos, loca, **kwargs):
|
|
|
if loca != ():
|
|
|
i = loca[0]-1
|
|
|
j = loca[1]-1
|
|
|
num = num_map(pos)
|
|
|
global note_list, step, seq,form
|
|
|
if num: # 直接点击了数字按钮,就添加num
|
|
|
if martix[j][i] == 0 and not note_mark: # 空格且笔记模式为False才可以填写
|
|
|
|
|
|
if u_martix[j][i] == 11: # 如果修改了这个格子的值,删除原来的记录
|
|
|
for note in note_martix: # 如果这个空格有笔记直接覆盖清空笔记
|
|
|
if note[0] == i and note[1] == j:
|
|
|
note_martix.remove(note)
|
|
|
|
|
|
u_martix[j][i] = num # 为用户数组添加数值
|
|
|
xianse(form, martix, u_martix, loca[0], loca[1]) # 更新显示用户数组
|
|
|
seq = STEP[-1][0] + 1 # 记录顺序
|
|
|
step=[seq, loca,num, u_martix.copy(),note_martix.copy()] # 记录一步
|
|
|
|
|
|
print(step)
|
|
|
print(loca, num, "添加")
|
|
|
|
|
|
elif martix[j][i] == 0 and note_mark: # 笔记模式打开时
|
|
|
note_list.append(num)
|
|
|
note_list = list(set(note_list))
|
|
|
face = possible_num(bc, loca[0], loca[1], note_list)
|
|
|
show_face(form,face,bc,i,j)
|
|
|
print(note_list)
|
|
|
print(note_martix)
|
|
|
|
|
|
def listen_del(pos,loca):
|
|
|
if loca != ():
|
|
|
i = loca[0] - 1
|
|
|
j = loca[1] - 1
|
|
|
tag = control_map(pos) # 控制号码
|
|
|
if tag==2: # 直接点击了数字按钮,就添加num
|
|
|
if martix[j][i] == 0: # 空格才可以删除
|
|
|
u_martix[j][i] = 0
|
|
|
xianse(form, martix, u_martix, loca[0], loca[1], note_martix)
|
|
|
print(loca, tag,"删除")
|
|
|
|
|
|
for note in note_martix: # 如果这个空格有笔记直接覆盖清空笔记
|
|
|
if note[0] == i and note[1] == j:
|
|
|
note_martix.remove(note)
|
|
|
|
|
|
print(note_martix)
|
|
|
|
|
|
def listen_all_note(pos):
|
|
|
tag = control_map(pos) # 控制号码
|
|
|
if tag==4: # 直接点击了数字按钮,就添加num
|
|
|
for i in range(9):
|
|
|
for j in range(9):
|
|
|
if u_martix[j][i] == 11: # 更新可能值
|
|
|
u_martix[j][i] = 0
|
|
|
if u_martix[j][i] == 0:
|
|
|
possible = GetPossible(u_martix,j,i)
|
|
|
face = possible_num(bc,i+1,j+1,possible)
|
|
|
show_face(form, face, bc, i, j)
|
|
|
u_martix[j][i] = 11
|
|
|
note_martix.append((i,j,possible))
|
|
|
|
|
|
def listen_hint(pos,loca):
|
|
|
if loca != ():
|
|
|
i = loca[0] - 1
|
|
|
j = loca[1] - 1
|
|
|
tag = control_map(pos) # 控制号码
|
|
|
if tag == 5:
|
|
|
if u_martix[j][i] == 0:
|
|
|
possible = GetPossible(u_martix, j, i)
|
|
|
face = possible_num(bc, i + 1, j + 1, possible)
|
|
|
show_face(form,face,bc,i, j)
|
|
|
|
|
|
def listen_note(pos,loca):
|
|
|
if loca != ():
|
|
|
i = loca[0] - 1
|
|
|
j = loca[1] - 1
|
|
|
tag = control_map(pos) # 控制号码
|
|
|
global note_mark , note_list, form # 函数内修改全局变量
|
|
|
if tag == 3:
|
|
|
note_mark = not note_mark
|
|
|
print(note_mark)
|
|
|
if u_martix[j][i] == 0 and note_mark: # 判断是否在笔记模式
|
|
|
xianse(form,martix, u_martix, i + 1, j + 1, note_martix) # 显示输入
|
|
|
if u_martix[j][i] == 0 and not note_mark and len(note_list)!=0: # 判断是否不是处于笔记模式
|
|
|
u_martix[j][i] = 11
|
|
|
note_martix.append((i,j,note_list))
|
|
|
note_list = []
|
|
|
xianse(form,martix, u_martix, i + 1, j + 1, note_martix)
|
|
|
print(note_martix)
|
|
|
|
|
|
def listen_backspace(pos):
|
|
|
tag = control_map(pos) # 控制号码
|
|
|
global u_martix, note_martix, step
|
|
|
if tag==1: # 直接点击了数字按钮,撤回
|
|
|
if len(STEP) > 1:
|
|
|
STEP.pop()
|
|
|
u_martix = STEP[-1][-2].copy()
|
|
|
note_martix = STEP[-1][-1].copy()
|
|
|
step = []
|
|
|
refresh(form,martix, u_martix, note_martix)
|
|
|
else:
|
|
|
u_martix = STEP[-1][-2].copy()
|
|
|
note_martix = STEP[-1][-1].copy()
|
|
|
step = []
|
|
|
|
|
|
print(STEP, '\n')
|
|
|
|
|
|
def martix_map(pos:tuple):
|
|
|
"""martix坐标映射"""
|
|
|
x = pos[0]
|
|
|
y = pos[1]
|
|
|
i = 0
|
|
|
j = 0
|
|
|
global note_list, STEP, step
|
|
|
# (n * (bc + 3) + 50, p * (bc + 3) + 50)
|
|
|
for n in range(9):
|
|
|
for p in range(9):
|
|
|
top_x, top_y = n * (bc + 3) + 50, p * (bc + 3) + 50
|
|
|
ud_x, ud_y = (n + 1) * (bc + 3) + 50, (p + 1) * (bc + 3) + 50
|
|
|
if x < ud_x and x > top_x:
|
|
|
i = n + 1
|
|
|
if y < ud_y and y > top_y:
|
|
|
j = p + 1
|
|
|
if i >= 1 and j >= 1:
|
|
|
note_list = []
|
|
|
xianse(form, martix, u_martix, i, j, note_martix)
|
|
|
if len(step) !=0: # 添加步骤
|
|
|
STEP.append(step)
|
|
|
step = []
|
|
|
return (i, j)
|
|
|
|
|
|
def num_map(pos:tuple):
|
|
|
"""数字按键坐标映射"""
|
|
|
x = pos[0]
|
|
|
y = pos[1]
|
|
|
for i in range(9):
|
|
|
top_x, top_y = i * (bc + 3) + 50, 12 * (bc + 3) + 10
|
|
|
ud_x, ud_y = (i + 1) * (bc + 3) + 50, 13 * (bc + 3) + 10
|
|
|
if x < ud_x and x > top_x:
|
|
|
if y < ud_y and y > top_y:
|
|
|
control_number_xs(form,bc,i,WHITE,SILVER)
|
|
|
num = i + 1
|
|
|
return num
|
|
|
|
|
|
def control_map(pos:tuple):
|
|
|
"""控制栏坐标映射函数"""
|
|
|
x = pos[0]
|
|
|
y = pos[1]
|
|
|
lon = 9 * (bc + 3) + 50
|
|
|
lon = lon // 5 - 12 # 控制按键长度
|
|
|
for i in range(5):
|
|
|
top_x, top_y = (lon + 2) * i + 50, (bc + 3) * 11 + 8
|
|
|
ud_x, ud_y = (i + 1) * (lon + 2) + 50, (bc + 3) * 12 + 8
|
|
|
if x < ud_x and x > top_x:
|
|
|
if y < ud_y and y > top_y:
|
|
|
control_xs(form,bc,i)
|
|
|
num = i + 1
|
|
|
return num
|
|
|
|
|
|
def empty(u_martix):
|
|
|
count = 0 # 计数
|
|
|
for i in range(9):
|
|
|
for j in range(9):
|
|
|
if u_martix[i][j] == 0 or u_martix[i][j] == 11:
|
|
|
count += 1
|
|
|
return count
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
# martix = InitMartix() # 初始化martix
|
|
|
martix = np.load("martix.npy")
|
|
|
u_martix = martix.copy() # 复制用户martix数组
|
|
|
|
|
|
form = GuiForm() # 构建主窗口
|
|
|
loca_o = tuple() # martix数组上次滞留坐标
|
|
|
|
|
|
note_mark = False # 笔记开启标志
|
|
|
note_list = [] # 笔记数字记录
|
|
|
note_martix = [] # 笔记位置记录列表
|
|
|
|
|
|
step = [] # 操作步骤
|
|
|
STEP = [[0, (0, 0), 0,martix,note_martix]] # 记录每一步的状态
|
|
|
seq = 1 # 全局操作次序初始化
|
|
|
|
|
|
time1 = MyTimer() # 实例计时器
|
|
|
time1.start() # 计时开始
|
|
|
|
|
|
control_number(form,bc,) # 显示输入键盘
|
|
|
control(form,bc) # 显示控制栏
|
|
|
|
|
|
face, top = text_enter(bc) # 返回信息框对象,和顶点坐标
|
|
|
empty_count = empty(martix) # 计算数组空格
|
|
|
face = timer(face,time1,empty_count) # 计时,空格显示
|
|
|
form.blit(face,top) # 显示信息框
|
|
|
|
|
|
refresh(form, martix, u_martix, note_martix) # 刷新屏幕
|
|
|
|
|
|
|
|
|
while True:
|
|
|
"""动态区"""
|
|
|
face, top = text_enter(bc) # 返回信息框对象,和顶点坐标
|
|
|
face = timer(face, time1, empty_count) # 显示时间到消息框对象
|
|
|
face = noter(face, note_mark) # 显示笔记模式到消息框对象
|
|
|
face = seq_recode(face,STEP) # 显示步骤记录到消息框对象
|
|
|
face = win(face,u_martix,time1) # 显示数独成功到消息框对象
|
|
|
form.blit(face, top) # 显示消息框对象
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == 27):
|
|
|
exit()
|
|
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
|
|
pos = event.pos # 鼠标坐标
|
|
|
refresh(form,martix, u_martix, note_martix) # 下一次鼠标坐标更新前刷新屏幕
|
|
|
control(form,bc) # 跟随鼠标显示控制按钮
|
|
|
control_number(form, bc) # 更新数字按钮
|
|
|
|
|
|
loca = martix_map(pos) # 返回取选中的格子位置
|
|
|
if loca:
|
|
|
loca_o = loca # 保留上次点击过的坐标
|
|
|
|
|
|
listen_add(pos,loca_o) # 添加功能
|
|
|
listen_del(pos,loca_o) # 擦除功能
|
|
|
listen_all_note(pos) # 一键笔记功能
|
|
|
listen_hint(pos,loca_o) # 提示功能
|
|
|
listen_note(pos,loca_o) # 笔记功能
|
|
|
listen_backspace(pos) # 回撤功能
|
|
|
pygame.display.update()
|