ScratchData LogoScratchData
Back to beckerb4's profile

TowersOfHanoi

BEbeckerb4•Created September 7, 2008
TowersOfHanoi
0
0
75 views
View on Scratch

Description

Attempt a recursive Towers of Hanoi program. Fails because scratch does not support reentrance from broadcast and wait if you call the same message before the last call completed. The typical implementation is a 5 line recusive algorithm: hanoi(n,A,B,C) if (n>=1) { hanoi(n-1, A, C, B); doMove(A, B); hanoi(n-1, C, B, A); } The first problem I hist was that I would have to maintain my own call stack with lists for every argument. It would be nice if scratch allowed parameterized procedures (i.e. custom blocks). But even when I had done this, was hit the problem that my recursive calls never completed (the "backFromBroad" sound never plays). I do not want to make the effort to rewrite with tail recursion and convert to iteration, since I believe the recursive solution is more elegant. I do not see how the workaround (described above)of blocking the call until the message block completes is going to help - because it will just lead to deadlock. Any other suggestions? Here is my recursive method (with simulated callstack using lists).

Project Details

Project ID260239
CreatedSeptember 7, 2008
Last ModifiedSeptember 7, 2008
SharedSeptember 7, 2008
Visibilityvisible
CommentsAllowed