// hanoi.c  -- Hanoi tower, @CopyLeft by tsaiwn@csie.nctu.edu.tw
//Hanoi Tower(河內之塔) -- 神奇的 Recursive -- 自己叫自己 !
//關於 Hanoi Tower, 請用 http://gogle.com 打入 "hanoi tower"  查詢
//注意用雙引號夾住如 "hanoi tower" 查到的才是你要的 :-)
//或是直接看這  http://en.wikipedia.org/wiki/Tower_of_Hanoi
///
void moveOne(int px, int py, int ); //3rd parameter is the disk#
void hanoi(int n, int pa, int pc, int pb) {
    if(n==0) return;    // there is no disk
    hanoi(n-1, pa, pb, pc);  // 先幫我把上面 n-1 disks 搬走
    moveOne(pa, pc, n);   /// move the n-th disk from pa to pc
    hanoi(n-1, pb, pc, pa); // move n-1 disks from pb to pc
} // hanoi

#include <stdio.h>
void moveOne(int px, int py, int n ) {  // the n-th disk
   char *p123[ ] = {"th", "st", "nd", "rd"}; // -th, Fir-st, 
                      // ..Seco-nd, 3-rd
   int k = n%10;
   if(k!=1  && k!=2 && k!=3) k = 0;  // -th
   printf("Move %d-%s disk from peg-%c  to  peg-%c \n", 
                     n,p123[k], px, py);
}

int getInt( ) {   // static char buf[99];
   char buf[99]; // String buf = null;  //Java
                 // buf = br.readLine( );
   int ans=0;
   fgets(buf, sizeof(buf), stdin);  // buf = br.readLine( );
   ans=atoi(buf);  // ans = Integer.parseInt(buf);  
    // 如果 輸入 64; 你認為會做多久? ? ?
   return ans;  //若一秒只能搬一個動作, 
                // ..則需約 600 billion(六千億) years (580+億)
}

int main( ) {
    int n; 
    while( 38==38 ) {
       printf("Hanoi tower\r\n Enter negative number to stop.");
       fprintf(stderr, "\r\n Enter height n=? ");
       n = getInt( );  // read an int into n
       printf("\r\n");
       if(n<=0) break;
       hanoi(n, 'A', 'C', 'B'); // move n disks on peg 'A' to peg 'C'
    } // while
    fprintf(stderr, "Thanks ... hit ENTER key..");
    getchar( );
    return 0;
}
