/*
 * hanoi-r.c
 * File revision 0
 * Recursive implementation of the Towers of Hanoi problem.
 * (c) 2000 Jacob Lundberg, jacob@chaos2.org
 */


/*
 * 2000.10.12	original framework
 */


#include <stdio.h>
#include <stdlib.h>


/* List the moves as they occur? */
#define  OUTPUT         0


void _hanoi(int A, int B, int C, int n);
void _move(int A, int B);


int main(int argc, char **argv) {
/*
 * main()
 * Set up and call _hanoi().
 */

   /* how many disks */
   int lvl = 0;

   /* maybe say which alg we're using */
   if(OUTPUT) printf("hanoi: starting recursive\n\n");

   /* act accordingly */
   if(argc > 1) {
      lvl = atoi(argv[1]);
      if(lvl < 1) return 1;
      _hanoi(1, 2, 3, lvl);
   } else {
      printf("You must enter a number of disks on the command line.\n");
   }
   return(0);

}


void _hanoi(int A, int B, int C, int n) {
/*
 * _hanoi()
 * Recurse and solve a Towers of Hanoi stack.
 */

   if(n == 1) {
      _move(A, C);
   } else {
      _hanoi(A, C, B, n-1);
      _move(A, C);
      _hanoi(B, A, C, n-1);
   }

}


void _move(int A, int B) {
/*
 * _move()
 * Perform whatever form of movement accounting is desired.
 */

   /* compiler will optimize this if(constant) away */
   if(OUTPUT) {
      printf("Move disk from tower %i to tower %i.\n", A, B);
   }

}

