SHARE
TWEET

hanoi prawie ideał <3

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