在蟒蛇与乌龟图形移动磁盘的河内塔

时间:2016-01-05 18:36:28

标签: python list function turtle-graphics towers-of-hanoi

我制作了游戏:Python中的河内塔用龟图形,因为这是我的学校作业。

#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

2 个答案:

答案 0 :(得分: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))

答案 1 :(得分:0)

您可以使用functools.partial创建一个函数,其中Properties始终作为参数传递:

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