/* * 'Play' Hanoi towers game */ #include static int nmoves = 0; void hanoi (int n, char frompeg, char topeg, char auxpeg); int main (void) { int n = 4; hanoi (n, 'A', 'B', 'C'); printf ("%d moves\n", nmoves); return 0; } void hanoi (int n, char frompeg, char topeg, char auxpeg) { if (n == 1) { printf ("move from %c to %c\n", frompeg, topeg); nmoves += 1; } else { hanoi (n - 1, frompeg, auxpeg, topeg); hanoi (1, frompeg, topeg, auxpeg); hanoi (n - 1, auxpeg, topeg, frompeg); } }