AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Hanoi towers recursion3/29/2023 ![]() A peg object might have attributes for its position and which disc objects are attached to it. Each disc might have attributes for its size and color along with which peg it is stacked. In the Tower of Hanoi an object oriented model might consist of a class for a disc and another for a peg. Some applications may have multiple views. The View outputs the model, either presenting a visual repretation, outputting information from the model, responding to signals from the Control or all of these. The Control is makes changes to the contents of the model and also passes the changing model along with other signals to the view. The Model is a structure for the data being manipulated by the program The MVC discipline strives to find separate responsibilities of a program into code modules that are independent and loosely linked with each other. And I think the problem is the difficulty in visualizing the nested context of the recursive calls. It is much better than the usual factorial example which does nothing more than use recursion to replace a simple for loop.īut students have a hard time getting it. I have tried a couple of times to teach the Hanoi puzzle as a good example of recursion. Here is a screen shot of the game in progress.Īnd here is a javascript animation. The other 99% of the program involves doing TK graphics to make it come alive. Guido's hanoi.py in the Python Demo area is a nice demonstration of this recursive process. Can't do 4 discs? Well then, move just 3. If you don't know how to move 5 discs from A to C (using B for intermediate storage), then just move 4 discs from A to B, one disk from A to C (this is really easy) and then move 4 discs from B to C. The problem is to move the discs, one at a time from peg to peg in such a way that this is always true and to finally end up with all of the discs on peg 3 in the original order. ![]() A disc is never stacked on top of a smaller disc. In the initial configuration all the discs are stacked on the first peg with the largest disc on the bottom and the smallest on top. Each disc can fit on any of 3 pegs and each peg is high enough to hold all the discs in a stack. There are a number of discs each with a hole in the center. The Tower of Hanoi is a classic game that is often emulated on computers to demonstrate recursion.
0 Comments
Read More
Leave a Reply. |