|
|
@ -35,6 +35,7 @@ main_menu = tk.Menu(root)
|
|
|
|
root.config(menu=main_menu)
|
|
|
|
root.config(menu=main_menu)
|
|
|
|
game_menu(main_menu)
|
|
|
|
game_menu(main_menu)
|
|
|
|
auto_menu(main_menu)
|
|
|
|
auto_menu(main_menu)
|
|
|
|
|
|
|
|
martix_position_mapping = creat_martix_mapping(side)
|
|
|
|
|
|
|
|
|
|
|
|
def speed_set(spd):
|
|
|
|
def speed_set(spd):
|
|
|
|
global wait_time
|
|
|
|
global wait_time
|
|
|
@ -176,6 +177,8 @@ def load_game():
|
|
|
|
|
|
|
|
|
|
|
|
draw(loca, martix, u_martix, STEP)
|
|
|
|
draw(loca, martix, u_martix, STEP)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def left1(event):
|
|
|
|
def left1(event):
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
martix坐标映射
|
|
|
|
martix坐标映射
|
|
|
@ -185,21 +188,17 @@ def left1(event):
|
|
|
|
""""""
|
|
|
|
""""""
|
|
|
|
x = event.x
|
|
|
|
x = event.x
|
|
|
|
y = event.y
|
|
|
|
y = event.y
|
|
|
|
row = -1
|
|
|
|
|
|
|
|
col = -1
|
|
|
|
|
|
|
|
global STEP, loca, loca_o
|
|
|
|
global STEP, loca, loca_o
|
|
|
|
for cur_col in range(9):
|
|
|
|
|
|
|
|
for cur_row in range(9):
|
|
|
|
for key, posit_lt in martix_position_mapping.items():
|
|
|
|
top_x, top_y = cur_col * (bc + 3) + 50, cur_row * (bc + 3) + 50# 计算每个方格的四个角
|
|
|
|
if posit_lt[0] < x < posit_lt[0] + side and posit_lt[1] < y < posit_lt[1] + side:
|
|
|
|
ud_x, ud_y = (cur_col + 1) * (bc + 3) + 50, (cur_row + 1) * (bc + 3) + 50# 计算每个方格的四个角
|
|
|
|
print(key)
|
|
|
|
if x < ud_x and x > top_x:#计算点击的列数
|
|
|
|
|
|
|
|
col = cur_col
|
|
|
|
if key[0] >= 0 and key[1] >= 0 and not martix[key[0], key[1]]:
|
|
|
|
if y < ud_y and y > top_y:#计算点击的行数
|
|
|
|
# print(col, row, "dks")
|
|
|
|
row = cur_row
|
|
|
|
|
|
|
|
if row >= 0 and col >= 0 and not martix[row, col]:
|
|
|
|
|
|
|
|
if loca:#若已经点击过坐标
|
|
|
|
if loca:#若已经点击过坐标
|
|
|
|
loca_o = loca # 保留上次点击过的坐标
|
|
|
|
loca_o = loca # 保留上次点击过的坐标
|
|
|
|
loca = (row + 1, col + 1) # 更新选中的格子位置
|
|
|
|
loca = (key[0] + 1, key[1] + 1) # 更新选中的格子位置
|
|
|
|
draw(loca, martix, u_martix, STEP)#绘制界面
|
|
|
|
draw(loca, martix, u_martix, STEP)#绘制界面
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
@ -267,17 +266,17 @@ def u_number(num, loca):
|
|
|
|
possible[row][col].add(num)
|
|
|
|
possible[row][col].add(num)
|
|
|
|
draw(loca, martix, u_martix, STEP)#重新绘制界面
|
|
|
|
draw(loca, martix, u_martix, STEP)#重新绘制界面
|
|
|
|
|
|
|
|
|
|
|
|
def control_draw_old(bc):
|
|
|
|
def control_draw_old(side):
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
绘制数字按钮
|
|
|
|
绘制数字按钮
|
|
|
|
:param bc: 全局按钮大小
|
|
|
|
:param side: 全局按钮大小
|
|
|
|
:return:
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
for i in range(9):#遍历0~9
|
|
|
|
for i in range(9):#遍历0~9
|
|
|
|
top_x, top_y = i * (bc + 3) + 50, 12 * (bc + 3) + 10#计算每个按钮左上角坐标
|
|
|
|
top_x, top_y = i * (side + 3) + 50, 12 * (side + 3) + 10#计算每个按钮左上角坐标
|
|
|
|
B = tk.Button(root, text=f"{i + 1}", bg='white', fg='black',
|
|
|
|
B = tk.Button(root, text=f"{i + 1}", bg='white', fg='black',
|
|
|
|
font=('幼圆', 14, 'bold'), command=lambda num=i + 1: u_number(num, loca))
|
|
|
|
font=('幼圆', 14, 'bold'), command=lambda num=i + 1: u_number(num, loca))
|
|
|
|
B.place(x=top_x, y=top_y, width=bc + 3, height=bc + 3)#绘制按钮到图上
|
|
|
|
B.place(x=top_x, y=top_y, width=side + 3, height=side + 3)#绘制按钮到图上
|
|
|
|
def control_draw(bc, u_martix):
|
|
|
|
def control_draw(bc, u_martix):
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
绘制数字按钮
|
|
|
|
绘制数字按钮
|
|
|
@ -331,19 +330,19 @@ def listen_all_note(possible, u_martix):
|
|
|
|
u_martix[row][col] = 10#标记这个格子为存在提示
|
|
|
|
u_martix[row][col] = 10#标记这个格子为存在提示
|
|
|
|
draw(loca, martix, u_martix, STEP)#重新绘制界面
|
|
|
|
draw(loca, martix, u_martix, STEP)#重新绘制界面
|
|
|
|
|
|
|
|
|
|
|
|
def show_hint(bc, u_martix, possible):
|
|
|
|
def show_hint(side, u_martix, possible):
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
显示提示信息
|
|
|
|
显示提示信息
|
|
|
|
:param bc: 方格大小
|
|
|
|
:param side: 方格大小
|
|
|
|
:return:
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
hint_bc = int(bc // 3)# 小数字的间距
|
|
|
|
hint_bc = int(side // 3)# 小数字的间距
|
|
|
|
for row in range(9):
|
|
|
|
for row in range(9):
|
|
|
|
for col in range(9):
|
|
|
|
for col in range(9):
|
|
|
|
if not u_martix[row][col] == 10:
|
|
|
|
if not u_martix[row][col] == 10:
|
|
|
|
possible[row][col].clear()# 只有u_martix[j][i]为10才表示该格子有笔记存在
|
|
|
|
possible[row][col].clear()# 只有u_martix[j][i]为10才表示该格子有笔记存在
|
|
|
|
continue# 若该格无笔记,则跳过该格
|
|
|
|
continue# 若该格无笔记,则跳过该格
|
|
|
|
x, y = col * (bc + 3) + 50 + bc / 2, row * (bc + 3) + 50 + bc / 2# 计算大方格中点坐标
|
|
|
|
x, y = col * (side + 3) + 50 + side / 2, row * (side + 3) + 50 + side / 2# 计算大方格中点坐标
|
|
|
|
x = x - hint_bc#计算左上角第一个数字坐标
|
|
|
|
x = x - hint_bc#计算左上角第一个数字坐标
|
|
|
|
y = y - hint_bc#计算左上角第一个数字坐标
|
|
|
|
y = y - hint_bc#计算左上角第一个数字坐标
|
|
|
|
for ii in range(3):# 按照九宫格显示小数字
|
|
|
|
for ii in range(3):# 按照九宫格显示小数字
|
|
|
@ -352,9 +351,9 @@ def show_hint(bc, u_martix, possible):
|
|
|
|
if possible[row][col]: # 在用户已填的部分有可能导致部分格子无解,所以先加一个判断
|
|
|
|
if possible[row][col]: # 在用户已填的部分有可能导致部分格子无解,所以先加一个判断
|
|
|
|
for k in possible[row][col]:
|
|
|
|
for k in possible[row][col]:
|
|
|
|
if n == k:# 如果possible[row][col]中含有k,则说明该格子可以填k,则将k显示在方格中
|
|
|
|
if n == k:# 如果possible[row][col]中含有k,则说明该格子可以填k,则将k显示在方格中
|
|
|
|
cv.create_text(x + ii * hint_bc, y + jj * hint_bc,
|
|
|
|
canvas.create_text(x + ii * hint_bc, y + jj * hint_bc,
|
|
|
|
text=f"{n}", font=('楷体', 10), fill='purple')
|
|
|
|
text=f"{n}", font=('楷体', 10), fill='purple', tags="mini_num")
|
|
|
|
cv.update()#更新画布
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -430,6 +429,15 @@ def control(bc):
|
|
|
|
font=('幼圆', 12, 'bold'), command=lambda: listen_hint(loca, u_martix))
|
|
|
|
font=('幼圆', 12, 'bold'), command=lambda: listen_hint(loca, u_martix))
|
|
|
|
B5.place(x=(lon + 2) * 4 + 50, y=(bc + 3) * 11 + 8, width=lon, height=bc)
|
|
|
|
B5.place(x=(lon + 2) * 4 + 50, y=(bc + 3) * 11 + 8, width=lon, height=bc)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# def draw(loca, martix, u_martix, STEP):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def draw_fixation_num():
|
|
|
|
|
|
|
|
for i in range(9):
|
|
|
|
|
|
|
|
for n in range(9):
|
|
|
|
|
|
|
|
x,y = martix_position_mapping[i,n]
|
|
|
|
|
|
|
|
canvas.create_text(x+ side / 2, x + side / 2,
|
|
|
|
|
|
|
|
text=f"{u_martix[i, n]}", font=('幼圆', 18, 'bold'), fill="black", tags="fixa_num")
|
|
|
|
|
|
|
|
num_draw(side,i,n,martix,u_martix)
|
|
|
|
|
|
|
|
|
|
|
|
def draw(loca, martix, u_martix, STEP):
|
|
|
|
def draw(loca, martix, u_martix, STEP):
|
|
|
|
'''
|
|
|
|
'''
|
|
|
@ -437,33 +445,33 @@ def draw(loca, martix, u_martix, STEP):
|
|
|
|
:param loca: 当前位置
|
|
|
|
:param loca: 当前位置
|
|
|
|
:return:
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
for i in range(9):
|
|
|
|
|
|
|
|
for j in range(9):# 绘制小方格
|
|
|
|
|
|
|
|
rect_draw(BC, i, j)
|
|
|
|
|
|
|
|
if loca != ():#根据当前选中位置绘制阴影
|
|
|
|
if loca != ():#根据当前选中位置绘制阴影
|
|
|
|
|
|
|
|
canvas.delete("rect")
|
|
|
|
row, col = loca[0] - 1, loca[1] - 1
|
|
|
|
row, col = loca[0] - 1, loca[1] - 1
|
|
|
|
for i in range(9):
|
|
|
|
for i in range(9):
|
|
|
|
for j in range(9):
|
|
|
|
for j in range(9):
|
|
|
|
if col == i and row == j:#当前选中的方格使用灰色标记
|
|
|
|
if col == i and row == j:#当前选中的方格使用灰色标记
|
|
|
|
cv.create_rectangle(col * (BC + 3) + 52, row * (BC + 3) + 52, col * (BC + 3) + 48 + BC,
|
|
|
|
canvas.create_rectangle(col * (side + 3) + 52, row * (side + 3) + 52, col * (side + 3) + 48 + side,
|
|
|
|
row * (BC + 3) + 48 + BC, fill='grey', width=0)
|
|
|
|
row * (side + 3) + 48 + side, fill='grey', width=0, tags="rect")
|
|
|
|
elif col == i or row == j:#当前选中方格所在行列使用浅灰色标记
|
|
|
|
elif col == i or row == j:#当前选中方格所在行列使用浅灰色标记
|
|
|
|
cv.create_rectangle(i * (BC + 3) + 52, j * (BC + 3) + 52, i * (BC + 3) + 48 + BC,
|
|
|
|
canvas.create_rectangle(i * (side + 3) + 52, j * (side + 3) + 52, i * (side + 3) + 48 + side,
|
|
|
|
j * (BC + 3) + 48 + BC, fill='#CCCCCC', width=0)
|
|
|
|
j * (side + 3) + 48 + side, fill='#CCCCCC', width=0, tags="rect")
|
|
|
|
show_hint(BC, u_martix, possible)# 显示提示信息
|
|
|
|
canvas.delete("num")
|
|
|
|
for row in range(9):
|
|
|
|
num_draw(side, row, col, martix, u_martix)
|
|
|
|
for col in range(9):# 在小方格上绘制数字
|
|
|
|
|
|
|
|
num_draw(BC, row, col, martix, u_martix)
|
|
|
|
canvas.delete("mini_num")
|
|
|
|
print(u_martix.tolist())
|
|
|
|
show_hint(side, u_martix, possible)# 显示提示信息
|
|
|
|
print(loca)
|
|
|
|
|
|
|
|
control(BC)# 绘制五个控制按键
|
|
|
|
|
|
|
|
control_draw(BC, u_martix)# 绘制9个数字按钮
|
|
|
|
# print(u_martix.tolist())
|
|
|
|
|
|
|
|
# print(loca)
|
|
|
|
|
|
|
|
|
|
|
|
empty_count = empty(martix)# 计算数组空格
|
|
|
|
empty_count = empty(martix)# 计算数组空格
|
|
|
|
Difficulty(empty_count)# 根据空格数量显示难度
|
|
|
|
Difficulty(empty_count)# 根据空格数量显示难度
|
|
|
|
noter(note_mark)# 显示笔记提示
|
|
|
|
noter(note_mark)# 显示笔记提示
|
|
|
|
seq_recode(right, STEP)# 显示步骤
|
|
|
|
seq_recode(right, STEP)# 显示步骤
|
|
|
|
win(u_martix, time1, STEP)# 检查游戏是否结束
|
|
|
|
win(u_martix, time1, STEP)# 检查游戏是否结束
|
|
|
|
cv.update()# 更新画布
|
|
|
|
# canvas.update()# 更新画布
|
|
|
|
|
|
|
|
|
|
|
|
N = 30 # 空白格子数
|
|
|
|
N = 30 # 空白格子数
|
|
|
|
martix = InitMartix(N)
|
|
|
|
martix = InitMartix(N)
|
|
|
@ -488,12 +496,18 @@ step = [] # 操作步骤
|
|
|
|
STEP = [[0, (0, 0), 0, martix]] # 记录每一步的状态
|
|
|
|
STEP = [[0, (0, 0), 0, martix]] # 记录每一步的状态
|
|
|
|
seq = 1 # 全局操作次序初始化
|
|
|
|
seq = 1 # 全局操作次序初始化
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
draw_all_rect() # 显示底图
|
|
|
|
|
|
|
|
control(side) # 绘制五个控制按键
|
|
|
|
|
|
|
|
control_draw(side, u_martix) # 绘制9个数字按钮
|
|
|
|
|
|
|
|
draw_fixation_num()
|
|
|
|
|
|
|
|
|
|
|
|
time1 = MyTimer() # 实例计时器
|
|
|
|
time1 = MyTimer() # 实例计时器
|
|
|
|
# 显示时间信息
|
|
|
|
# 显示时间信息
|
|
|
|
show_Time(time1)
|
|
|
|
show_Time(time1)
|
|
|
|
draw(loca, martix, u_martix, STEP)
|
|
|
|
draw(loca, martix, u_martix, STEP)
|
|
|
|
|
|
|
|
|
|
|
|
cv.bind('<Button-1>', left1)
|
|
|
|
canvas.bind('<Button-1>', left1)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if __name__ == "__main__":
|
|
|
|
mainloop()
|
|
|
|
mainloop()
|