Question: Create a graphical user interface ( GUI ) that demonstrates the steps of the Tower of Hanoi problem as the discs are moved from the
Create a graphical user interface GUI that demonstrates the steps of the Tower of Hanoi problem as the discs are moved from the starting peg to the target peg. The GUI should allow the user to specify the number of discs and the speed at which the discs move.
I have the code for the towers of hanoi solution
# import time for speed control
import time
# import os for terminal control
import os
class TOWEROFHANOI:
def initself numdiscs startpeg A targetpeg B auxpeg C: # O
self.numdiscs numdiscs
self.moves # Store moves for visualization
self.start startpeg # save peg naemes
self.target targetpeg
self.aux auxpeg
self.pegnames startpeg, auxpeg, targetpeg
self.towers None
# solve the toh problem via iteration
def solveviaiterationself: # On
self.moves # reset moves
n self.numdiscs
if n :
return
# create a stack for simulating
# elements are number of discs, start peg, target peg, auxiliary peg, is done flag
stack n False
# while the stack discs exists
while stack:
# pop first element from the stack
numdiscs, start, target, aux, done stack.pop
# base case when only disc
if numdiscs :
# move disc to final target
self.moves.append self.pegnamesstart self.pegnamestarget
continue
# while not done with the sorting
if not done:
# append the next moves onto the stack
stack.appendnumdiscs, start, target, aux, True # current is as done
stack.appendnumdiscs start, aux, target, False # move n discs from start to aux
else:
# save the move
self.moves.appendnumdiscs, self.pegnamesstart self.pegnamestarget
# append the next moves onto the stack
stack.appendnumdiscs aux, target, start, False # move n discs from aux to target
# solve the toh problem via recursion
def solveviarecursionself: # base function is O but calls a On function
self.moves # reset moves
# use the cb function to populate moves
self.recursivesolutioncbselfnumdiscs, self.pegnames self.pegnames self.pegnames
def recursivesolutioncbself n start, target, aux: # On
# base case of no discs
if n :
return
# move from start peg to aux peg
self.recursivesolutioncbn start, aux, target
# save move
self.moves.appendn start, target
# move from aux peg to target peg
self.recursivesolutioncbn aux, target, start
# function to play the animation in the terminal time
# parameter is the speed how many seconds between each frame
def playself speed:
# function is used to simulate the towers of hanoi problem
# it needs the instructions saved in self.moves to work properly
# this function cannot solve the problem on its own however it uses
# the moves another function found as a solution and simulates it prints it to the terminal
# reset towers
self.towers None # populate towers based off of moves
self.towers for in range self.numdiscs
# populate first move
self.towersx for x in rangeselfnumdiscs,
# variable to save prev towers
prevtowers None
# print turn
self.printtoterminal
# variable to save current turn number
turn
# for loop for each move
for i in rangelenselfmoves:
# the start and target of each move
start self.movesi
target self.movesi
# for the second move, third move, etc
# when a prev towers array has been saved
if prevtowers:
# get the disc from the start peg
if start self.start:
disc prevtowerspop
elif start self.aux:
disc prevtowerspop
else:
disc prevtowerspop
# move the disc to the target peg
if target self.start:
prevtowersappenddisc
elif target self.aux:
prevtowersappenddisc
else:
prevtowersappenddisc
self.towersi prevtowers
else:
# for the f
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
