python解决数独问题_python中的数独游戏问题

本文介绍了用Python实现数独游戏的代码。通过构建棋盘、填充数字,并检查行、列和宫格来确保每行每列每个宫格内数字不重复。然而,代码在寻找第四个数字的可用位置时遇到问题,作者寻求帮助以完善解决方案。
摘要由CSDN通过智能技术生成

这是到目前为止我的代码,多亏了前面的一个问题,我设法把代码改成了代码,我可以在代码出现错误之前把代码运行到第4位。需要指出的一点是,它检查每一行和每列,如果它们清除了当前的数字,则会在列表中放入一个true。然后它像网格一样使用这些true和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)

另一个原因是有两个板必须使用一个来检查索引,以测试行、列和块。而另一个保存的是已更改的号码

谢谢,

雅各布

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值