If you cannot make any moves, you may click the Restart button at the top left corner to reset the puzzle. The amount of time you have spent and the number of moves are both counted on the left of the screen, and your final score will be given based on these two factors. Note that only 1 ring can be moved at a time, and a ring with a larger number cannot be placed on top of a smaller ring, for example, ring 4 cannot be placed on top of ring 2. To move a ring, simply click the topmost ring on a pile and drag it to the target pile, then click the pile to place the ring. To successfully transfer all rings to pile C, you may make use of the empty pile B for better arrangement. Your chosen quantity of rings are stacked on pile A in ascending order of numbers from top to bottom, for example, ring 1 is put on the top while ring 5 is at the bottom. You will then be given a total of 3 piles, namely pile A, B and C from left to right. When you are satisfied with your choice, click the OK button to start playing. When the game begins, you may set the number of rings between 1 to 10 by clicking the up and down arrow buttons in the dialogue box. Then it can move disks from any of the three towers to any of the others.Utilize your precise organization skills to conquer the Tower of Hanoi! Your goal in this game is to move all rings from pile A to pile C and stack them according to the original order. I can have my method take in values just as the moveSingleDisc() method did. Note that I'm not confined to always moving discs from tower 0 to tower 1. The Tower of Hanoi (also called The problem of Benares Temple or Tower of Brahma or Lucas Tower and sometimes pluralized as Towers, or simply pyramid puzzle ) is a mathematical game or puzzle consisting of three rods and a number of disks of various diameters, which can slide onto any rod. You can try following these steps yourself using the applet above, or the alternate Towers of Hanoi applet. Then, in the third call all I had left to do was move the smaller disk back from tower 2 onto tower 1. #THE HANOI TOWERS FREE#Then in the second call to "moveSingleDisc" I was free to move the remaining disk on tower 0 (the larger disk) from tower 0 to tower 1. What did this do? I moved the smallest disk from tower 0 to tower 2, my spare tower, to get it out of the way. Of course, the larger disk can never be placed on top of the smaller disk. Consider a two-disk game were I want to move 2 disks from tower 0 onto tower 1. I can now use this method to write an algorithm for solving the Towers of Hanoi game. So, if I were to call moveSingleDisc(0, 1, 2) it would move the top disk from tower 0 onto the top of tower 1. This will take the top disk from the from tower, move it to the to tower, and leave the third tower spare unaffected. MoveSingleDisc(int from, int to, int spare) Also, suppose we have the following method: For this lab, assume that we're playing the Towers of Hanoi game with 3 towers labeled 0, 1, and 2. #THE HANOI TOWERS CODE#We'll do something similar here, we just won't type in any code to Eclipse. In previous labs we've been trying to get used to the idea of calling methods to get work done, often with those methods having been written by someone else. According to the legend, before the monks could make the final move to complete the new pile in the new location, the temple would turn to dust and the world would end.īelow is an applet program representing the task of moving a pile of disks from one tower to another. Using the intermediate location, the monks began to move disks back and forth from the original pile to the pile at the new location, always keeping the piles in order (largest on the bottom, smallest on the top). In addition, there was only one other location in the temple (besides the original and destination locations) sacred enough for a pile of disks to be placed there. A disk could not be placed on top of a smaller, less valuable disk. The disks were fragile only one could be carried at a time. In an ancient city, so the legend goes, monks in a temple had to move a pile of 64 sacred disks from one location to another. Using recursion to solve the Towers of Hanoi problem.<< Return to Homework Page Lab 9 : Recursion Lab Overview
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |