2016-01-05 65 views
1

我做了遊戲:與龜圖形河內塔,因爲它是我的學校作業。蟒蛇與龜圖形移動磁盤河內的塔

#For moving disks 
Selected = [] 
Steps = 0 

amount = 6 #amount of disks 

#Properties = [X, Y, Width, Height, Tower's Disks] 
Tower1 = [-2 - amount, -1, amount, 3/2, []] 
Tower2 = [0, -1, amount, 3/2, []] 
Tower3 = [2 + amount, -1, amount, 3/2, []] 

Tower1[4] = CreateDisks(amount, Tower1) 

Tower1 = TowerCreate(Tower1) 
Tower2 = TowerCreate(Tower2) 
Tower3 = TowerCreate(Tower3) 
#(After/Na 'TowerCreate') Properties = [X, Y, Width, Height, Tower's Disks, Tower's Button] 

現在有這樣一段代碼的運動,但我的老師不會允許我使用類,所以我只能用功能(DEFS),任何人都可以幫助我重新編碼到這一點DEFS所以我程序仍然有效?這樣您仍然可以單擊按鈕,並且磁盤移動到正確的位置。

class Move: 
    #'Properties' contents: [X, Y, Width, Height, Tower's Disks, Tower's Button] 
    #'Selected' contents: [Disk to move, Tower's selected Button/Start Disk location] 
    def __init__(self, Properties): 
     self.Properties = Properties 
     self.X = Properties[0] * 20 
     self.Disk_List = Properties[4] 
     self.Act_Colour = 'white' 
     self.Inact_Colour = 'black' 

    def movement(self, x, y): 
     global Steps 
     self.Y = (self.Properties[1] + len(self.Disk_List)) * 20 + 45 

     if len(Selected) == 0 and len(self.Disk_List) > 0: 
      self.Properties[5].fillcolor(self.Act_Colour) 
      Selected.append(self.Disk_List) #Adds the list with disks to the selected tower 
      Selected.append(self.Properties[5]) #To reset the color of the botton 
     elif len(Selected) == 2 and len(self.Disk_List) > 0 and Selected[0][-1].shapesize()[1] == self.Disk_List[-1].shapesize()[1]: #To deselect toe button 
      Selected[1].fillcolor(self.Inact_Colour) 
      del Selected[0] 
      del Selected[0]    
     elif len(Selected) == 2 and (len(self.Disk_List) == 0 or Selected[0][-1].shapesize()[1] < self.Disk_List[-1].shapesize()[1]): #Lazy function 
      Selected[1].fillcolor(self.Inact_Colour) 
      Selected[0][-1].goto(self.X, self.Y) #Highest disk of thelist 
      self.Disk_List.append(Selected[0][-1]) #Put the highest disk of the list in the correct place in the list 

      del Selected[0][-1] #del the disk from the tower and selected 
      del Selected[0] #del the lost from 'Selected',the list of the tower remains 
      del Selected[0] #del thebutton 

      Steps = Steps + 1 
+0

不會允許什麼樣的老師的課?沒有意義,因爲面向對象是一個有用的概念來理解... – mbomb007

+0

他沒有向我們解釋它,所以我們不希望使用它。如果我們這樣做,他會懷疑我們作弊或成爲滋擾,因爲我們需要讓我們接受我們任務的處方(這隻使用函數和烏龜圖形= p)。我也發現它更容易,但他讓我重新編碼它。 –

回答

1

你可以使用一個封蓋從你的類名綁定本地變量:

def make_movement(Properties): 
    X = Properties[0] * 20 
    Disk_List = Properties[4] 
    Act_Colour = 'white' 
    Inact_Colour = 'black' 

    def movement(x, y): 
     global Steps 
     Y = (Properties[1] + len(Disk_List)) * 20 + 45 

     if len(Selected) == 0 and len(Disk_List) > 0: 
      Properties[5].fillcolor(Act_Colour) 
      Selected.append(Disk_List) #Adds the list with disks to the selected tower 
      Selected.append(Properties[5]) #To reset the color of the botton 
     elif len(Selected) == 2 and len(Disk_List) > 0 and Selected[0][-1].shapesize()[1] == Disk_List[-1].shapesize()[1]: #To deselect toe button 
      Selected[1].fillcolor(Inact_Colour) 
      del Selected[0] 
      del Selected[0] 
     elif len(Selected) == 2 and (len(Disk_List) == 0 or Selected[0][-1].shapesize()[1] < Disk_List[-1].shapesize()[1]): #Lazy function 
      Selected[1].fillcolor(Inact_Colour) 
      Selected[0][-1].goto(X, Y) #Highest disk of thelist 
      Disk_List.append(Selected[0][-1]) #Put the highest disk of the list in the correct place in the list 

      del Selected[0][-1] #del the disk from the tower and selected     
      del Selected[0] #del the lost from 'Selected', the list of the tower remains 
      del Selected[0] #del thebutton 

      Steps = Steps + 1 

    return movement 

然後,調用make_movement(Properties)而不是Move(Properties).movement

def Tower(Properties): 
    Button = Properties[5] 
    Button.onclick(make_movement(Properties)) 
+0

我現在只能移動1個磁盤,然後按鈕變得無法點擊,我怎麼能解決這個問題? –

+0

這就是我用你最初的'Move'類得到的行爲,你的問題是關於「重新編碼爲defs」,這就是這個答案提出的。你可以仔細檢查一下,你的移動類在這裏粘貼實際上有效嗎? – cr3

+0

哎呦我是白癡哈哈,我在#後面有一行代碼。非常感謝您現在的工作:D –

0

你可以使用functools.partial來創建一個具有總是作爲參數傳遞的屬性的函數:

import functools 
def Tower(Properties): 
    Button = Properties[5] 
    Button.onclick(functools.partial(movement, Properties)) 

然後,重新定義movement爲需要Properties作爲第一個參數的函數:

def movement(Properties, x, y): 
    global Steps 
    X = Properties[0] * 20 
    Disk_List = Properties[4] 
    Act_Colour = 'white' 
    Inact_Colour = 'black' 
    Y = (Properties[1] + len(Disk_List)) * 20 + 45 

    if len(Selected) == 0 and len(Disk_List) > 0: 
     Properties[5].fillcolor(Act_Colour) 
     Selected.append(Disk_List) #Adds the list with disks to the selected tower 
     Selected.append(Properties[5]) #To reset the color of the botton 
    elif len(Selected) == 2 and len(Disk_List) > 0 and Selected[0][-1].shapesize()[1] == Disk_List[-1].shapesize()[1]: #To deselect toe button 
     Selected[1].fillcolor(Inact_Colour) 
     del Selected[0] 
     del Selected[0]   
    elif len(Selected) == 2 and (len(Disk_List) == 0 or Selected[0][-1].shapesize()[1] < Disk_List[-1].shapesize()[1]): #Lazy function 
     Selected[1].fillcolor(Inact_Colour) 
     Selected[0][-1].goto(X, Y) #Highest disk of thelist 
     Disk_List.append(Selected[0][-1]) #Put the highest disk of the list in the correct place in the list 

     del Selected[0][-1] #del the disk from the tower and selected 
     del Selected[0] #del the lost from 'Selected',the list of the tower remains 
     del Selected[0] #del thebutton 

     Steps = Steps + 1