Advertisement
Monika__

Hanoi Tower

Mar 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.12 KB | None | 0 0
  1. #include "primlib.h"
  2. #define NUMBER_OF_RODS 5
  3. #define HEIGHT_OF_GROUND  (screenHeight() - WIDTH_GROUND)
  4. #define WIDTH_GROUND 50
  5. #define WIDTH_OF_ROD 5
  6. #define NUMBER_OF_DISKS 9
  7. #define STEP 2
  8. #define DISKS_WIDTH 10
  9. #define HEIGHT_OF_ROD 200
  10. #define DISKS_HEIGHT ((HEIGHT_OF_ROD - 15) / NUMBER_OF_DISKS)
  11. #define FRAME_COLOR BLACK
  12. #define DISKS_COLOR BLUE
  13. #define ROD_COLOR RED
  14. #define TOWERS_COLOR GREEN
  15.  
  16. int gameArray[NUMBER_OF_DISKS + 1][NUMBER_OF_RODS];
  17. int howManyDisksArr[NUMBER_OF_RODS];
  18.  
  19. double DistanceBetweenRods();
  20.  
  21. void DrawRods();
  22.  
  23. void DrawDisks();
  24.  
  25. void Draw();
  26.  
  27. void InitialGame();
  28.  
  29. int FindingTopDisc(int tower);
  30.  
  31. void MovingConditions();
  32.  
  33. int Win();
  34.  
  35. void HowManyDisks(int tower);
  36.  
  37. void Animation(int startTower, int stopTower, int startRow, int stopRow);
  38.  
  39. void MovingUp(int position_x, int position_y, int diskValue, int stopTower, int direction);
  40.  
  41. void MovingHorizontally(int position_x, int position_y, int stopTower, int diskValue, int direction);
  42.  
  43. void MovingDown(int position_x, int position_y, int stopTower, int diskValue);
  44.  
  45. int main()
  46. {
  47.     if(initGraph())
  48.     {
  49.         exit(3);
  50.     }
  51.  
  52.     InitialGame();
  53.  
  54.     while(1){
  55.  
  56.         filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
  57.         filledRect(0, screenHeight(), screenWidth(), screenHeight() - 50 , YELLOW);
  58.         DrawRods();
  59.         DrawDisks();
  60.         updateScreen();
  61.  
  62.  
  63.         if(Win() == 1)
  64.         {
  65.             break;
  66.         }
  67.         MovingConditions();
  68.         updateScreen();
  69.         if (isKeyDown(SDLK_SPACE) == 1){
  70.             break;
  71.         }
  72.     }
  73.  
  74.         SDL_Delay(40);
  75.  
  76.     return 0;
  77. }
  78.  
  79. void Draw()
  80. {
  81.   filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
  82.   filledRect(0, screenHeight(), screenWidth(), screenHeight() - 50 , YELLOW);
  83.   DrawRods();
  84.   DrawDisks();
  85.  
  86. }
  87.  
  88. double DistanceBetweenRods()
  89. {
  90.     return (screenWidth()/(NUMBER_OF_RODS + 1));
  91. }
  92.  
  93. void DrawRods()
  94. {
  95.     int column;
  96.     for(column = 0; column < NUMBER_OF_RODS; column++){
  97.  
  98.       filledRect((column * 2 + 1) * screenWidth() / (NUMBER_OF_RODS * 2) - WIDTH_OF_ROD / 2,
  99.                   screenHeight() - ((NUMBER_OF_DISKS + 1) * DISKS_HEIGHT + WIDTH_GROUND),
  100.                   (column * 2 + 1) * screenWidth() / (NUMBER_OF_RODS * 2) + WIDTH_OF_ROD / 2,
  101.                   screenHeight() - WIDTH_GROUND - 1, TOWERS_COLOR);
  102.         /*filledRect((DistanceBetweenRods() * iterationNumber) - WIDTH_OF_ROD, HEIGHT_OF_GROUND - 1,
  103.         (DistanceBetweenRods() * iterationNumber) + WIDTH_OF_ROD, HEIGHT_OF_GROUND - HEIGHT_OF_ROD , ROD_COLOR);
  104.     */}
  105. }
  106.  
  107. void DrawDisks()
  108. {
  109.     int row, column, individualWidth;
  110.     for(column = 0; column < NUMBER_OF_RODS; column++){
  111.       for(row = 0; row < NUMBER_OF_DISKS; row++){
  112.                 individualWidth = DISKS_WIDTH * gameArray[row][column];
  113.         if(gameArray[row][column] != 0){
  114.                     if(individualWidth > ((screenWidth() / NUMBER_OF_RODS) - WIDTH_OF_ROD) / 2){
  115.                         individualWidth = ((screenWidth() / NUMBER_OF_RODS) - WIDTH_OF_ROD) / 2;
  116.                     }
  117.           filledRect(screenWidth() / (NUMBER_OF_RODS * 2) * ( 1 + 2 * column) - individualWidth - WIDTH_OF_ROD / 2,
  118.                     //(DistanceBetweenRods() * (column + 1)) - (DISKS_WIDTH * gameArray[row][column]),
  119.                                         HEIGHT_OF_GROUND - (NUMBER_OF_DISKS - row) * DISKS_HEIGHT,
  120.                                         //screenHeight() - (NUMBER_OF_DISKS - row) * DISKS_HEIGHT - WIDTH_GROUND,
  121.                     //                   screenHeight()-(NUMBER_OF_DISKS-row)*DISKS_HEIGHT- WIDTH_GROUND,
  122.                     screenWidth() / (NUMBER_OF_RODS * 2) * (1+2*column)+ individualWidth + WIDTH_OF_ROD/2,
  123.                     //(DistanceBetweenRods() * (column + 1)) + (DISKS_WIDTH * gameArray[row][column]),
  124.                     HEIGHT_OF_GROUND - ((NUMBER_OF_DISKS - row - 1) * DISKS_HEIGHT) - 1, DISKS_COLOR);
  125.                     //                   screenHeight()-((NUMBER_OF_DISKS-row-1)*DISKS_HEIGHT)- WIDTH_GROUND -1, DISKS_COLOR);
  126.  
  127.           rect(screenWidth()/(NUMBER_OF_RODS*2)*(1+2*column)-DISKS_WIDTH * gameArray[row][column]-WIDTH_OF_ROD/2,
  128.           //(DistanceBetweenRods() * (column + 1)) - (DISKS_WIDTH * gameArray[row][column]),
  129.           HEIGHT_OF_GROUND - (NUMBER_OF_DISKS - row) * DISKS_HEIGHT,
  130.           //              screenHeight()-(NUMBER_OF_DISKS-row)*DISKS_HEIGHT- WIDTH_GROUND,
  131.           screenWidth()/(NUMBER_OF_RODS*2)*(1+2*column)+DISKS_WIDTH * gameArray[row][column]+WIDTH_OF_ROD/2,
  132.           //              (DistanceBetweenRods() * (column + 1)) + (DISKS_WIDTH * gameArray[row][column]),
  133.           HEIGHT_OF_GROUND - ((NUMBER_OF_DISKS - row - 1) * DISKS_HEIGHT) - 1, FRAME_COLOR);
  134.           //              screenHeight()-((NUMBER_OF_DISKS-row-1)*DISKS_HEIGHT)- WIDTH_GROUND -1, FRAME_COLOR);
  135.  
  136.     }
  137.   }
  138. }
  139.  
  140.  
  141. }
  142.  
  143. void InitialGame()
  144. {
  145.   int i, j;
  146.   for(i = 0; i < NUMBER_OF_DISKS; i++)
  147.   {
  148.     for(j = 0; j < NUMBER_OF_RODS; j++)
  149.     {
  150.       if(j==0){
  151.         gameArray[i][j] = i + 1;
  152.       } else {
  153.         gameArray[i][j] = 0;
  154.       }
  155.     }
  156.   }
  157. }
  158.  
  159. int FindingTopDisc(int tower)
  160. {
  161.     int row, topDisc;
  162.     for(row = NUMBER_OF_DISKS; row >= 0; row--)
  163.     {
  164.         if(gameArray[row][tower] != 0)
  165.         {
  166.             topDisc = row;
  167.         }
  168.     }
  169.     return topDisc;
  170. }
  171.  
  172. void MovingConditions()
  173. {
  174.   int startTower, stopTower, startRow, stopRow;
  175.   startTower = getkey() - '1';
  176.   stopTower = getkey() - '1';
  177.   if((startTower < NUMBER_OF_RODS) && (startTower >= 0) && (startTower != stopTower) &&
  178.      (stopTower >= 0) && (stopTower < NUMBER_OF_RODS))
  179.   {
  180.     startRow = NUMBER_OF_DISKS;
  181.     stopRow = NUMBER_OF_DISKS;
  182.     gameArray[NUMBER_OF_DISKS][startTower] = NUMBER_OF_DISKS;
  183.     gameArray[NUMBER_OF_DISKS][stopTower] = NUMBER_OF_DISKS;
  184.  
  185.     startRow = FindingTopDisc(startTower);
  186.     stopRow = FindingTopDisc(stopTower);
  187.  
  188.   if((startRow != NUMBER_OF_DISKS) && (gameArray[startRow][startTower] <= gameArray[stopRow][stopTower]))
  189.   {
  190.     Animation(startTower, stopTower, startRow, stopRow - 1 );
  191.   }
  192. }
  193. }
  194.  
  195. int Win()
  196. {
  197.   if(gameArray[0][NUMBER_OF_RODS - 1] == 1){
  198.     textout((screenWidth()/2) - 100, (screenHeight()/2) - 50, "YOU WON", RED);
  199.     updateScreen();
  200.     getkey();
  201.     return 1;
  202.   }
  203.   return 0;
  204. }
  205.  
  206. void HowManyDisks(int tower)
  207. {
  208.   int row;
  209.   howManyDisksArr[tower] = 0;
  210.   for(row = 0; row < NUMBER_OF_DISKS; row++)
  211.   {
  212.     if(gameArray[row][tower] != 0)
  213.     {
  214.       howManyDisksArr[tower] += 1;
  215.     }
  216.   }
  217. }
  218.  
  219. void Animation(int startTower, int stopTower, int startRow, int stopRow)
  220. {
  221.     int position_y, position_x, diskValue, direction;
  222.  
  223.     HowManyDisks(startTower);
  224.  
  225.     diskValue = gameArray[startRow][startTower];
  226.     gameArray[startRow][startTower] = 0;
  227.  
  228.     position_y = screenHeight() - (WIDTH_GROUND + DISKS_HEIGHT * howManyDisksArr[startTower]);
  229.     position_x = (startTower * 2 + 1) * screenWidth() / (NUMBER_OF_RODS * 2);
  230.  
  231.     if(startTower < stopTower)
  232.   {
  233.         direction = 1;
  234.   }
  235.   if(startTower > stopTower)
  236.     {
  237.     direction = -1;
  238.   }
  239.   MovingUp( position_x, position_y, diskValue, stopTower, direction);
  240.     HowManyDisks(stopTower);
  241.  
  242.   gameArray[stopRow][stopTower] = diskValue;
  243. }
  244.  
  245. void MovingUp(int position_x, int position_y, int diskValue, int stopTower, int direction)
  246. {
  247.   while((position_y + STEP) >= (screenHeight() - (WIDTH_GROUND + (NUMBER_OF_DISKS + 4) * DISKS_HEIGHT)))
  248.     {
  249.     Draw();
  250.     filledRect(position_x - (WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
  251.     position_y, position_x + (WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
  252.     position_y + DISKS_HEIGHT, DISKS_COLOR);
  253.     rect(position_x - (WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH), position_y,
  254.     position_x + (WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH), position_y + DISKS_HEIGHT, FRAME_COLOR);
  255.  
  256.     position_y -= STEP;
  257.     updateScreen();
  258.     }
  259.     MovingHorizontally(position_x, position_y, stopTower, diskValue, direction);
  260. }
  261.  
  262. void MovingHorizontally(int position_x, int position_y, int stopTower, int diskValue, int direction)
  263. {
  264.   while(abs(position_x - (stopTower * 2 + 1) * screenWidth() / (NUMBER_OF_RODS * 2)) >= STEP)
  265.         {
  266.         Draw();
  267.         filledRect(position_x - (WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH), position_y,
  268.     position_x + (WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
  269.     position_y + DISKS_HEIGHT, DISKS_COLOR);
  270.         rect(position_x - (WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH), position_y,
  271.     position_x + (WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH), position_y + DISKS_HEIGHT, FRAME_COLOR);
  272.  
  273.         position_x = position_x + direction * STEP;
  274.         updateScreen();
  275.         }
  276.     MovingDown(position_x, position_y, stopTower, diskValue);
  277. }
  278.  
  279. void MovingDown(int position_x, int position_y, int stopTower, int diskValue)
  280. {
  281.     while(position_y < (screenHeight() - (WIDTH_GROUND + (howManyDisksArr[stopTower] + 1) * DISKS_HEIGHT)))
  282.         {
  283.         Draw();
  284.         filledRect(position_x - (WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH), position_y,
  285.       position_x + (WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
  286.       position_y + DISKS_HEIGHT, DISKS_COLOR);
  287.         rect(position_x - (WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH), position_y,
  288.       position_x + (WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
  289.       position_y + DISKS_HEIGHT, FRAME_COLOR);
  290.  
  291.         position_y += STEP;
  292.         updateScreen();
  293.         }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement