python繪制漢諾塔
本文實(shí)例為大家分享了python繪制漢諾塔的具體代碼,供大家參考,具體內(nèi)容如下
源碼:
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個(gè)盤子
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() # 確定移動(dòng)到底部的高度(恰好放在原來最上面的盤子上面)
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("請輸入漢諾塔的層數(shù)并回車:\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()
效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python3爬蟲學(xué)習(xí)之?dāng)?shù)據(jù)存儲txt的案例詳解
這篇文章主要介紹了python3爬蟲學(xué)習(xí)之?dāng)?shù)據(jù)存儲txt的案例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
全面剖析Python的Django框架中的項(xiàng)目部署技巧
這篇文章主要全面剖析了Python的Django框架的部署技巧,包括Fabric等自動(dòng)化部署和建立單元測試等方面,強(qiáng)烈推薦!需要的朋友可以參考下2015-04-04
Python實(shí)現(xiàn)獲取彈幕的兩種方式分享
彈幕可以給觀眾一種“實(shí)時(shí)互動(dòng)”的錯(cuò)覺,在相同時(shí)刻發(fā)送的彈幕基本上也具有相同的主題,在參與評論時(shí)就會(huì)有與其他觀眾同時(shí)評論的錯(cuò)覺。本文為大家總結(jié)了兩個(gè)Python獲取彈幕的方法,希望對大家有所幫助2023-03-03
Pandas之Dropna濾除缺失數(shù)據(jù)的實(shí)現(xiàn)方法
這篇文章主要介紹了Pandas之Dropna濾除缺失數(shù)據(jù)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
使用PySpark實(shí)現(xiàn)數(shù)據(jù)清洗與JSON格式轉(zhuǎn)換的實(shí)踐詳解
在大數(shù)據(jù)處理中,PySpark?提供了強(qiáng)大的工具來處理海量數(shù)據(jù),特別是在數(shù)據(jù)清洗和轉(zhuǎn)換方面,本文將介紹如何使用?PySpark?進(jìn)行數(shù)據(jù)清洗,并將數(shù)據(jù)格式轉(zhuǎn)換為?JSON?格式的實(shí)踐,感興趣的可以了解下2023-12-12
淺談pytorch grad_fn以及權(quán)重梯度不更新的問題
今天小編就為大家分享一篇淺談pytorch grad_fn以及權(quán)重梯度不更新的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python命令行參數(shù)解析模塊getopt使用實(shí)例
這篇文章主要介紹了Python命令行參數(shù)解析模塊getopt使用實(shí)例,本文講解了使用語法格式、短選項(xiàng)參數(shù)實(shí)例、長選項(xiàng)參數(shù)實(shí)例等內(nèi)容,需要的朋友可以參考下2015-04-04

