使用python生成sudoku游戏附加2个独立程序,生成sudoku题库和解决sudoku的程序-泓源视野

使用python生成sudoku游戏附加2个独立程序,生成sudoku题库和解决sudoku的程序

#说明新建sudoku文件夹下面由两个模块组成分别为主程序gui.py和解决模块solve.py 游戏运行主程序gui.py

#游戏玩法再生成的已知代码中的数组 9x9的题目 生成已知游戏,然后再图形界面中输入输入使用回车进行确认

# GUI.py
# RUN THIS FILE
import pygame
from solver import solve, valid
import time
pygame.font.init()

class Grid:
# To change the starting board change this
board = [
[0, 0, 0, 0, 2, 4, 0, 0, 3],
[3, 0, 0, 5, 0, 0, 0, 2, 0],
[0, 0, 4, 0, 0, 0, 1, 6, 0],
[0, 7, 6, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 6, 2, 8, 0, 0],
[0, 1, 0, 8, 0, 0, 6, 0, 7],
[0, 3, 0, 4, 0, 0, 5, 0, 8],
[0, 0, 7, 2, 0, 5, 0, 0, 0],
[0, 5, 0, 9, 8, 7, 0, 4, 2]
]

def __init__(self, rows, cols, width, height):
self.rows = rows
self.cols = cols
self.cubes = [[Cube(self.board[i][j], i, j, width, height) for j in range(cols)] for i in range(rows)]
self.width = width
self.height = height
self.model = None
self.selected = None

def update_model(self):
self.model = [[self.cubes[i][j].value for j in range(self.cols)] for i in range(self.rows)]

def place(self, val):
row, col = self.selected
if self.cubes[row][col].value == 0:
self.cubes[row][col].set(val)
self.update_model()

if valid(self.model, val, (row,col)) and solve(self.model):
return True
else:
self.cubes[row][col].set(0)
self.cubes[row][col].set_temp(0)
self.update_model()
return False

def sketch(self, val):
row, col = self.selected
self.cubes[row][col].set_temp(val)

def draw(self, win):
# Draw Grid Lines
gap = self.width / 9
for i in range(self.rows+1):
if i % 3 == 0 and i != 0:
thick = 4
else:
thick = 1
pygame.draw.line(win, (0,0,0), (0, i*gap), (self.width, i*gap), thick)
pygame.draw.line(win, (0, 0, 0), (i * gap, 0), (i * gap, self.height), thick)

# Draw Cubes
for i in range(self.rows):
for j in range(self.cols):
self.cubes[i][j].draw(win)

def select(self, row, col):
# Reset all other
for i in range(self.rows):
for j in range(self.cols):
self.cubes[i][j].selected = False

self.cubes[row][col].selected = True
self.selected = (row, col)

def clear(self):
row, col = self.selected
if self.cubes[row][col].value == 0:
self.cubes[row][col].set_temp(0)

def click(self, pos):
"""
:param: pos
:return: (row, col)
"""
if pos[0] < self.width and pos[1] < self.height:
gap = self.width / 9
x = pos[0] // gap
y = pos[1] // gap
return (int(y),int(x))
else:
return None

def is_finished(self):
for i in range(self.rows):
for j in range(self.cols):
if self.cubes[i][j].value == 0:
return False
return True

class Cube:
rows = 9
cols = 9

def __init__(self, value, row, col, width ,height):
self.value = value
self.temp = 0
self.row = row
self.col = col
self.width = width
self.height = height
self.selected = False

def draw(self, win):
fnt = pygame.font.SysFont("comicsans", 40)

gap = self.width / 9
x = self.col * gap
y = self.row * gap

if self.temp != 0 and self.value == 0:
text = fnt.render(str(self.temp), 1, (128,128,128))
win.blit(text, (x+5, y+5))
elif not(self.value == 0):
text = fnt.render(str(self.value), 1, (0, 0, 0))
win.blit(text, (x + (gap/2 - text.get_width()/2), y + (gap/2 - text.get_height()/2)))

if self.selected:
pygame.draw.rect(win, (255,0,0), (x,y, gap ,gap), 3)

def set(self, val):
self.value = val

def set_temp(self, val):
self.temp = val

def redraw_window(win, board, time, strikes):
win.fill((255,255,255))
# Draw time
fnt = pygame.font.SysFont("comicsans", 40)
text = fnt.render("Time: " + format_time(time), 1, (0,0,0))
win.blit(text, (540 - 160, 560))
# Draw Strikes
text = fnt.render("X " * strikes, 1, (255, 0, 0))
win.blit(text, (20, 560))
# Draw grid and board
board.draw(win)

def format_time(secs):
sec = secs%60
minute = secs//60
hour = minute//60

mat = " " + str(minute) + ":" + str(sec)
return mat

def main():
win = pygame.display.set_mode((540,600))
pygame.display.set_caption("Sudoku")
board = Grid(9, 9, 540, 540)
key = None
run = True
start = time.time()
strikes = 0
while run:

play_time = round(time.time() - start)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
key = 1
if event.key == pygame.K_2:
key = 2
if event.key == pygame.K_3:
key = 3
if event.key == pygame.K_4:
key = 4
if event.key == pygame.K_5:
key = 5
if event.key == pygame.K_6:
key = 6
if event.key == pygame.K_7:
key = 7
if event.key == pygame.K_8:
key = 8
if event.key == pygame.K_9:
key = 9
if event.key == pygame.K_DELETE:
board.clear()
key = None
if event.key == pygame.K_RETURN:
i, j = board.selected
if board.cubes[i][j].temp != 0:
if board.place(board.cubes[i][j].temp):
print("Success")
else:
print("Wrong")
strikes += 1
key = None

if board.is_finished():
print("Game over")
run = False

if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
clicked = board.click(pos)
if clicked:
board.select(clicked[0], clicked[1])
key = None

if board.selected and key != None:
board.sketch(key)

redraw_window(win, board, play_time, strikes)
pygame.display.update()

main()
pygame.quit()

 


# solver.py

def solve(bo):
find = find_empty(bo)
if not find:
return True
else:
row, col = find

for i in range(1,10):
if valid(bo, i, (row, col)):
bo[row][col] = i

if solve(bo):
return True

bo[row][col] = 0

return False

def valid(bo, num, pos):
# Check row
for i in range(len(bo[0])):
if bo[pos[0]][i] == num and pos[1] != i:
return False

# Check column
for i in range(len(bo)):
if bo[i][pos[1]] == num and pos[0] != i:
return False

# Check box
box_x = pos[1] // 3
box_y = pos[0] // 3

for i in range(box_y*3, box_y*3 + 3):
for j in range(box_x * 3, box_x*3 + 3):
if bo[i][j] == num and (i,j) != pos:
return False

return True

def print_board(bo):
for i in range(len(bo)):
if i % 3 == 0 and i != 0:
print("- - - - - - - - - - - - - ")

for j in range(len(bo[0])):
if j % 3 == 0 and j != 0:
print(" | ", end="")

if j == 8:
print(bo[i][j])
else:
print(str(bo[i][j]) + " ", end="")

def find_empty(bo):
for i in range(len(bo)):
for j in range(len(bo[0])):
if bo[i][j] == 0:
return (i, j) # row, col

return None


附加解决独立程序(用于解决无法完成的sudoku)

import random
import sys
import numpy as np

sys.setrecursionlimit(100000) # 发现python默认的递归深度是很有限的
#(默认是1000),因此当递归深度超过999的
# 样子,就会引发这样的一个异常。

def get_next(m:"数独矩阵", x:"空白格行数", y:"空白格列数"):
""" 功能:获得下一个空白格在数独中的坐标。
"""
for next_y in range(y+1, 9): # 下一个空白格和当前格在一行的情况
if m[x][next_y] == 0:
return x, next_y
for next_x in range(x+1, 9): # 下一个空白格和当前格不在一行的情况
for next_y in range(0, 9):
if m[next_x][next_y] == 0:
return next_x, next_y
return -1, -1 # 若不存在下一个空白格,则返回 -1,-1

def value(m:"数独矩阵", x:"空白格行数", y:"空白格列数"):
""" 功能:返回符合"每个横排和竖排以及
九宫格内无相同数字"这个条件的有效值。
"""
i, j = x//3, y//3
grid = [m[i*3+r][j*3+c] for r in range(3) for c in range(3)]
v = set([x for x in range(1,10)]) - set(grid) - set(m[x]) - \
set(list(zip(*m))[y])
return list(v)

def start_pos(m:"数独矩阵"):
""" 功能:返回第一个空白格的位置坐标"""
for x in range(9):
for y in range(9):
if m[x][y] == 0:
return x, y
return False, False # 若数独已完成,则返回 False, False

def try_sudoku(m:"数独矩阵", x:"空白格行数", y:"空白格列数"):
""" 功能:试着填写数独 """
for v in value(m, x, y):
m[x][y] = v
next_x, next_y = get_next(m, x, y)
if next_y == -1: # 如果无下一个空白格
return True
else:
end = try_sudoku(m, next_x, next_y) # 递归
if end:
return True
m[x][y] = 0 # 在递归的过程中,如果数独没有解开,
# 则回溯到上一个空白格

def sudoku(m):
x, y = start_pos(m)
try_sudoku(m, x, y)
print(np.array(m))

 

if __name__ == "__main__":
m = [
[0, 0, 0, 0, 2, 4, 0, 0, 3],
[3, 0, 0, 5, 0, 0, 0, 2, 0],
[0, 0, 4, 0, 0, 0, 1, 6, 0],
[0, 7, 6, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 6, 2, 8, 0, 0],
[0, 1, 0, 8, 0, 0, 6, 0, 7],
[0, 3, 0, 4, 0, 0, 5, 0, 8],
[0, 0, 7, 2, 0, 5, 0, 0, 0],
[0, 5, 0, 9, 8, 7, 0, 4, 2]
]

sudoku(m)

""" 数独结果如下:
[
[6, 9, 5, 1, 2, 3, 7, 4, 8],
[7, 4, 1, 8, 6, 9, 2, 5, 3],
[2, 3, 8, 4, 5, 7, 1, 6, 9],
[8, 1, 6, 7, 4, 5, 3, 9, 2],
[5, 2, 4, 3, 9, 8, 6, 7, 1],
[3, 7, 9, 6, 1, 2, 4, 8, 5],
[4, 8, 3, 9, 7, 1, 5, 2, 6],
[1, 6, 2, 5, 8, 4, 9, 3, 7],
[9, 5, 7, 2, 3, 6, 8, 1, 4]
]
"""


附加程序2 用于生成sudoku的游戏题目数组

import random
import time
def pr(a):#用于输出方便阅读的结果
for i in range(9):
if i%3==0:
print('\t---------\t\t---------\t\t---------')
for j in range(9):
if j%3==0:
print('|',end='\t')
print(a[i][j],end='\t')
print('|',end='\n')
print('\t---------\t\t---------\t\t---------')
def diff(d):
print('数独难度:',end='')
if d==0:
print('极容易')
if d==1:
print('容易')
if d==2:
print('一般')
if d==3:
print('困难')
if d==4:
print('极困难')
def con(a):#用于将数据转化为符合格式的序列
if len(a)==81:
a_con=[[],[],[],[],[],[],[],[],[]]
for i in range(9):
b=a[i*9:i*9+9]
for j in range(9):
a_con[i].append(int(b[j]))
return a_con
return False
def examine_sudoku(sudoku):#检查是否符合数独规则
if len(sudoku)!=9:
return False
for i in range(9):
if len(sudoku[i])!=9 or sudoku[i].count([])>0:
return False
for j in range(1,10):
if sudoku[i].count(j)>1:
return False
return True
def complete(a):#用于检测数独是否完成
for i in range(9):
for j in range(9):
if type(a[i][j])!=int or a[i][j]==0:
return False
return True
def copy(a):#复制数独的值,用于后期假设法运算失败,找回原来的值
d=[[], [], [], [], [], [], [], [], []]
for i in range(9):
for j in range(9):
if type(a[i][j])==int:
d[i].append(a[i][j])
elif type(a[i][j])==list:
d[i].append([])
for k in a[i][j]:
d[i][j].append(k)
return d
def extract_b_c(a):#用9行表示的a,得到9列表示的b,及9宫表示的c,并对每个0所在的位置用[1,2,3,4,5,6,7,8,9]代替,作为笔记
b=[[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]]
c=[[],[],[],[],[],[],[],[],[]]
for i in range(9):
for j in range(9):
if a[i][j]==0:
a[i][j] = [1,2,3,4,5,6,7,8,9]
b[j][i]=a[i][j]
s=ij_s(i,j)
c[s].append(a[i][j])
return b,c
def ij_s(i,j):#用i和j求得宫所在的位置s
s=i//3*3+j//3
return s
def ij_t(i,j):#用i和j求得所在宫里的位置t
t=i%3*3+j%3
return t
def st_i(s,t):#用s和t求得原i的值
i=s//3*3+t//3
return i
def st_j(s, t):#用s和t求得原j的值
j=s%3*3+t%3
return j
def play_1(a,b,c,i,j,change):#将笔记中同行、同列及同宫的笔记里删除已知数字,并将唯一可能的格子变成已知数字
s=ij_s(i,j)
for num in range(1,10):
for k in range(9):
if type(a[i][k])==list and num in a[i] and num in a[i][k]:
a[i][k].remove(num)
change=True
if len(a[i][k])==1:
n=a[i][k][0]
a[i][k]=n
b[k][i]=n
c[ij_s(i,k)][ij_t(i,k)]=n
change=True
if type(b[j][k])==list and num in b[j] and num in b[j][k]:
b[j][k].remove(num)
change=True
if len(b[j][k])==1:
n=b[j][k][0]
a[k][j]=n
b[j][k]=n
c[ij_s(k,j)][ij_t(k,j)]=n
change=True
if type(c[s][k])==list and num in c[s] and num in c[s][k]:
c[s][k].remove(num)
change=True
if len(c[s][k])==1:
n=c[s][k][0]
a[st_i(s,k)][st_j(s,k)]=n
b[st_j(s,k)][st_i(s,k)]=n
c[s][k]=n
change=True
return a,b,c,change
def play_2(a,b,c,i,j,change):#唯一候选数法
def num_abc():
num_a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
num_b = [1, 2, 3, 4, 5, 6, 7, 8, 9]
num_c = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for k in range(1,10):
if k in a[i]:
num_a.remove(k)
if k in b[j]:
num_b.remove(k)
if k in c[s]:
num_c.remove(k)
return num_a,num_b,num_c
s=ij_s(i,j)
num_a,num_b,num_c=num_abc()
for n in num_a:
t=0
for k in range(9):
if type(a[i][k])==list and n in a[i][k]:
t+=1
if t==1:
for k in range(9):
if type(a[i][k]) == list and n in a[i][k]:
a[i][k] = n
b[k][i] = n
c[ij_s(i, k)][ij_t(i, k)] = n
num_a, num_b, num_c = num_abc()
change=True
for n in num_b:
t=0
for k in range(9):
if type(b[j][k])==list and n in b[j][k]:
t+=1
if t==1:
for k in range(9):
if type(b[j][k])==list and n in b[j][k]:
a[k][j] = n
b[j][k] = n
c[ij_s(k, j)][ij_t(k, j)] = n
num_a, num_b, num_c = num_abc()
change=True
for n in num_c:
t=0
for k in range(9):
if type(c[s][k])==list and n in c[s][k]:
t+=1
if t==1:
for k in range(9):
if type(c[s][k])==list and n in c[s][k]:
a[st_i(s,k)][st_j(s,k)]=n
b[st_j(s,k)][st_i(s,k)]=n
c[s][k]=n
change=True
return a,b,c,change
def play_3(a,b,c,i,j,change):#链数删减法,专业用语是这样的,有2链数、3链数的,此方法扩展到n链数(帮助排除不可能项的)
list_a=[]
list_b=[]
list_c=[]
s=ij_s(i,j)
for k in range(9):
if type(a[i][k]) == list:
list_a.append(a[i][k])
for k in range(9):
if type(b[j][k]) == list:
list_b.append(b[j][k])
for k in range(9):
if type(c[s][k]) == list:
list_c.append(c[s][k])
for k in range(3,len(list_a)+1):
l=[]
n=0
for m in list_a:
if len(m)<k:
for p in m:
if p not in l:
l.append(p)
n+=1
if len(l)==n:
for m in list_a:
for p in m:
if p not in l:
for q in l:
if q in m:
m.remove(q)
change=True
for k in range(3,len(list_b)+1):
l=[]
n=0
for m in list_b:
if len(m)<k:
for p in m:
if p not in l:
l.append(p)
n+=1
if len(l)==n:
for m in list_b:
for p in m:
if p not in l:
for q in l:
if q in m:
m.remove(q)
change=True
for k in range(3,len(list_c)+1):
l=[]
n=0
for m in list_c:
if len(m)<k:
for p in m:
if p not in l:
l.append(p)
n+=1
if len(l)==n:
for m in list_c:
for p in m:
if p not in l:
for q in l:
if q in m:
m.remove(q)
change=True
return a,b,c,change
def play_4(a,b,c,change):#区块摒除法
for i in range(3):
m = [[], [], [], [], [], [], [], [], []]
for k in range(3):
for j in range(9):
if type(a[i * 3 + k][j]) == list:
for n in a[i * 3 + k][j]:
if n not in m[k * 3 + j // 3]:
m[k * 3 + j // 3].append(n)
for p in range(3):
o = []
for q in range(3):
if m[p * 3 + q]!=[]:
for k in m[p * 3 + q]:
if k not in o:
o.append(k)
if o!=[]:
for s in o:
t = 0
for q in range(3):
if s in m[p * 3 + q]:
t += 1
if t == 1:
for q in range(3):
if s in m[p * 3 + q]:
if p == 0:
l = [1, 2]
elif p == 1:
l = [0, 2]
else:
l = [0, 1]
for k in l:
for j in range(3):
if type(a[i * 3 + k][q*3+j]) == list and s in a[i * 3 + k][q*3+j]:
a[i * 3 + k][q*3+j].remove(s)
change = True
for i in range(3):
m = [[], [], [], [], [], [], [], [], []]
for k in range(3):
for j in range(9):
if type(b[i * 3 + k][j]) == list:
for n in b[i * 3 + k][j]:
if n not in m[k * 3 + j // 3]:
m[k * 3 + j // 3].append(n)
for p in range(3):
o = []
for q in range(3):
if m[p * 3 + q]!=[]:
for k in m[p * 3 + q]:
if k not in o:
o.append(k)
if o!=[]:
for s in o:
t = 0
for q in range(3):
if s in m[p * 3 + q]:
t += 1
if t == 1:
for q in range(3):
if s in m[p * 3 + q]:
if p == 0:
l = [1, 2]
elif p == 1:
l = [0, 2]
else:
l = [0, 1]
for k in l:
for j in range(3):
if type(b[i * 3 + k][q*3+j]) == list and s in b[i * 3 + k][q*3+j]:
b[i * 3 + k][q*3+j].remove(s)
change = True
return a,b,c,change
def play_5(a,b,c,change,examine,a_copy):#假设法
if examine==True:
for l in range(2,10):
for i in range(9):
for j in range(9):
if type(a[i][j])==list and len(a[i][j])==l:
a_copy=copy(a)
n=a[i][j][random.randint(0,len(a[i][j])-1)]
a_copy[i][j].remove(n)
if len(a_copy[i][j])==1:
a_copy[i][j]=a_copy[i][j][0]
a[i][j]=n
b[j][i]=n
c[ij_s(i,j)][ij_t(i,j)]=n
change=True
return a,b,c,change,examine,a_copy
if examine==False and examine_sudoku(a_copy)==True:
a=a_copy
b,c=extract_b_c(a)
change=True
examine=True
return a,b,c,change,examine,a_copy
examine=False
return a,b,c,change,examine,a_copy
def play_all(a,b,c,change,examine,a_copy,d):#进阶型运算,能初级方法解决就不用高级的方法,省时
for i in range(9):
for j in range(9):
a,b,c,change=play_1(a,b,c,i,j,change)
if change==False:
for i in range(9):
for j in range(9):
a,b,c,change=play_2(a,b,c,i,j,change)
if d<1 and change==True:
if complete(a)==False:
d=1
if change==False:
for i in range(9):
for j in range(9):
a,b,c,change=play_3(a,b,c,i,j,change)
if d<2 and change==True:
if complete(a)==False:
d=2
if change==False:
a,b,c,change=play_4(a,b,c,change)
if d<3 and change==True:
if complete(a)==False:
d=3
if change==False:
examine=examine_sudoku(a)
a,b,c,change,examine,a_copy=play_5(a,b,c,change,examine,a_copy)
if d<4 and change==True:
if complete(a)==False:
d=4
return a,b,c,change,examine,a_copy,d
def play_all_1(a,b,c,change,examine,a_copy):#进阶型运算,能初级方法解决就不用高级的方法,省时
for i in range(9):
for j in range(9):
a,b,c,change=play_1(a,b,c,i,j,change)
if change==False:
for i in range(9):
for j in range(9):
a,b,c,change=play_2(a,b,c,i,j,change)
if change==False:
for i in range(9):
for j in range(9):
a,b,c,change=play_3(a,b,c,i,j,change)
if change==False:
a,b,c,change=play_4(a,b,c,change)
return a,b,c,change,examine,a_copy
def complate_sudoku(a):#计算数独的主体
if examine_sudoku(a)==False:
return False
b,c = extract_b_c(a)
if examine_sudoku(b)==False:
return False
if examine_sudoku(c)==False:
return False
change = True#初始化change
examine=True#初始化examine
a_copy=[]#初始化a_copy
d=0
while change==True:
change=False
a,b,c,change,examine,a_copy,d=play_all(a,b,c,change,examine,a_copy,d)
examine=examine_sudoku(a)&examine_sudoku(b)&examine_sudoku(c)
if complete(a)==False:
return None,4
return a,d
print('正在生成数独......')
t=time.time()
a=[[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]]
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for j in range(9):
if j<8:
k=random.randint(0,len(l)-1)
a[0][j]=l[k]
del l[k]
else:
a[0][j] = l[0]
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
l.remove(a[0][0])
l.remove(a[0][1])
l.remove(a[0][2])
for i in range(1,9):
if i==3:
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
l.remove(a[0][0])
l.remove(a[1][0])
l.remove(a[2][0])
if i<8:
k=random.randint(0,len(l)-1)
a[i][0]=l[k]
del l[k]
else:
a[i][0] = l[0]
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
l.remove(a[0][6])
l.remove(a[0][7])
l.remove(a[0][8])
for i in range(1,9):
if i==3:
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
l.remove(a[0][8])
l.remove(a[1][8])
l.remove(a[2][8])
if i<8:
if i==7:
if a[8][0] in l:
a[7][8]=a[8][0]
l.remove(a[8][0])
a[8][8]=l[0]
break
k=random.randint(0,len(l)-1)
while l[k]==a[i][0]:
k = random.randint(0, len(l) - 1)
a[i][8]=l[k]
del l[k]
else:
a[i][8] = l[0]
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
l.remove(a[8][0])
l.remove(a[8][8])
b, c = extract_b_c(a)
a_copy2=[]
d=0
change = True # 初始化change
examine = True # 初始化examine
a_co = [] # 初始化a_copy
while change == True:
change = False
a, b, c, change, examine, a_co ,d= play_all(a, b, c, change, examine, a_co,d)
print('数独答案生成完毕,开始抠除数字......')
ti=0
com=copy(a)
change = True # 初始化change
x=True
co=copy(a)
l=[0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80]
while x==True:
change = True # 初始化change
examine = True # 初始化examine
a_co = [] # 初始化a_copy
if len(l)>1:
k=random.randint(0,len(l)-1)
else:
k=0
x=False
p=l[k]//9
q=l[k]%9
del l[k]
if co[p][q]==0:
continue
co[p][q]=0
cop=copy(co)
cop_b, cop_c = extract_b_c(cop)
while change == True:
change = False
cop, cop_b, cop_c, change, examine, a_co= play_all_1(cop, cop_b, cop_c, change, examine, a_co)
if complete(cop)==True:
a = copy(co)
else:
if ti==0:
print('一般难度已生成,正在精细推敲骨灰难度......')
ti=1
for i in range(9):
for j in range(9):
if type(cop[i][j])==list and len(cop[i][j])==2:
a_copy=copy(cop)
n=cop[i][j][0]
a_copy[i][j]=a_copy[i][j][1]
a_copy_b, a_copy_c = extract_b_c(a_copy)
cop[i][j]=n
cop_b[j][i]=n
cop_c[ij_s(i,j)][ij_t(i,j)]=n
change=True
while change == True:
change = False
cop, cop_b, cop_c, change, examine, a_co,d=play_all(cop, cop_b, cop_c, change, examine, a_co,d)
while change == True:
change = False
a_copy, a_copy_b, a_copy_c, change, examine, a_copy2,d=play_all(a_copy, a_copy_b, a_copy_c, change, examine, a_copy2,d)
if (complete(cop)==True and complete(a_copy)==False) or (complete(cop)==False and complete(a_copy)==True):
if cop==com or cop==com:
a = copy(co)
print('数独生成完毕,生成数独所用时间为:',time.time()-t)
pr(a)
t=time.time()
a,d=complate_sudoku(a)
print('解数独所用时间为:',time.time()-t)
diff(d)

 

本文由 泓源视野 作者:admin 发表,其版权均为 泓源视野 所有,文章内容系作者个人观点,不代表 泓源视野 对观点赞同或支持。如需转载,请注明文章来源。
15

发表评论

Protected with IP Blacklist CloudIP Blacklist Cloud
您是第8235595 位访客, 您的IP是:[52.15.112.69]