SHARE
TWEET

HANOI ZANIM SI EZDENERWUJE

Mary_99 Mar 31st, 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. #define TOWERS_NUMBER 4
  12. #define DISKS_NUMBER 3
  13. #define TOWERS_WIDTH 6
  14. #define FLOOR_HEIGHT 30
  15. #define TOWERS_HIGHT 200
  16. #define DISKS_WIDTH 3
  17. #define STEP_PER_ITERATION_OF_MOVING_DISC 2
  18. #define DISKS_HEIGHT 10
  19. #define SPACE_BTW_TOWERS screenWidth()/TOWERS_NUMBER
  20. #define GROUND_HEIGHT  screenHeight() - 30
  21.  
  22. int how_many_disks_on_the_current_tower_tab[TOWERS_NUMBER];
  23. int disks_tab[DISKS_NUMBER + 1][TOWERS_NUMBER];
  24. float number_of_towers = TOWERS_NUMBER;
  25. float nuber_of_disks = DISKS_NUMBER;
  26.  
  27. double makingDistanceBetweenTowers();
  28. void checkingBoundry();
  29. void drawingBackgraund();
  30. void drawingTowers();
  31. void drawingDisks();
  32. void initialingPositionOfDisks();
  33. void howManyDisks(int);
  34. void drawingAll(void);
  35. void moving(int,int,int,int);
  36. int winning();
  37. void array_transport(void);
  38.  
  39. int main(int argc, char* argv[])
  40. {  
  41.  /*
  42.    
  43.    
  44.     if(initGraph())
  45.     {
  46.        exit(3);
  47.     }
  48.     do
  49.     {
  50.         checkingBoundry();
  51.         drawingBackgraund();
  52.         drawingTowers();
  53.         drawingDisks();
  54.         initialingPositionOfDisks();
  55.         updateScreen();
  56.  
  57.      
  58.         winning();
  59.  
  60.     }while(isKeyDown(SDLK_ESCAPE) != 1);
  61.    
  62.     return 0;*/
  63.  
  64.  
  65.     initialingPositionOfDisks();
  66.  
  67.     if(initGraph())
  68.        exit(3);
  69.    
  70.     do
  71.         {
  72.         checkingBoundry();
  73.         drawingBackgraund();
  74.         drawingTowers();
  75.         drawingDisks();
  76.         updateScreen();
  77.    
  78.         winning();
  79.         if(winning()==1)
  80.             break;
  81.  
  82.         array_transport();
  83.         updateScreen();
  84.  
  85.         }while(pollkey()!=SDLK_ESCAPE);
  86.    
  87.     return 0;
  88.  
  89.  
  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.  
  111.  
  112.  
  113. void drawingBackgraund(void)
  114. {
  115.     int number_of_row, 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. double makingDistanceBetweenTowers()
  123. {
  124.     double distance_between_towers = screenWidth() / (TOWERS_NUMBER + 1);
  125.     return distance_between_towers;
  126.    
  127. }
  128.  
  129.  
  130.  
  131. void drawingTowers(void)
  132. {
  133.     int number_of_row, number_of_column;
  134.    
  135.    
  136.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)  
  137.     {
  138.        filledRect((number_of_column * 2 + 1)*screenWidth() / (number_of_towers * 2)-TOWERS_WIDTH / 2, screenHeight()-((nuber_of_disks + 1)*DISKS_HEIGHT+FLOOR_HEIGHT), (number_of_column * 2 + 1)*screenWidth() /(number_of_towers * 2) + TOWERS_WIDTH / 2, screenHeight() - FLOOR_HEIGHT - 1, TOWERS_COLOR);
  139.     }
  140. }
  141.  
  142.  
  143.  
  144.  
  145. void drawingDisks(void)
  146. {
  147.     int number_of_row, number_of_column;
  148.    
  149.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
  150.     {
  151.         for(number_of_row = 0; number_of_row < DISKS_NUMBER; number_of_row++)
  152.         {
  153.             if(disks_tab[number_of_row][number_of_column]!=0)
  154.             {
  155.                 filledRect(screenWidth()/(number_of_towers * 2)*(1 + 2 * number_of_column) - DISKS_WIDTH * disks_tab[number_of_row][number_of_column]-TOWERS_WIDTH/2, screenHeight()-(nuber_of_disks-number_of_row)*DISKS_HEIGHT-FLOOR_HEIGHT, screenWidth()/(number_of_towers * 2) * ( 1 + 2 * number_of_column) + DISKS_WIDTH*disks_tab[number_of_row][number_of_column]+TOWERS_WIDTH / 2, screenHeight()-((nuber_of_disks-number_of_row-1)*DISKS_HEIGHT)-FLOOR_HEIGHT-1, DISKS_COLOR);
  156.            
  157.                 rect(screenWidth()/(number_of_towers * 2) * (1 + 2 * number_of_column)-DISKS_WIDTH*disks_tab[number_of_row][number_of_column]-TOWERS_WIDTH/2,screenHeight()-(nuber_of_disks-number_of_row)*DISKS_HEIGHT-FLOOR_HEIGHT, screenWidth()/(number_of_towers * 2) * (1 + 2 * number_of_column)+DISKS_WIDTH*disks_tab[number_of_row][number_of_column]+TOWERS_WIDTH / 2, screenHeight()-((nuber_of_disks-number_of_row-1)*DISKS_HEIGHT)-FLOOR_HEIGHT-1, FRAME_COLOR);
  158.             }
  159.         }
  160.     }
  161.    
  162. }
  163.  
  164. void initialingPositionOfDisks(void)
  165. {
  166.     int number_of_row, number_of_column;
  167.  
  168.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
  169.     {  
  170.         for(number_of_row = 0; number_of_row < DISKS_NUMBER; number_of_row ++)  
  171.         {    
  172.             if(number_of_column == 0)
  173.             {
  174.                 disks_tab[number_of_row][number_of_column] = number_of_row + 1;
  175.             }
  176.             else
  177.             {
  178.             disks_tab[number_of_row][number_of_column]= 0;
  179.             }
  180.         }
  181.     }
  182.  
  183. }
  184.  
  185.  
  186.  
  187.  
  188. void howManyDisks(int number_of_towers)
  189. {
  190.     int number_of_row;
  191.    
  192.    
  193.     how_many_disks_on_the_current_tower_tab[number_of_towers] = 0;
  194.         for(number_of_row = 0;number_of_row < DISKS_NUMBER; number_of_row++)
  195.         {
  196.         if(disks_tab[number_of_row][number_of_towers]!= 0)
  197.             {
  198.                 how_many_disks_on_the_current_tower_tab[number_of_towers] += 1;
  199.             }
  200.         }
  201.        
  202. }
  203.  
  204.  
  205.  
  206.  
  207. void moving(initial_tower,destination_tower,initial_row,destination_row)
  208.  
  209. {
  210.     int position_y, position_x, disk_value, direction;
  211.  
  212.     howManyDisks(initial_tower);
  213.  
  214.     disk_value = disks_tab[initial_row][initial_tower];
  215.     disks_tab[initial_row][initial_tower] = 0;
  216.  
  217.     position_y = screenHeight()-(FLOOR_HEIGHT+DISKS_HEIGHT*how_many_disks_on_the_current_tower_tab[initial_tower]);
  218.     position_x = (initial_tower*2+1)*screenWidth()/(TOWERS_NUMBER*2);
  219.  
  220.     if(initial_tower < destination_tower)
  221.         direction = 1;
  222.     if(initial_tower > destination_tower)
  223.         direction = -1;
  224.  
  225.     while((position_y+STEP_PER_ITERATION_OF_MOVING_DISC)>=(screenHeight()-(FLOOR_HEIGHT+(DISKS_NUMBER+4)*DISKS_HEIGHT)))
  226.         {
  227.        
  228.         drawingBackgraund();
  229.         drawingTowers();
  230.         drawingDisks();
  231.    
  232.         filledRect(position_x-(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y, position_x+(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y+DISKS_HEIGHT, DISKS_COLOR);
  233.         rect(position_x-(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y, position_x+(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y+DISKS_HEIGHT, FRAME_COLOR);
  234.  
  235.         position_y-= STEP_PER_ITERATION_OF_MOVING_DISC;
  236.         updateScreen();
  237.         }
  238.  
  239.    
  240.     /*This while loop is responsible for the horizontal move */
  241.     while(abs(position_x-(destination_tower*2+1)*screenWidth()/(TOWERS_NUMBER*2))>=STEP_PER_ITERATION_OF_MOVING_DISC)
  242.         {
  243.         drawingBackgraund();
  244.         drawingTowers();
  245.         drawingDisks();
  246.    
  247.         filledRect(position_x-(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y, position_x+(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y+DISKS_HEIGHT, DISKS_COLOR);
  248.         rect(position_x-(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y, position_x+(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y+DISKS_HEIGHT, FRAME_COLOR);
  249.        
  250.         position_x=position_x+direction*STEP_PER_ITERATION_OF_MOVING_DISC;
  251.         updateScreen();
  252.         }
  253.    
  254.     howManyDisks(destination_tower); /*Calculation of how many disks are on the destination tower*/
  255.  
  256.     /*This while loop is responsible for the second part of the vertical move of the disks*/
  257.     while(position_y<(screenHeight()-(FLOOR_HEIGHT+(how_many_disks_on_the_current_tower_tab[destination_tower]+1)*DISKS_HEIGHT)))
  258.         {
  259.        
  260.         drawingBackgraund();
  261.         drawingTowers();
  262.         drawingDisks();
  263.    
  264.         filledRect(position_x-(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y, position_x+(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y+DISKS_HEIGHT, DISKS_COLOR);
  265.         rect(position_x-(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y, position_x+(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y+DISKS_HEIGHT, FRAME_COLOR);
  266.  
  267.         position_y += STEP_PER_ITERATION_OF_MOVING_DISC;
  268.         updateScreen();
  269.         }
  270.     disks_tab[destination_row][destination_tower]=disk_value;
  271.  
  272.  
  273. }
  274.  
  275. int winning(void)
  276. {  
  277.     int exit,column;
  278.     exit = 0;
  279.     for(column=1;column<TOWERS_NUMBER;column++)
  280.     {
  281.         howManyDisks(column);
  282.         if (how_many_disks_on_the_current_tower_tab[column]==DISKS_NUMBER)
  283.         {
  284.             textout(screenWidth()/2-30, screenHeight()/2-50, "YOU WON !!!", TEXT_COLOR);
  285.             updateScreen();
  286.             getkey();
  287.             exit = 1;
  288.         }
  289.     }  
  290.     return exit;       
  291. }
  292.  
  293.  
  294. void array_transport(void) /*This function is responsible for operations on disks_array. If the conditions to move a disk from one towers to the other are satisfied then it launches the moving() function*/
  295. {  
  296.     int initial_tower, destination_tower, initial_row, destination_row, occupied_row, row_counter;
  297.  
  298.     /*initial_row - the first row from above occupied by disk on the initial tower
  299.       occupied_row - the first occupied row(from above) on the destination tower
  300.       destination_row - row on the destination tower where the disk will be moved
  301.       row - counter when operating on rows*/
  302.  
  303.     initial_tower=getkey()-'1'; /*This variable represents the initial tower on which was located the disk that will be moved*/
  304.     destination_tower=getkey()-'1'; /*This variable represents the tower on which the disk will be placed after the movement*/
  305.    
  306.     if(initial_tower==('0'-'1'))
  307.         initial_tower=9;
  308.  
  309.     if(destination_tower==('0'-'1'))
  310.         destination_tower=9;   
  311.     /*These if functions make the programm to work on the 10th tower when the user presses '0'*/
  312.    
  313.     if((initial_tower!=destination_tower)&&(initial_tower>=0)&&(initial_tower<TOWERS_NUMBER)&&(destination_tower>=0)&&(destination_tower<TOWERS_NUMBER))
  314.     /*This if function ckecks whether the initial and destination towers aren't the same and belong to the interval between 0 and TOWERS_NUMBER*/
  315.         {
  316.         initial_row=DISKS_NUMBER;
  317.         occupied_row=DISKS_NUMBER;
  318.         how_many_disks_on_the_current_tower_tab[DISKS_NUMBER][initial_tower]=DISKS_NUMBER;
  319.         how_many_disks_on_the_current_tower_tab[DISKS_NUMBER][destination_tower]=DISKS_NUMBER;
  320.         for(row_counter=DISKS_NUMBER-1;row_counter>=0;row_counter--)/*This loop looks for the last (from below) disk on the initial tower and writes its row coordinate as initial_row variable*/
  321.             {
  322.             if(how_many_disks_on_the_current_tower_tab[rrow_counterow][initial_tower]!=0)
  323.                 {
  324.                 initial_row=rrow_counterow;
  325.                     }
  326.             }
  327.         for(row_counter=DISKS_NUMBER-1;row_counter>=0;row_counter--)/*This loop looks for the last (from below) disk on the destination tower and writes destination_row as the first (from below) free space on the tower*/
  328.             {
  329.             if(how_many_disks_on_the_current_tower_tab[row_counter][destination_tower]!=0)
  330.                 {
  331.                 occupied_row=row;
  332.                     }
  333.             }
  334.         if((initial_row!=DISKS_NUMBER)&&(how_many_disks_on_the_current_tower_tab[initial_row][initial_tower]<=how_many_disks_on_the_current_tower_tab[occupied_row][destination_tower])) /*This if function checks if the conditions of movement are satisfied - if the disk which we want to move is smaller than the disk on which we want to place it*/
  335.             moving(initial_tower,destination_tower, initial_row, occupied_row-1);
  336.        
  337.         }
  338.    
  339. }
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