自己
自己动手做了一个汉诺塔的手把手指导的图形界面软件,看起来不是非常高大上。不过复习了递归调用,学习了python的图形界面。以及多次尝试与错误之后,学会了怎么解汉诺塔。话不多说了,直接上代码。
语言环境: python3.5.2
from tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidges()
def createWidges(self):
self.label = Label(text="How many plates are there of hanoi? number: ")
self.numInput = Entry()
self.resolveButton = Button(self, text='RESOLVE', command=self.resolve)
self.resultOutput = Text(self)
self.label.pack(side=LEFT)
self.numInput.pack()
self.resolveButton.pack()
self.resultOutput.pack()
def resolve(self):
self.count = 0
self.resultOutput.delete(0.0, END)
num = int(self.numInput.get() or '3')
self.moveUI(num, 'A', 'B', 'C')
def moveUI(self, n , a, b, c):
if n == 1:
self.count += 1
self.resultOutput.insert(INSERT, '第' + str(self.count) + '步: ' + a + '->' +c + '\n')
return
self.moveUI(n-1, a, c, b)
self.moveUI(1, a, b, c)
self.moveUI(n-1, b, a, c)
return
app = Application()
app.master.title('Hanoi Solver')
app.mainloop()