| 1 |
// towers of annoy : non-sonified version |
| 2 |
// demonstrates: recursion |
| 3 |
|
| 4 |
// number of disks |
| 5 |
10 => int disks; |
| 6 |
// step number |
| 7 |
0 => int STEPS; |
| 8 |
|
| 9 |
// moves |
| 10 |
0 => int moves; |
| 11 |
|
| 12 |
// the hanoi |
| 13 |
fun void hanoi( int num, int src, int dest, int other ) |
| 14 |
{ |
| 15 |
// stop here |
| 16 |
if( num <= 0 ) return; |
| 17 |
|
| 18 |
// increment steps |
| 19 |
STEPS++; |
| 20 |
|
| 21 |
// move all except the biggest |
| 22 |
hanoi( num - 1, src, other, dest ); |
| 23 |
// move the biggest |
| 24 |
<<< "step", STEPS, " | move disk from peg", src, " -> ", "peg", dest >>>; |
| 25 |
// move onto the biggest |
| 26 |
hanoi( num - 1, other, dest, src ); |
| 27 |
} |
| 28 |
|
| 29 |
// start it |
| 30 |
hanoi( disks, 1, 3, 2 ); |
| 31 |
|
| 32 |
<<< "done.", "" >>>; |