1. 程式人生 > >python海龜turtle遞迴 漢諾塔的動畫實現

python海龜turtle遞迴 漢諾塔的動畫實現

這裡僅限制7階漢諾塔,不過稍微改一下整體引數還是可以做到“任意”階數的。

主要用了遞迴和棧的想法,用turtle實現。

import turtle
class Stack:
    def __init__(self):
        self.items = []
    def isEmpty(self):
        return len(self.items) == 0
    def push(self, item):
        self.items.append(item)
    def pop(self):
        return self.items.pop()
    def peek(self):
        if not self.isEmpty():
            return self.items[len(self.items) - 1]
    def size(self):
        return len(self.items)
def drawpole_3():#畫出漢諾塔的poles
    t = turtle.Turtle()
    t.hideturtle()
    def drawpole_1(k):
        t.up()
        t.pensize(10)
        t.speed(100)
        t.goto(400*(k-1), 100)
        t.down()
        t.goto(400*(k-1), -100)
        t.goto(400*(k-1)-20, -100)
        t.goto(400*(k-1)+20, -100)
    drawpole_1(0)#畫出漢諾塔的poles[0]
    drawpole_1(1)#畫出漢諾塔的poles[1]
    drawpole_1(2)#畫出漢諾塔的poles[2]
def creat_plates(n):#製造n個盤子
    plates=[turtle.Turtle() for i in range(n)]
    for i in range(n):
        plates[i].up()
        plates[i].hideturtle()
        plates[i].shape("square")
        plates[i].shapesize(1,8-i)
        plates[i].goto(-400,-90+20*i)
        plates[i].showturtle()
    return plates
def pole_stack():#製造poles的棧
    poles=[Stack() for i in range(3)]
    return poles
def moveDisk(plates,poles,fp,tp):#把poles[fp]頂端的盤子plates[mov]從poles[fp]移到poles[tp]
    mov=poles[fp].peek()
    plates[mov].goto((fp-1)*400,150)
    plates[mov].goto((tp-1)*400,150)
    l=poles[tp].size()#確定移動到底部的高度(恰好放在原來最上面的盤子上面)
    plates[mov].goto((tp-1)*400,-90+20*l)
def moveTower(plates,poles,height,fromPole, toPole, withPole):#遞迴放盤子
    if height >= 1:
        moveTower(plates,poles,height-1,fromPole,withPole,toPole)
        moveDisk(plates,poles,fromPole,toPole)
        poles[toPole].push(poles[fromPole].pop())
        moveTower(plates,poles,height-1,withPole,toPole,fromPole)

myscreen=turtle.Screen()
drawpole_3()
n=int(input("請輸入漢諾塔的層數並回車:\n"))
plates=creat_plates(n)
poles=pole_stack()
for i in range(n):
    poles[0].push(i)
moveTower(plates,poles,n,0,2,1)
myscreen.exitonclick()