Так что я пытаюсь сделать игру судоку, и она идет довольно неплохо, за исключением состояния, в котором я нахожусь в программе, выдает ошибку при каждом движении, которое я пытаюсь сделать.isRow проверяет, возможно ли перемещение строки и т. д., и isPossible объединяет 3, чтобы проверить, возможно ли перемещение.Может кто-нибудь сказать мне, что не так с моими проверками?
board = list(range(81)) # Creates a board with 9*9 elements
board2 = board # Duplicates this board for later verfications
def showLine(start, end): # Prints each line of the sudoku puzzle
for i in range(start, end):
if i < end-1:
print(str(board[i]), '|', end =" ")
else:
print(str(board[i]))
def showGroup(start): # Prints each 9*3 group of regions (1/3 of the puzzle)
for i in range(3):
end = start + 9
showLine(start, end)
start += 9
if start != 81:
print('----------+-----------+-----------')
def show(): # Prints whole puzzle
for i in range(0, 55, 27):
showGroup(i)
rows = []
columns = []
groups = []
for i in range(0, 80, 9): # Cretes a list of rows
rows.append(board[i:i+9])
for i in range(9): # Creates a list of columns
columns.append(board[i::9])
temp = 0
for x in range(0, 61, 3): # Creates a list of groups (3*3 regions)
group = []
for i in range(3):
for j in range(3):
temp = x + 9*i + j
group.append(temp)
groups.append(group)
#Duplicating rows columns and groups for verifications
rows2 = rows
columns2 = columns
groups2 = groups
def isRow(cell, number): # Checking if an introduces number isn't repeating in the row
x = 0
for row in rows2:
if cell in row:
x = rows2.index(row)
if number in rows[x]:
return False
else:
return True
def isColumn(cell, number):): # Checking if an introduces number isn't repeating in the column
x = 0
for column in columns2:
if cell in column:
x = columns2.index(column)
if number in columns[x]:
return False
else:
return True
def isGroup(cell, number):): # Checking if an introduces number isn't repeating in the group
x = 0
for group in groups2:
if cell in group:
x = groups2.index(group)
if number in groups[x]:
return False
else:
return True
def isPossible(cell, number): # Combines the last 3 verifications
if isRow(cell, number) and isColumn(cell, number) and isGroup(cell, number):
return True
else:
return False
for i in board: # Fills the board with 0's to not interfere with the game
board[i] = 0
while True:
show()
selected_cell = int(input("Please select an available cell: "))
number = int(input("Please select a number for that cell: "))
if isPossible(selected_cell, number):
board[selected_cell] = number
else:
print("Error, please try again")