home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 170_01 / towers.c < prev    next >
Text File  |  1979-12-31  |  4KB  |  195 lines

  1. #include <stdio.h>
  2.  
  3. /*
  4.  *                 IBM RBBS-PC Tulsa, OK  
  5.  *                 Switching totally to the "C" Language
  6.  *                 24 Hour operation 300/1200 baud XMODEM
  7.  *                 918-664-8737
  8.  *                 SYSOP LYNN LONG
  9.  *
  10.  *                  Towers of Hanoi
  11.  *
  12.  * The object of the game is to transfer the disks from
  13.  * the leftmost tower to the rightmost tower obeying the
  14.  * following set of rules:
  15.  * 
  16.  * 1) Only the top disk of any tower may be moved at a time
  17.  * 2) At no time must a larger disk be placed on a smaller disk
  18.  *
  19.  */
  20.  
  21. #define FORMFEED        0x0c
  22. #define ESC                0x1b
  23.  
  24. #define POST            0xba
  25. #define POST_BASE        0xca
  26. #define BASE            0xcd
  27. #define RING            0xdc
  28.  
  29. #define SCREEN_WIDTH    80
  30. #define SCREEN_HEIGHT    25
  31.  
  32. #define RING_WIDTH        ((((SCREEN_WIDTH - 2)/3) & 0xfe)-1)
  33. #define LEFT_POST        (RING_WIDTH/2+1)
  34. #define CENTER_POST        (LEFT_POST+RING_WIDTH)
  35. #define RIGHT_POST        (LEFT_POST+2*RING_WIDTH)
  36.  
  37. #define MOVING_ROW        2
  38. #define BASE_ROW        15
  39. #define POST_HEIGHT        11
  40.  
  41. char top[3] { BASE_ROW-1, BASE_ROW-1, BASE_ROW-1 };
  42. int pause 0;
  43.  
  44.  
  45. main(argc, argv)
  46. int argc;
  47. char *argv[];
  48.     {
  49.     int nrings;
  50.  
  51.     if(argc < 1  ||  argc > 3)
  52.         abort("Use: hanoi [rings [delay]]\n");
  53.  
  54.     nrings = argc > 1 ? atoi(argv[1]) : 7;
  55.     pause = argc > 2 ? atoi(argv[2]) : 1;
  56.  
  57.     setup(nrings);
  58.     hanoi(nrings, 0, 2, 1);
  59.  
  60.     curse(0, SCREEN_HEIGHT-1);
  61.     }
  62.  
  63.  
  64. hanoi(n, a, b, c)
  65. char n, a, b, c;
  66.     {
  67.     if(n == 0)
  68.         return;
  69.  
  70.     hanoi(n-1, a, c, b);
  71.     movering(n, a, b);
  72.     hanoi(n-1, c, b, a);
  73.     }
  74.  
  75.  
  76. setup(n)
  77. char n;
  78.     {
  79.     char i;
  80.  
  81.     putchar(FORMFEED);
  82.  
  83.     for(i = MOVING_ROW+2; i < BASE_ROW; ++i) {
  84.         cput(LEFT_POST, i, POST);
  85.         cput(CENTER_POST, i, POST);
  86.         cput(RIGHT_POST, i, POST);
  87.         }
  88.  
  89.     curse(0, BASE_ROW);
  90.  
  91.     for(i = 1; i < SCREEN_WIDTH; ++i)
  92.         putchar(BASE);
  93.  
  94.     cput(LEFT_POST, BASE_ROW, POST_BASE);
  95.     cput(CENTER_POST, BASE_ROW, POST_BASE);
  96.     cput(RIGHT_POST, BASE_ROW, POST_BASE);
  97.  
  98.     for(i = n; i > 0; --i)
  99.         draw(i, LEFT_POST, top[0]--, RING);
  100.     }
  101.  
  102.  
  103. curse(x, y)
  104. char x, y;
  105.     {
  106.  
  107.     wait();
  108.  
  109.     putchar(ESC);
  110.     putchar('=');
  111.     putchar(y + ' ');
  112.     putchar(x + ' ');
  113.     }
  114.  
  115.  
  116. cput(x, y, ch)
  117. char ch;
  118. int x,y;
  119.     {
  120.     putchar(ESC);
  121.     putchar('=');
  122.     putchar(y + ' ');
  123.     putchar(x + ' ');
  124.     putchar(ch);
  125.     wait();
  126.     }
  127.  
  128.  
  129. draw(ring, centre, y, ch)
  130. char ring, centre, y, ch;
  131.     {
  132.     char i;
  133.  
  134.     curse(centre-ring, y);
  135.  
  136.     for(i=0; i<ring; ++i)
  137.         putchar(ch);
  138.  
  139.     curse(centre+1, y);
  140.  
  141.     for(i=0; i<ring; ++i)
  142.         putchar(ch);
  143.     }
  144.  
  145.  
  146. movering(ring, from, to)
  147. char ring, from, to;
  148.     {
  149.     char fromc, toc;
  150.     char fromy, toy;
  151.  
  152.     fromc = LEFT_POST + from * RING_WIDTH;
  153.     toc = LEFT_POST + to * RING_WIDTH;
  154.     fromy = ++top[from];
  155.     toy = top[to]--;
  156.  
  157.     while(fromy != MOVING_ROW) {
  158.         draw(ring, fromc, fromy, ' ');
  159.         draw(ring, fromc, --fromy, RING);
  160.         }
  161.  
  162.     if(fromc < toc)
  163.         while(fromc != toc) {
  164.             cput(fromc-ring, fromy, ' ');
  165.             cput(fromc, fromy, RING);
  166.             cput(fromc+1, fromy, ' ');
  167.             cput(fromc+ring+1, fromy, RING);
  168.             ++fromc;
  169.             }
  170.     else if (fromc > toc)
  171.         while(fromc != toc) {
  172.             cput(fromc+ring, fromy, ' ');
  173.             cput(fromc, fromy, RING);
  174.             cput(fromc-1, fromy, ' ');
  175.             cput(fromc-ring-1, fromy, RING);
  176.             --fromc;
  177.             }
  178.  
  179.     while(fromy != toy) {
  180.         draw(ring, fromc, fromy, ' ');
  181.         draw(ring, fromc, ++fromy, RING);
  182.         }
  183.     }
  184.  
  185.  
  186. wait() {
  187.     int i;
  188.  
  189.     i = 1 << pause;
  190.  
  191.     while(--i)
  192.         i = i;
  193.     }
  194.  
  195.