所以这是我的代码到目前为止,由于此前的一个问题,我设法改为代码,我可以让它在发出错误之前运行到大约4号。需要指出的几点是,它检查每一行和每列,以及它们是否清除了当前数字,而不是在列表中添加了一个true。然后它使用那些trues和false就像一个网格,所以如果行是(true,true,false)并且列是(false,true,true)那么块中可接受的位置将是([0,1], [0,2],[1,1],[1,2]),这有助于将随机猜测保持在最小。我遇到的问题是第四个号码可用的位置不足。非常欢迎帮助
#sudoku game
import random
#generate board
def build_board():
board=[]
for i in range(81):
board.append(str(i)) #add numbers not empty spaces to help in index identification
return board
#add numbers
def fill_board(board):
change_board=board[:] #board to add changes to
current_num=1 #current number to place
for number in range(9): #for the 9 numbers
current_block=0 #block currently on
for time in range(9):
#get block
block=[] #block list
for i in board:
Inti=int(i)
pos=Inti // 27 * 3 + Inti % 9 // 3
if pos==current_block:
if isinstance(i,int)==True:
block.append(int(i))
elif isinstance(i,str)==True:
block.append(str(i))
#check row
TF_rows=[]
if current_block in (0,1,2): #which rows to check for this block
row_list=[0,1,2]
elif current_block in (3,4,5):
row_list=[3,4,5]
elif current_block in (6,7,8):
row_list=[6,7,8]
for i in row_list: #for each of those rows
#get index's from board to then get list of current numbers from change_board
row_index=[]
for num in board: #test all numbers in board to see if in this row
num=int(num)
pos=num//9
if pos==i: #if it is in row
row_index.append(num)
current_row=[]
for index in row_index: #use index's to check against the change board
to_add=change_board[index] #for each index
if isinstance(to_add,int)==True:
current_row.append(int(to_add))
elif isinstance(to_add,str)==True:
current_row.append(str(to_add))
if current_num in current_row:
TF_rows.append("false")
elif current_num not in current_row:
TF_rows.append("true")
#check columns
TF_columns=[]
if current_block in (0,3,6):
column_list=[0,1,2]
elif current_block in (1,4,7):
column_list=[3,4,5]
elif current_block in (2,5,8):
column_list=[6,7,8]
for i in column_list:
#get index's from board to then use to get list of current numbers from board changed
column_index=[]
for num in board:
num=int(num)
pos=num%9
if pos==i:
column_index.append(num)
current_column=[]
for index in column_index:
to_add=change_board[index]
if isinstance(to_add,int)==True:
current_column.append(int(to_add))
elif isinstance(to_add,str)==True:
current_column.append(str(to_add))
if current_num in current_column:
TF_columns.append("false")
elif current_num not in current_column:
TF_columns.append("true")
#evaluate true/false statements to get (x,y) type lists
positions=[]
ROW=0
for i in TF_rows:
COLUMN=0
for subI in TF_columns:
if i=="true" and subI=="true":
add_pos=[ROW,COLUMN]
#check to make sure position is clear, get index's from block and then use on change_board
if ROW==0:
ROW_INDEX=0
elif ROW==1:
ROW_INDEX=3
elif ROW==2:
ROW_INDEX=6
pos_to_check=ROW_INDEX + COLUMN
check_index=int(block[pos_to_check])
check=change_board[check_index]
if isinstance(check,str)==True:
positions.append(add_pos)
COLUMN+=1
ROW+=1
#change number
try:
add_position=random.choice(positions) #edit###if you add this try and except in then you can see that it gets pretty close to having the final answer, but not quite
except:
pass
ROW=add_position[0]
COLUMN=add_position[1]
if ROW==0:
ROW_INDEX=0
elif ROW==1:
ROW_INDEX=3
elif ROW==2:
ROW_INDEX=6
add_pos=ROW_INDEX+COLUMN
block_index=int(block[add_pos])
change_board[block_index]=current_num
current_block+=1
current_num+=1
return change_board
#display board
def display(board):
slicers=[0,9] #slice points
row_counter=0 #counts row to know when to place double line
for i in range(9): #for all the rows
RP=board[slicers[0]:slicers[1]] #sliced board
if row_counter in (3,6):
print("---------------------------------------")
print("---------------------------------------")
else:
print("---------------------------------------")
print("| {} | {} | {} || {} | {} | {} || {} | {} | {} |".format(RP[0],RP[1],RP[2],RP[3],RP[4],RP[5],RP[6],RP[7],RP[8]))
row_counter+=1 #update counter
slicers[0]+=9 #update slicers
slicers[1]+=9 #update slicers
print("---------------------------------------")
#test
board=build_board()
fill=fill_board(board)
display=display(fill)
另一个原因是有两个板与使用一个板检查索引来测试行,列和块有关。而另一个持有已经改变的数字
谢谢, 雅各布