SHARE
TWEET

HANOI Z DELAY

Mary_99 Apr 3rd, 2019 15 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "primlib.h"
  4. #include <stdbool.h>
  5.  
  6. #define BACKGROUND_COLOR BLACK
  7. #define FLOOR_COLOR YELLOW
  8. #define TOWERS_COLOR RED
  9. #define DISKS_COLOR MAGENTA
  10. #define FRAME_COLOR BLACK
  11. #define TEXT_COLOR YELLOW
  12.  
  13. #define DELAY 4
  14.  
  15. #define TOWERS_NUMBER 9
  16. #define TOWERS_WIDTH 5
  17. #define TOWERS_HIGHT 300
  18.  
  19. #define DISKS_NUMBER 15
  20. #define DISKS_WIDTH 15
  21. #define STEP_PER_ITERATION_OF_MOVING_DISC 1
  22. #define DISKS_HEIGHT 10
  23.  
  24. #define FLOOR_HEIGHT 30
  25. //-----------------------global variables----------------------------------
  26.  
  27. int how_many_disks_on_the_current_tower_tab[TOWERS_NUMBER];
  28. int disks_tab[DISKS_NUMBER + 1][TOWERS_NUMBER];
  29. float number_of_towers = TOWERS_NUMBER;
  30. float number_of_disks = DISKS_NUMBER;
  31.  
  32. double disks_height = TOWERS_HIGHT / (DISKS_NUMBER + 5);
  33. double disks_width = (DISKS_WIDTH + 6) / TOWERS_NUMBER;
  34.  
  35. void initialingPositionOfDisks();
  36. void checkingBoundry();
  37. void drawingAll();
  38. bool winning();
  39. void array_transport();
  40.  
  41. void howManyDisks(int number_of_towers);
  42. void movingUp(int vertical_position_of_the_bottom_of_moving_disk, int horizontal_position_of_the_centre_of_moving_disk, int valu_for_the_disk_to_move,
  43.               int direction_on_horizontal_axes, int destination_tower);
  44.  
  45. void movingSide(int vertical_position_of_the_bottom_of_moving_disk, int horizontal_position_of_the_centre_of_moving_disk, int valu_for_the_disk_to_move,
  46.                 int direction_on_horizontal_axes, int destination_tower);
  47. void moveDown(int vertical_position_of_the_bottom_of_moving_disk, int horizontal_position_of_the_centre_of_moving_disk, int valu_for_the_disk_to_move,
  48.               int direction_on_horizontal_axes, int destination_tower);
  49. void moving(int start_tower, int destination_tower, int start_row, int destination_row);
  50. void drawingBackgraund();
  51. void drawingTowers();
  52. void drawingDisks();
  53.  
  54. int main(int argc, char* argv[])
  55. {  
  56.     initialingPositionOfDisks();
  57.     if(initGraph())
  58.     {
  59.        exit(3);
  60.     }
  61.     do
  62.     {
  63.         checkingBoundry();
  64.         drawingAll();
  65.         updateScreen();
  66.         winning();
  67.         if (winning())
  68.         {
  69.             break;
  70.         }
  71.         array_transport();
  72.         updateScreen();
  73.     }while(isKeyDown(SDLK_ESCAPE) != 1);
  74.     return 0;
  75. }
  76.  
  77. void initialingPositionOfDisks(void)
  78. {
  79.     int row_counter, number_of_column;
  80.  
  81.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
  82.     {  
  83.         for(row_counter = 0; row_counter < DISKS_NUMBER; row_counter ++)  
  84.         {    
  85.             if(number_of_column == 0)
  86.             {
  87.                 disks_tab[row_counter][number_of_column] = row_counter + 1;
  88.             }
  89.             else
  90.             {
  91.             disks_tab[row_counter][number_of_column] = 0;
  92.             }
  93.         }
  94.     }
  95. }
  96.  
  97. void checkingBoundry()
  98. {
  99.     if(DISKS_NUMBER < 3 || TOWERS_NUMBER > 10)
  100.     {
  101.         printf("Invalid number of Towers \n");
  102.         exit(3);
  103.     }
  104.  
  105.     if(DISKS_NUMBER < 3 || DISKS_NUMBER >15)
  106.     {
  107.         printf("Invalid number of Disks.\n");
  108.         exit(3);
  109.     }
  110. }
  111.  
  112. void drawingAll(void)
  113. {
  114.     drawingBackgraund();
  115.     drawingTowers();
  116.     drawingDisks();
  117. }
  118.  
  119. void drawingBackgraund(void)
  120. {
  121.     filledRect(0, 0, screenWidth(), screenHeight(), BACKGROUND_COLOR);
  122.     filledRect(0, screenHeight() - FLOOR_HEIGHT, screenWidth(), screenHeight(), FLOOR_COLOR);
  123. }
  124.  
  125. void drawingTowers(void)
  126. {
  127.     int number_of_column;
  128.    
  129.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)  
  130.     {
  131.         filledRect((number_of_column * 2 + 1) * screenWidth() / (number_of_towers * 2)-TOWERS_WIDTH / 2,
  132.         screenHeight() - TOWERS_HIGHT + FLOOR_HEIGHT ,
  133.         (number_of_column * 2 + 1) * screenWidth() / (number_of_towers * 2) + TOWERS_WIDTH / 2, screenHeight() - FLOOR_HEIGHT - 1, TOWERS_COLOR);
  134.     }
  135. }
  136.  
  137.  
  138. void drawingDisks(void)
  139. {
  140.     int row_counter, number_of_column;
  141.    
  142.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
  143.     {
  144.         for(row_counter = 0; row_counter < DISKS_NUMBER; row_counter ++)
  145.         {
  146.             if(disks_tab[row_counter][number_of_column] != 0)
  147.             {
  148.                 filledRect(screenWidth()/(number_of_towers * 2) * (1 + 2 * number_of_column) - disks_width * disks_tab[row_counter][number_of_column]-TOWERS_WIDTH / 2, screenHeight() - (number_of_disks-row_counter) * disks_height-FLOOR_HEIGHT,
  149.                 screenWidth() / (number_of_towers * 2) * ( 1 + 2 * number_of_column) + disks_width * disks_tab[row_counter][number_of_column] + TOWERS_WIDTH / 2, screenHeight() - ((number_of_disks-row_counter - 1) * disks_height) - FLOOR_HEIGHT - 1, DISKS_COLOR);
  150.            
  151.                 rect(screenWidth() / (number_of_towers * 2) * (1 + 2 * number_of_column) - disks_width * disks_tab[row_counter][number_of_column] -                TOWERS_WIDTH / 2,
  152.                      screenHeight() - (number_of_disks-row_counter) * disks_height - FLOOR_HEIGHT, screenWidth() / (number_of_towers * 2) * (1 + 2 * number_of_column) + disks_width * disks_tab[row_counter][number_of_column] + TOWERS_WIDTH / 2,
  153.                      screenHeight() -((number_of_disks - row_counter - 1) * disks_height) - FLOOR_HEIGHT - 1, FRAME_COLOR);
  154.             }
  155.         }
  156.     }
  157.     SDL_Delay(DELAY);
  158. }
  159.  
  160. bool winning(void)
  161. {  
  162.     int exit;
  163.     int number_column;
  164.     exit = false;
  165.     for(number_column = 1; number_column < TOWERS_NUMBER; number_column ++)
  166.     {
  167.         howManyDisks(number_column);
  168.         if (how_many_disks_on_the_current_tower_tab[number_column] == DISKS_NUMBER)
  169.         {
  170.             textout(screenWidth() / 2 - 30, screenHeight() / 2 - 50, "YOU WON !!!", MAGENTA);
  171.             updateScreen();
  172.             getkey();
  173.             exit = true;
  174.         }
  175.     }  
  176.     return exit;      
  177. }
  178.  
  179. void array_transport(void)
  180. {  
  181.     int start_tower, end_tower;
  182.     int start_row;
  183.     int occupied_row, row_counter;
  184.  
  185.     start_tower = getkey() - '1';
  186.     end_tower = getkey() - '1';
  187.    
  188.     if(start_tower == ('0' - '1'))
  189.     {
  190.         start_tower = 9;
  191.     }
  192.     if(end_tower == ('0' - '1'))
  193.     {
  194.         end_tower = 9;  
  195.     }
  196.    
  197.     if((start_tower != end_tower) && (start_tower >= 0) && (start_tower < TOWERS_NUMBER) && (end_tower >= 0) && (end_tower < TOWERS_NUMBER))
  198.     {
  199.         start_row = DISKS_NUMBER;
  200.         occupied_row = DISKS_NUMBER;
  201.         disks_tab[DISKS_NUMBER][start_tower] = DISKS_NUMBER;
  202.         disks_tab[DISKS_NUMBER][end_tower] = DISKS_NUMBER;
  203.        
  204.         for(row_counter=DISKS_NUMBER - 1; row_counter >=0; row_counter --)//looks for last disk on initial tower
  205.         {
  206.             if(disks_tab[row_counter][start_tower]!=0)
  207.             {
  208.                 start_row = row_counter;
  209.             }
  210.         }
  211.         for(row_counter = DISKS_NUMBER - 1; row_counter >= 0; row_counter --)//looks for last disk on last tower
  212.         {
  213.             if(disks_tab[row_counter][end_tower] != 0)
  214.             {
  215.                 occupied_row = row_counter;
  216.             }
  217.         }
  218.         if((start_row != DISKS_NUMBER) && (disks_tab[start_row][start_tower] <= disks_tab[occupied_row][end_tower]))
  219.         {
  220.             moving(start_tower, end_tower, start_row, occupied_row - 1);
  221.         }
  222.     }
  223. }
  224.  
  225. void howManyDisks(int number_of_towers)
  226. {
  227.     int row_counter;
  228.    
  229.     how_many_disks_on_the_current_tower_tab[number_of_towers] = 0;
  230.        
  231.     for(row_counter = 0; row_counter < DISKS_NUMBER; row_counter ++)//check if current tower is occupied
  232.     {
  233.         if(disks_tab[row_counter][number_of_towers]!= 0)
  234.         {
  235.             how_many_disks_on_the_current_tower_tab[number_of_towers] += 1;
  236.         }
  237.     }
  238. }
  239.  
  240. void moving(int start_tower, int destination_tower, int start_row, int destination_row)
  241.  
  242. {
  243.     int vertical_position_of_the_bottom_of_moving_disk;
  244.     int horizontal_position_of_the_centre_of_moving_disk;
  245.     int valu_for_the_disk_to_move;
  246.     int direction_on_horizontal_axes;
  247.  
  248.     howManyDisks(start_tower);
  249.  
  250.     valu_for_the_disk_to_move = disks_tab[start_row][start_tower];
  251.     disks_tab[start_row][start_tower] = 0;
  252.  
  253.     vertical_position_of_the_bottom_of_moving_disk = screenHeight() - (FLOOR_HEIGHT + DISKS_HEIGHT *    how_many_disks_on_the_current_tower_tab[start_tower]);
  254.    
  255.     horizontal_position_of_the_centre_of_moving_disk  = (start_tower * 2 + 1 ) * screenWidth() / (TOWERS_NUMBER * 2);
  256.  
  257.     if(start_tower < destination_tower)
  258.     {
  259.         direction_on_horizontal_axes = 1;
  260.     }
  261.     if(start_tower > destination_tower)
  262.     {
  263.         direction_on_horizontal_axes = -1;
  264.     }
  265.     movingUp(vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk, valu_for_the_disk_to_move, direction_on_horizontal_axes, destination_tower);
  266.    
  267.     disks_tab[destination_row][destination_tower] = valu_for_the_disk_to_move;
  268. }
  269.  
  270. void movingUp(int vertical_position_of_the_bottom_of_moving_disk, int horizontal_position_of_the_centre_of_moving_disk, int valu_for_the_disk_to_move,
  271.             int direction_on_horizontal_axes, int destination_tower)
  272. {
  273.     while((vertical_position_of_the_bottom_of_moving_disk+STEP_PER_ITERATION_OF_MOVING_DISC) >= (screenHeight() - (FLOOR_HEIGHT + (DISKS_NUMBER + 4) *  disks_height)))
  274.     {
  275.         drawingAll();
  276.    
  277.         filledRect(horizontal_position_of_the_centre_of_moving_disk - (TOWERS_WIDTH / 2 + valu_for_the_disk_to_move * disks_width), vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk + (TOWERS_WIDTH / 2 + valu_for_the_disk_to_move * disks_width), vertical_position_of_the_bottom_of_moving_disk +  disks_height, DISKS_COLOR);
  278.        
  279.         rect(horizontal_position_of_the_centre_of_moving_disk - (TOWERS_WIDTH / 2 + valu_for_the_disk_to_move * disks_width), vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk + (TOWERS_WIDTH / 2 + valu_for_the_disk_to_move * disks_width), vertical_position_of_the_bottom_of_moving_disk +  disks_height, FRAME_COLOR);
  280.  
  281.         vertical_position_of_the_bottom_of_moving_disk -= STEP_PER_ITERATION_OF_MOVING_DISC;
  282.         updateScreen();
  283.     }
  284.     movingSide(vertical_position_of_the_bottom_of_moving_disk,  horizontal_position_of_the_centre_of_moving_disk, valu_for_the_disk_to_move, direction_on_horizontal_axes, destination_tower);
  285. }
  286.  
  287.  
  288. void movingSide(int vertical_position_of_the_bottom_of_moving_disk, int horizontal_position_of_the_centre_of_moving_disk, int valu_for_the_disk_to_move,
  289.               int direction_on_horizontal_axes, int destination_tower)
  290. {
  291.     while(abs(horizontal_position_of_the_centre_of_moving_disk - (destination_tower * 2 + 1) * screenWidth() /(TOWERS_NUMBER * 2)) >= STEP_PER_ITERATION_OF_MOVING_DISC)
  292.     {
  293.         drawingAll();
  294.    
  295.         filledRect(horizontal_position_of_the_centre_of_moving_disk - (TOWERS_WIDTH / 2 + valu_for_the_disk_to_move * disks_width), vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk + (TOWERS_WIDTH / 2 + valu_for_the_disk_to_move*disks_width), vertical_position_of_the_bottom_of_moving_disk + disks_height, DISKS_COLOR);
  296.        
  297.         horizontal_position_of_the_centre_of_moving_disk = horizontal_position_of_the_centre_of_moving_disk + direction_on_horizontal_axes * STEP_PER_ITERATION_OF_MOVING_DISC;
  298.        
  299.         updateScreen();
  300.     }
  301.     moveDown( vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk, valu_for_the_disk_to_move, direction_on_horizontal_axes, destination_tower);
  302. }
  303.  
  304. void moveDown(int vertical_position_of_the_bottom_of_moving_disk, int horizontal_position_of_the_centre_of_moving_disk, int valu_for_the_disk_to_move,
  305.               int direction_on_horizontal_axes, int destination_tower)
  306. {
  307.     howManyDisks(destination_tower);
  308.    
  309.     while(vertical_position_of_the_bottom_of_moving_disk < (screenHeight() - (FLOOR_HEIGHT + (how_many_disks_on_the_current_tower_tab[destination_tower] + 1) *  disks_height)))
  310.     {
  311.         drawingAll();
  312.    
  313.         filledRect(horizontal_position_of_the_centre_of_moving_disk - (TOWERS_WIDTH / 2 + valu_for_the_disk_to_move * disks_width), vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk + (TOWERS_WIDTH / 2 + valu_for_the_disk_to_move * disks_width), vertical_position_of_the_bottom_of_moving_disk +  disks_height, DISKS_COLOR);
  314.        
  315.         vertical_position_of_the_bottom_of_moving_disk += STEP_PER_ITERATION_OF_MOVING_DISC;
  316.         updateScreen();
  317.     }
  318. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top