1/28/2024 0 Comments Hanoi towers code javaVar param = callStack.shift() // Get call parameters from callStack // Note: throughout the code, I use fromBar, toBar to refer to towers MoveDisk() // moveDisk takes its parameters from callStack The following listing shows the complete source code of my script.ĬallStack= // callStack array is global Now the modified Hanoi() function is called from the function executeHanoi() given below. This is essentially what I did, that is, in the above code comment the line moveDisk() and uncomment the line callStack.push(). Using a stack will also preserve the order of the calls. After some thought, I was asking myself: Why not use a stack (callStack) to save the "from,to" call-parameters to moveDisk(). But then how to find all these moveDisk() calls. In other words, every call to moveDisk() should issue (at the time when it is done) the next call to moveDisk(). But with JavaScript animation, somehow, we have to follow the pattern: animate moveDisk(), animate the next moveDisk(). It seems that JavaScript animation uses the pattern: if you start animation, do not do anything else.įor the tower-of-Hanoi algorithm, we want to animate one call to moveDisk(), return to the caller until we encounter the next call to moveDisk(), animate it, and so on. If we are to implement the delay by a spin-loop, it would bring things to a stand-still because the whole of JavaScript code executes on one thread. However, JavaScript lacks a "real" delay or sleep mechanism. One might suggest that the caller sleeps for a while to allow for animation to complete. A problem arises because code in the caller will continue executing and interfere (miss up shared data) with the animation. Suppose, in the above code, the call to the function moveDisk() is animated using setInterval(). Normally animation code can utilize JavaScript timer functions: setInterval() or setTimer(). Animation Handling in JavaScript is Problematic The above algorithm encodes the preceding description but with the parameters for the towers kept as variables instead of being specific values. After this, we can move the n-th disk from tower #0 to tower #2 and then move all the n-1 disks from the middle tower to the last tower using tower #0 as intermediate by the call Hanoi(n-1, 1,2,0). This moves the disks to the middle tower (#1) using the other tower (#2) as intermediate. The recursive algorithm for the tower of Hanoi is based on observing that the top n-1 disks at the "from" tower (together with the other two towers) represent a smaller-size instance of the original problem and, thus, can be solved by the call Hanoi(n-1, 0,1,2). Then express the solution for the original problem in terms of the solutions to the subproblems. Normally (if we denote the towers with 0,1,2), the initial call for n disks will be Hanoi(n,0,2,1).Ī recursive algorithm normally tries to find subproblems (instances of the original problem but with smaller problem size).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |