저는 Sudoku puzzle solver를 작성하려 했었고, 지금까지 퍼즐을 표시하려고 노력했습니다. 여기 내 코드는 지금까지입니다 : 나는 그것을 인쇄하려고 할 때Python sudoku puzzle solver가 정확하게 퍼즐을 표시하지 않습니다.
class Grid:
'''The grid for the soduku game.'''
def __init__(self, puzzle):
'''Constructs the soduku puzzle from the file.'''
self.__file = open(puzzle)
self.__puzzle = ''
self.__template = ' | | \n | | \n | | \n | | \n | | \n | | \n | | \n | | \n | | \n'
for char in self.__file:
if char == '.':
self.__puzzle += ' '
else:
self.__puzzle += char
count = 0
self.__template_list = list(self.__template)
for char in self.__puzzle:
if char != '|':
if char == '.' or ' ':
self.__template_list[count] = ' '
else:
self.__template_list[count] = char
self.__answer = ''
for char in self.__template_list:
self.__answer += char
self.__file.close()
def __str__(self):
'''Prints the soduku puzzle nicely.'''
return self.__answer
, 나는 파이프의 두 개의 수직선을 얻을 :
여기에 세포이며,class Cell:
'''A cell for the soduku game.'''
def __init__(self):
#This is our constructor
self.__done = False #We are not finished at the start
self.__answer = (1,2,3,4,5,6,7,8,9) #Here is the tuple containing all of our possibilities
self.__setnum = 8 #This will be used later when we set the number.
def __str__(self):
'''This formats what the cell returns.'''
answer = 'This cell can be: '
answer += str(self.__answer) #This pulls our answer from our tuple
return answer
def get_possible(self):
'''This tells us what our possibilities exist.'''
answer =()
return self.__answer
def is_done(self):
'''Does a simple check on a variable to determine if we are done.'''
return self.__done
def remove(self, number):
'''Removes a possibility from the possibility tuple.'''
if number == 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9: #Checks if we have a valid answer
temp = list(self.__answer) #Here is the secret: We change the tuple to a list, which we can easily modify, and than turn it back.
temp.remove(number)
self.__answer = tuple(temp)
def set_number(self, number):
'''Removes all but one possibility from the possibility tuple. Also sets "__done" to true.'''
answer = 8
for num in self.__answer:
if num == number:
answer = number #Checks if the number is in the tuple, and than sets that value as the tuple, which becomes an integer.
self.__answer = answer
self.__done = True
return self.__answer
그리드에 대한 코드입니다 (|). 누군가 내가 뭘 잘못하고 있다고 말할 수 있습니까? 이 파일 수익률 이상도 잘못
for char in self.__file:
if char == '.':
self.__puzzle += ' '
else:
self.__puzzle += char
순회입니다
if 1 <= number <= 9:
그러나 각 번호 사이에 두 개의 파이프가 있습니다. '템플릿'은 무엇입니까? – kirelagin
일부 출력물을 보여 주시겠습니까? – kirelagin
당신이 _need_ mangling을하지 않는 한, 모든것 앞에서'__'을 사용하지 마십시오. –