Advertisement
Mary_99

HANOI Ewentulanie da sie oddac

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