SHARE
TWEET

hanoi ocenione

Mary_99 Apr 10th, 2019 89 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.  
  30. double disks_height = TOWERS_HIGHT / (DISKS_NUMBER + 5);
  31. double disks_width = (DISKS_WIDTH + 6) / TOWERS_NUMBER;
  32.  
  33. void initialingPositionOfDisks();
  34. void checkingBoundry();
  35. void drawingAll();
  36. bool winning();
  37. void array_transport();
  38.  
  39. void howManyDisks(int number_of_towers1);
  40. 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,
  41.               int direction_on_horizontal_axes, int destination_tower);
  42.  
  43. 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,
  44.                 int direction_on_horizontal_axes, int destination_tower);
  45. 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,
  46.               int direction_on_horizontal_axes, int destination_tower);
  47. void moving(int start_tower, int destination_tower, int start_row, int destination_row);
  48. void drawingBackgraund();
  49. void drawingTowers();
  50. void drawingDisks();
  51.  
  52. int main(int argc, char* argv[])
  53. {  
  54.     initialingPositionOfDisks();
  55.     if(initGraph())
  56.     {
  57.        exit(3);
  58.     }
  59.     do
  60.     {
  61.         checkingBoundry();
  62.         drawingAll();
  63.         updateScreen();
  64.         winning();
  65.         if (winning())
  66.         {
  67.             break;
  68.         }
  69.         array_transport();
  70.         updateScreen();
  71.     }while(isKeyDown(SDLK_ESCAPE) != 1);
  72.     return 0;
  73. })
  74.  
  75. void initialingPositionOfDisks(void)
  76. {
  77.     int row_counter, number_of_column;
  78.  
  79.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
  80.     {  
  81.         for(row_counter = 0; row_counter < DISKS_NUMBER; row_counter ++)  
  82.         {    
  83.             if(number_of_column == 0)
  84.             {
  85.                 disks_tab[row_counter][number_of_column] = row_counter + 1;
  86.             }
  87.             else
  88.             {
  89.             disks_tab[row_counter][number_of_column] = 0;
  90.             }
  91.         }
  92.     }
  93. }
  94.  
  95. void checkingBoundry()
  96. {
  97.     if(DISKS_NUMBER < 3 || TOWERS_NUMBER > 10)
  98.     {
  99.         printf("Invalid number of Towers \n");
  100.         exit(3);
  101.     }
  102.  
  103.     if(DISKS_NUMBER < 3 || DISKS_NUMBER >15)
  104.     {
  105.         printf("Invalid number of Disks.\n");
  106.         exit(3);
  107.     }
  108. }
  109.  
  110. void drawingAll(void)
  111. {
  112.     drawingBackgraund();
  113.     drawingTowers();
  114.     drawingDisks();
  115. }
  116.  
  117. void drawingBackgraund(void)
  118. {
  119.     filledRect(0, 0, screenWidth(), screenHeight(), BACKGROUND_COLOR);
  120.     filledRect(0, screenHeight() - FLOOR_HEIGHT, screenWidth(), screenHeight(), FLOOR_COLOR);
  121. }
  122.  
  123. void drawingTowers(void)
  124. {
  125.     int number_of_column;
  126.    
  127.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)  
  128.     {
  129.         filledRect((number_of_column * 2 + 1) * screenWidth() / (TOWERS_NUMBER * 2)-TOWERS_WIDTH / 2,
  130.         screenHeight() - TOWERS_HIGHT + FLOOR_HEIGHT ,
  131.         (number_of_column * 2 + 1) * screenWidth() / (TOWERS_NUMBER * 2) + TOWERS_WIDTH / 2, screenHeight() - FLOOR_HEIGHT - 1, TOWERS_COLOR);
  132.     }
  133. }
  134.  
  135.  
  136. void drawingDisks(void)
  137. {
  138.     int row_counter, number_of_column;
  139.    
  140.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
  141.     {
  142.         for(row_counter = 0; row_counter < DISKS_NUMBER; row_counter ++)
  143.         {
  144.             if(disks_tab[row_counter][number_of_column] != 0)
  145.             {
  146.                 filledRect(screenWidth()/(TOWERS_NUMBER * 2 ) * (1 + 2 * number_of_column) - disks_width * disks_tab[row_counter][number_of_column]-TOWERS_WIDTH / 2, screenHeight() - (DISKS_NUMBER-row_counter) * disks_height-FLOOR_HEIGHT,
  147.                 screenWidth() / (TOWERS_NUMBER * 2) * ( 1 + 2 * number_of_column) + disks_width * disks_tab[row_counter][number_of_column] + TOWERS_WIDTH / 2, screenHeight() - ((DISKS_NUMBER-row_counter - 1) * disks_height) - FLOOR_HEIGHT - 1, DISKS_COLOR);
  148.            
  149.                 rect(screenWidth() / (TOWERS_NUMBER * 2) * (1 + 2 * number_of_column) - disks_width * disks_tab[row_counter][number_of_column] -                TOWERS_WIDTH / 2,
  150.                      screenHeight() - (DISKS_NUMBER-row_counter) * disks_height - FLOOR_HEIGHT, screenWidth() / (TOWERS_NUMBER * 2) * (1 + 2 * number_of_column) + disks_width * disks_tab[row_counter][number_of_column] + TOWERS_WIDTH / 2,
  151.                      screenHeight() -((DISKS_NUMBER - row_counter - 1) * disks_height) - FLOOR_HEIGHT - 1, FRAME_COLOR);
  152.             }
  153.         }
  154.     }
  155.     SDL_Delay(DELAY);
  156. }
  157.  
  158. bool winning(void)
  159. {  
  160.     int exit;
  161.     int number_column;
  162.     exit = false;
  163.     for(number_column = 1; number_column < TOWERS_NUMBER; number_column ++)
  164.     {
  165.         howManyDisks(number_column);
  166.         if (how_many_disks_on_the_current_tower_tab[number_column] == DISKS_NUMBER)
  167.         {
  168.             textout(screenWidth() / 2 - 30, screenHeight() / 2 - 50, "YOU WON !!!", MAGENTA);
  169.             updateScreen();
  170.             getkey();
  171.             exit = true;
  172.         }
  173.     }  
  174.     return exit;      
  175. }
  176.  
  177. void array_transport(void)
  178. {  
  179.     int start_tower, end_tower;
  180.     int start_row;
  181.     int occupied_row, row_counter;
  182.  
  183.     start_tower = getkey() - '1';
  184.     end_tower = getkey() - '1';
  185.    
  186.     if(start_tower == ('0' - '1'))
  187.     {
  188.         start_tower = 9;
  189.     }
  190.     if(end_tower == ('0' - '1'))
  191.     {
  192.         end_tower = 9;  
  193.     }
  194.    
  195.     if((start_tower != end_tower) && (start_tower >= 0) && (start_tower < TOWERS_NUMBER) && (end_tower >= 0) && (end_tower < TOWERS_NUMBER))
  196.     {
  197.         start_row = DISKS_NUMBER;
  198.         occupied_row = DISKS_NUMBER;
  199.         disks_tab[DISKS_NUMBER][start_tower] = DISKS_NUMBER;
  200.         disks_tab[DISKS_NUMBER][end_tower] = DISKS_NUMBER;
  201.        
  202.         for(row_counter=DISKS_NUMBER - 1; row_counter >=0; row_counter --)//looks for last disk on initial tower
  203.         {
  204.             if(disks_tab[row_counter][start_tower]!=0)
  205.             {
  206.                 start_row = row_counter;
  207.             }
  208.         }
  209.         for(row_counter = DISKS_NUMBER - 1; row_counter >= 0; row_counter --)//looks for last disk on last tower
  210.         {
  211.             if(disks_tab[row_counter][end_tower] != 0)
  212.             {
  213.                 occupied_row = row_counter;
  214.             }
  215.         }
  216.         if((start_row != DISKS_NUMBER) && (disks_tab[start_row][start_tower] <= disks_tab[occupied_row][end_tower]))
  217.         {
  218.             moving(start_tower, end_tower, start_row, occupied_row - 1);
  219.         }
  220.     }
  221. }
  222.  
  223. void howManyDisks(int number_of_towers1)
  224. {
  225.     int row_counter;
  226.    
  227.     how_many_disks_on_the_current_tower_tab[number_of_towers1] = 0;
  228.        
  229.     for(row_counter = 0; row_counter < DISKS_NUMBER; row_counter ++)//check if current tower is occupied
  230.     {
  231.         if(disks_tab[row_counter][number_of_towers1]!= 0)
  232.         {
  233.             how_many_disks_on_the_current_tower_tab[number_of_towers1] += 1;
  234.         }
  235.     }
  236. }
  237.  
  238. void moving(int start_tower, int destination_tower, int start_row, int destination_row)
  239.  
  240. {
  241.     int vertical_position_of_the_bottom_of_moving_disk;
  242.     int horizontal_position_of_the_centre_of_moving_disk;
  243.     int valu_for_the_disk_to_move;
  244.     int direction_on_horizontal_axes;
  245.  
  246.     howManyDisks(start_tower);
  247.  
  248.     valu_for_the_disk_to_move = disks_tab[start_row][start_tower];
  249.     disks_tab[start_row][start_tower] = 0;
  250.  
  251.     vertical_position_of_the_bottom_of_moving_disk = screenHeight() - how_many_disks_on_the_current_tower_tab[start_tower] * disks_height-FLOOR_HEIGHT;
  252.    
  253.     horizontal_position_of_the_centre_of_moving_disk  = (start_tower * 2 + 1 ) * screenWidth() / (TOWERS_NUMBER * 2);
  254.  
  255.     if(start_tower < destination_tower)
  256.     {
  257.         direction_on_horizontal_axes = 1;
  258.     }
  259.     if(start_tower > destination_tower)
  260.     {
  261.         direction_on_horizontal_axes = -1;
  262.     }
  263.     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);
  264.    
  265.     disks_tab[destination_row][destination_tower] = valu_for_the_disk_to_move;
  266. }
  267.  
  268. 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,
  269.             int direction_on_horizontal_axes, int destination_tower)
  270. {
  271.     while((vertical_position_of_the_bottom_of_moving_disk+STEP_PER_ITERATION_OF_MOVING_DISC) >= (screenHeight() - (FLOOR_HEIGHT + (DISKS_NUMBER + 4) *  disks_height)))
  272.     {
  273.         drawingAll();
  274.    
  275.         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);
  276.        
  277.         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);
  278.  
  279.         vertical_position_of_the_bottom_of_moving_disk -= STEP_PER_ITERATION_OF_MOVING_DISC;
  280.         updateScreen();
  281.     }
  282.     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);
  283. }
  284.  
  285.  
  286. 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,
  287.               int direction_on_horizontal_axes, int destination_tower)
  288. {
  289.     while(abs(horizontal_position_of_the_centre_of_moving_disk - (destination_tower * 2 + 1) * screenWidth() /(TOWERS_NUMBER * 2)) >= STEP_PER_ITERATION_OF_MOVING_DISC)
  290.     {
  291.         drawingAll();
  292.    
  293.         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);
  294.        
  295.         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);
  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.         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);
  316.        
  317.         vertical_position_of_the_bottom_of_moving_disk += STEP_PER_ITERATION_OF_MOVING_DISC;
  318.         updateScreen();
  319.     }
  320. }
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