python运用turtle 画出汉诺塔搬运过程

1.打开 IDLE 点击File-New File 新建立一个py文件

 

 

2.py文件中输入如下代码

 

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():  #这里是绘制三个塔柱子
    t = turtle.Turtle()
    t.hideturtle()
    def drawpole_1(k):
        t.up()
        t.pensize(10)
        t.speed(100)
        t.goto(400*(k-1), 300)
        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)
    drawpole_1(1)
    drawpole_1(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,20-i)
        plates[i].goto(-400,-90+20*i)
        plates[i].showturtle()
    return plates
def pole_stack(): #这里运用栈来控制一次只能搬动一个盘子并且递归
    poles=[Stack() for i in range(3)]
    return poles
def moveDisk(plates,poles,fp,tp): #搬动盘子
    mov=poles[fp].peek()
    plates[mov].goto((fp-1)*400,300)
    plates[mov].goto((tp-1)*400,300)
    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()

 

 

 

3. 点击Run-Run Module 或者F5 运行程序

 

这个时候会出现两个窗口,在左边窗口按照提示输入,右边则是turtle的画布

 

 

 

4 输入的数字越大,花的时间更久,然后要防止自己的电脑cpu过载!!

可自行输入别的数字

 

posted @ 2019-03-26 22:59  爱你一生12581  阅读(517)  评论(0编辑  收藏  举报