2013-06-05 44 views
1

我想寫一個數獨拼圖求解器,到目前爲止,我一直試圖讓它顯示拼圖。這是我到目前爲止的代碼:Python數獨拼圖求解器不能正確顯示拼圖

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 

這是對細胞,這裏是網格代碼:

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 

當我嘗試打印它,我得到管道的兩條垂直線(|)。有人能告訴我我做錯了什麼嗎?

+0

但是在你的'template'每個數字之間有兩個管道? – kirelagin

+0

請問你能否顯示一些輸出。 – kirelagin

+0

請不要在所有內容前使用'__',除非你需要修改 –

回答

0

你的代碼真的很難閱讀。你應該把你的問題分解成多個子問題,並且在邏輯上構造它們。

但要直接回答您的問題,請在第7行中爲self.__template分配一個空模板。在第14行中,您將模板轉換爲字符列表(爲什麼?畢竟您沒有寫入它),並將其分配給self.__template_list。最後在第21至23行中,您將遍歷模板字符列表(仍爲空)並將其附加到self.__answer,您在__str__()中打印該字符。因此你只需要管道。

也許我可以給你一些提示如何改進代碼:

  1. 網格的文字表述應該是無關的電網的一般概念,因此不應該最關心的的你的Grid類的方法。在你的情況下,它拋棄了__init__()方法,並且很難理解該方法實際執行的操作。你可以用你的網格做幾個操作,不需要知道最終網格是如何顯示的(如果有的話)。

    輸出您的網格的代碼應完全限於負責該方法,在您的案例__str__()

  2. 對於與其他方法或類的用戶無關的變量,請使用局部變量而不是成員變量。舉例來說,在檢查實例成員時,不必要的成員變量會讓你的代碼更難以理解,效率更低,並且會使你更加困惑。

  3. 想象一下更邏輯地表示您的網格(並且僅包含必要的數據,而不是多餘的表示細節)的數據結構。我建議列表的列表,因爲這是很容易在Python中操作(例如,你也可以使用二維numpy數組)。

我建議這個東西類似於:

class Grid: 
    '''The grid for the soduku game.''' 

    def __init__(self, puzzle): 
     '''Constructs the soduku puzzle from the file.''' 

     self.grid = [] 

     with open(puzzle, "r") as f: 
      for line in f: 
       # strip CR/LF, replace . by space, make a list of chars 
       self.grid.append([" " if char in " ." else char for char in line.rstrip("\r\n")]) 

    def __str__(self): 
     '''Prints the soduku puzzle nicely.''' 

     lines = [] 

     for i, row in enumerate(self.grid): 
      if i != 0 and i % 3 == 0: 
       # add a separator every 3 lines 
       lines.append("+".join(["-" * 3] * 3)) 

      # add a separator every 3 chars 
      line = "|".join(map("".join, zip(*([iter(row)] * 3)))) 
      lines.append(line) 

     lines.append("") 

     return "\n".join(lines) 

請注意,此版本預計有非常嚴格的格式要求(不區分線或字符,每行字符的確切數字)的文件。您可以練習改進以閱讀更多自由格式。

另請注意,我使用的唯一成員變量是self.grid。所有其他變量對各個功能都是本地的。

2

這是錯誤的(它永遠是真實的)

if number == 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9: 

使用

if 1 <= number <= 9: 

這也是錯誤的

for char in self.__file: 
    if char == '.': 
     self.__puzzle += ' ' 
    else: 
     self.__puzzle += char 

遍歷文件的產量不是字符。

我建議你在較小的部分編寫和測試你的代碼。把一些print在裏面,以確保代碼正在做你期望的。

+0

或者如果你想確保它查找整數值,則使用'if number in range(1,10)'。 – tamasgal