Advertisement
Guest User

TowersOfHanoi

a guest
Sep 8th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. #define KEY_ESCAPE      27
  5. #define KEY_ARROW       224
  6. #define KEY_UP          72
  7. #define KEY_DOWN        80
  8. #define KEY_LEFT        75
  9. #define KEY_RIGHT       77
  10.  
  11. #define DIR_LEFT        -1
  12. #define DIR_RIGHT       1
  13.  
  14. #define INSTRUCTIONS    " =====\t Towers of Hanoi \t===== \n"\
  15.                         " Use arrow keys to control\n"\
  16.                         " U/D: Change block\n"\
  17.                         " L/R: Move selected block left or right\n\n"\
  18.                         " Move all blocks to another tower to win!\n\n"
  19.  
  20. #define INVALID_MOVE    " Invalid move!\n"
  21.  
  22. const char TOWER_ITEM[5][10] = {
  23.     "    =    ",
  24.     "   ===   ",
  25.     "  =====  ",
  26.     " ======= ",
  27.     "    |    "
  28. };
  29. int BLOCK[4];
  30.  
  31. void DrawTowers(int selection) {
  32.     int i, j;
  33.  
  34.     for (i = 0; i < 4; i++) {
  35.         for (j = 0; j < 3; j++) {
  36.             printf("%s", TOWER_ITEM[4]);
  37.         }
  38.         printf("\n");
  39.  
  40.         for (j = 0; j < 3; j++) {
  41.             if (BLOCK[i] == j) {
  42.                 printf("%s", TOWER_ITEM[i]);
  43.             }
  44.             else {
  45.                 printf("%s", TOWER_ITEM[4]);
  46.             }
  47.         }
  48.         printf("\n");
  49.     }
  50.  
  51.     printf(" Current Block: %i\n\n", (selection + 1));
  52. }
  53.  
  54. void MoveBlock(int block, int direction) {
  55.     int limit;
  56.     limit = (direction == DIR_LEFT) ? (0) : (2);
  57.  
  58.     if (BLOCK[block] == limit) {
  59.         printf("%s", INVALID_MOVE);
  60.     }
  61.     else if (block != 0) {
  62.         int i, clear;
  63.         clear = 1;
  64.  
  65.         for (i = block; i != 0; i--) {
  66.             if ((BLOCK[i - 1] == BLOCK[block]) || (BLOCK[i - 1] == (BLOCK[block] + direction))) {
  67.                 clear = 0;
  68.                 break;
  69.             }
  70.         }
  71.  
  72.         if (clear == 1) {
  73.             BLOCK[block] += direction;
  74.         }
  75.         else {
  76.             printf("%s", INVALID_MOVE);
  77.         }
  78.     }
  79.     else {
  80.         BLOCK[block] += direction;
  81.     }
  82. }
  83.  
  84. bool CheckWin() {
  85.     bool    result;
  86.     int     i, tower;
  87.  
  88.     result  = false;
  89.     tower   = BLOCK[0];
  90.  
  91.     if (tower != 0) {
  92.         result = true;
  93.         for (i = 1; i < 4; i++) {
  94.             if (BLOCK[i] != tower) {
  95.                 result = false;
  96.                 break;
  97.             }
  98.         }
  99.     }
  100.  
  101.     return result;
  102. }
  103.  
  104. int main() {
  105.     int key, currBlock;
  106.    
  107.     BLOCK[0] = 0;
  108.     BLOCK[1] = 0;
  109.     BLOCK[2] = 0;
  110.     BLOCK[3] = 0;
  111.     currBlock = 0;
  112.    
  113.     printf("%s", INSTRUCTIONS);
  114.     DrawTowers(currBlock); 
  115.    
  116.     while (1) {
  117.         key = _getch();
  118.  
  119.         if (key == KEY_ESCAPE) {
  120.             break;
  121.         }
  122.         else if (key == KEY_ARROW) {
  123.             key = _getch();
  124.             switch (key) {
  125.                 case KEY_UP:
  126.                     currBlock = (currBlock != 0) ? (currBlock - 1) : (0);
  127.                     break;
  128.                 case KEY_DOWN:
  129.                     currBlock = (currBlock != 3) ? (currBlock + 1) : (3);
  130.                     break;
  131.                 case KEY_LEFT:
  132.                     MoveBlock(currBlock, DIR_LEFT);
  133.                     break;
  134.                 case KEY_RIGHT:
  135.                     MoveBlock(currBlock, DIR_RIGHT);
  136.                     break;
  137.             }
  138.             DrawTowers(currBlock);
  139.  
  140.             if (CheckWin()) {
  141.                 printf(" YOU WON!\n Press any key to exit...");
  142.                 _getch();
  143.                 break;
  144.             }
  145.         }
  146.     }
  147.  
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement