Я работаю над решением судоку уже некоторое время, и я только что реализовал обнаружение, если есть только один пробел, число может go, и оно не работает. Кроме того, некоторый предыдущий код, похоже, не работал вообще.
def square_solver(board):
"""Remove confirmed values from the possible values in the squares"""
global possibleBoard
# Sets up a modulator to multiply by to get the 3x3 grid of one square with the first value being the rows and the second being the column
blockNum = [0, 0]
for _ in range(9):
# A loop that checks the 9 numbers in one of the squares
for x in range(3):
for y in range(3):
if not board[(blockNum[0] * 3) + x][(blockNum[0] * 3) + y] == " ": # Checks if that square a number
# Checks all the empty spots in one of the squares for that number, then removes them
for z in range(3):
for w in range(3):
try:
# Removes the number from the possible board
possibleBoard[(blockNum[0] * 3) + z][(blockNum[1] * 3) + w].remove(
board[(blockNum[0] * 3) + x][(blockNum[1] * 3) + y])
# If it can't do anything, run this
except (ValueError, AttributeError):
pass
blockNum = block_num(blockNum)
return board
counter = [0] * 9
blockNum = [0, 0]
for _ in range(9):
for x in range(3):
for y in range(3):
# Checks the possible board and counts how many time a possible number appears
if type(possibleBoard[(blockNum[0] * 3) + x][(blockNum[0] * 3) + y]) == list:
for z in range(len(possibleBoard[(blockNum[0] * 3) + x][(blockNum[0] * 3) + y])):
counter[possibleBoard[(blockNum[0] * 3) + x][(blockNum[0] * 3) + y][z] - 1] += 1
for x in range(len(counter)):
# Checks to see if there was any times only one number appeared
if counter[x] == 1:
for y in range(3):
for z in range(3):
try:
# Finds the solo number, and makes that number definite
if (x + 1) in possibleBoard[(blockNum[0] * 3) + y][(blockNum[0] * 3) + z]:
board[(blockNum[0] * 3) + y][(blockNum[0] * 3) + z] = x + 1
except TypeError:
pass
blockNum = block_num(blockNum)
# Rests the counter
counter = [0] * 9
Я переписал оба раздела кода, и оба раза он не решил загадки судоку. Я не могу понять, что не так, но я думаю, что это может иметь отношение к разделам board[(blockNum[0] * 3) + x][(blockNum[0] * 3) + y]
.
Полный код здесь .