![]() ![]() I resisted the temptation to use a conditional in two other instances - when checking whether the smallest disk wraps around when being moved and when selecting the two rods to use in moving the alternate disk. This is a simple IF ELSE expression in a single line. The conditional (ternary) statement appears just once: to set the direction to move the smallest disk in the puzzle. ![]() The more commonly used MOD operator (%) has replaced the bitwise AND operator (&). This same odd/even test is also used at the beginning of the program to set the value of the variable which determines the direction of movement for the smallest disk. The Boolean flag variable, everyOtherMove, which controlled the alternating moves has been dropped the alternating moves are now handled by simply testing the odd/even condition of the variable moveCount. Vertical whitespace, mostly in the form of blank lines, has been reduced. Loop index variables are declared within the loop itself. Variables are declared just before use (instead of at the beginning of the program). The following changes, suggested by Caridorc, have been included in this version of the program: If ( topDisk > topDisk ) while (! isSolved(rod, numberOfDisks)) Int destinationCount (int array, int numberOfElements) MATHEMATICAL PUZZLES & DIVERSIONS by Martin Gardner, (Simon and Schuster, 1959), There are an odd number of disks in the puzzle clockwise if there are an evenįrom "The Icosian Game and the Tower of Hanoi" in THE SCIENTIFIC AMERICAN BOOK OF The smallest disk always moves in the same direction: counter-clockwise if Which does not involve the smallest disk. Place one of the three rods upright at each corner of a triangle.Īlternate between moving the smallest disk and making the only valid move Make alternating move) into functions the handling of variables becomes Second when I try to make the two primary routines (move smallest disk and Only valid move which does not involve the smallest disk. The program produces the correct results but I have two questions.įirst is there simpler way to write the alternating step of determining the Uppermost disk(smallest one) of poleC is being moved to poleB.Then,uppermost disk(largest one) of poleA is being moved to poleC.Īgain,uppermost disk(smallest one) of poleB is being moved to poleA.Also uppermost disk(second smallest one) of poleB is being moved to poleC.įinally,all the disks is being moved to poleC.I came across an interesting method of solution for the Tower of Hanoi puzzleĪnd wrote a short version of it as a programming exercise. Now uppermost disk(second smallest one) of poleA is being moved to poleB. first pole is A,second pole is B and third pole is Cįirstly all the disks is in poleA and then the uppermost disk(smallest one) is being moved to poleC. ![]() Since this is a recursive approach so,it is quite difficult to understand this by just reading the algorithm so,let's take an example to have a better idea of the solution.
0 Comments
Leave a Reply. |