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

/* Solves Towers of Hanoi Problem: Initially
   all discs are on peg A. Purpose is to move
   them to peg B. Rules are:
   1. Move only one disc at a time.
   2. A disc must never have a smaller one beneath it.
   See http://geminga.it.nuigalway.ie/~gettrick/courses/CT102/hanoi.c
   michael.mcgettrick@nuigalway.ie
*/

int hanoi(int a, char x, char y, char z)
{
if (a==1)
printf("move top disc from peg %c to peg %c\n",x,y);
else
{
hanoi(a-1,x,z,y);
hanoi(1,x,y,z);
hanoi(a-1,z,y,x);
}
}

main(int argc, char **argv)
{
int d=0;
char p='A', q='B', r='C';
printf("\n see: http://geminga.it.nuigalway.ie/~gettrick/courses/CT102/hanoi.c\n");
if(argc!=2)
{
printf("need 1 argument: number of discs on peg A\n");
printf("e.g. try typing in\n\n %s 3 \n\nfor 3 disc solution\n",argv[0]);
exit(1);
}
d=atoi(argv[1]);
printf("Towers of Hanoi Solution for n=%i discs\n\n",d);
hanoi(d,p,q,r);
}

