python繪制漢諾塔
更新時(shí)間:2021年03月01日 14:33:27 作者:一個(gè)超會(huì)寫B(tài)ug的安太狼
這篇文章主要為大家詳細(xì)介紹了python繪制漢諾塔,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(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("請(qǐng)輸入漢諾塔的層數(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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python卸載numpy出現(xiàn)WinError:拒絕訪問的解決方案
這篇文章主要介紹了python卸載numpy出現(xiàn)WinError:拒絕訪問的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08使用Python中的線程進(jìn)行網(wǎng)絡(luò)編程的入門教程
這篇文章主要介紹了使用Python中的線程進(jìn)行網(wǎng)絡(luò)編程的入門教程,本文來自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下2015-04-04Python爬蟲實(shí)現(xiàn)全國失信被執(zhí)行人名單查詢功能示例
這篇文章主要介紹了Python爬蟲實(shí)現(xiàn)全國失信被執(zhí)行人名單查詢功能,涉及Python爬蟲相關(guān)網(wǎng)絡(luò)接口調(diào)用及json數(shù)據(jù)轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05Python開發(fā)之迭代器&生成器的實(shí)戰(zhàn)案例分享
在 Python 中,迭代器和生成器都是用來遍歷數(shù)據(jù)集合的工具,可以按需逐個(gè)生成或返回?cái)?shù)據(jù),從而避免一次性加載整個(gè)數(shù)據(jù)集合所帶來的性能問題和內(nèi)存消耗問題。本文主要和大家分享幾個(gè)貼近實(shí)際運(yùn)維開發(fā)工作中的場景案例,希望對(duì)大家有所幫助2023-04-04Python讀取xlsx文件報(bào)錯(cuò):xlrd.biffh.XLRDError:?Excel?xlsx?file;no
這篇文章主要給大家介紹了關(guān)于Python庫xlrd中的xlrd.open_workbook()函數(shù)讀取xlsx文件報(bào)錯(cuò):xlrd.biffh.XLRDError:?Excel?xlsx?file;not?supported問題解決的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08