SHARE
TWEET

HANOII WIIIIIIIIIIIIIIIIIIIIIIIIIII <3

Mary_99 Mar 31st, 2019 58 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();
  35. void moving(int,int,int,int);
  36. int winning();
  37. void array_transport();
  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.         drawingAll();
  74.         updateScreen();
  75.    
  76.         winning();
  77.         if(winning()==1)
  78.             break;
  79.  
  80.         array_transport();
  81.         updateScreen();
  82.  
  83.         }while(pollkey()!=SDLK_ESCAPE);
  84.    
  85.     return 0;
  86.  
  87.  
  88.    
  89. }
  90.  
  91.  
  92.  
  93.  void checkingBoundry()
  94. {
  95.     if(DISKS_NUMBER < 3 || TOWERS_NUMBER > 10)
  96.     {
  97.         printf("Invalid number of Towers \n");
  98.         exit(3);
  99.     }
  100.  
  101.     if(DISKS_NUMBER < 3 || DISKS_NUMBER >15)
  102.     {
  103.         printf("Invalid number of Disks.\n");
  104.         exit(3);
  105.     }
  106.  
  107. }
  108.  
  109. void drawingAll(void)
  110. {
  111.     drawingBackgraund();
  112.     drawingTowers();
  113.     drawingDisks();
  114. }
  115.  
  116.  
  117.  
  118.  
  119. void drawingBackgraund(void)
  120. {
  121.     int number_of_row, number_of_column;
  122.    
  123.     filledRect(0, 0, screenWidth(), screenHeight(), BACKGROUND_COLOR);
  124.     filledRect(0, screenHeight()-FLOOR_HEIGHT, screenWidth(), screenHeight(), FLOOR_COLOR);
  125.    
  126. }
  127.  
  128. double makingDistanceBetweenTowers()
  129. {
  130.     double distance_between_towers = screenWidth() / (TOWERS_NUMBER + 1);
  131.     return distance_between_towers;
  132.    
  133. }
  134.  
  135.  
  136.  
  137. void drawingTowers(void)
  138. {
  139.     int number_of_row, number_of_column;
  140.    
  141.    
  142.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)  
  143.     {
  144.        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);
  145.     }
  146. }
  147.  
  148.  
  149.  
  150.  
  151. void drawingDisks(void)
  152. {
  153.     int number_of_row, number_of_column;
  154.    
  155.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
  156.     {
  157.         for(number_of_row = 0; number_of_row < DISKS_NUMBER; number_of_row++)
  158.         {
  159.             if(disks_tab[number_of_row][number_of_column]!=0)
  160.             {
  161.                 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);
  162.            
  163.                 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);
  164.             }
  165.         }
  166.     }
  167.    
  168. }
  169.  
  170. void initialingPositionOfDisks(void)
  171. {
  172.     int number_of_row, number_of_column;
  173.  
  174.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
  175.     {  
  176.         for(number_of_row = 0; number_of_row < DISKS_NUMBER; number_of_row ++)  
  177.         {    
  178.             if(number_of_column == 0)
  179.             {
  180.                 disks_tab[number_of_row][number_of_column] = number_of_row + 1;
  181.             }
  182.             else
  183.             {
  184.             disks_tab[number_of_row][number_of_column]= 0;
  185.             }
  186.         }
  187.     }
  188.  
  189. }
  190.  
  191.  
  192.  
  193.  
  194. void howManyDisks(int number_of_towers)
  195. {
  196.     int number_of_row;
  197.    
  198.    
  199.     how_many_disks_on_the_current_tower_tab[number_of_towers] = 0;
  200.         for(number_of_row = 0;number_of_row < DISKS_NUMBER; number_of_row++)
  201.         {
  202.         if(disks_tab[number_of_row][number_of_towers]!= 0)
  203.             {
  204.                 how_many_disks_on_the_current_tower_tab[number_of_towers] += 1;
  205.             }
  206.         }
  207.        
  208. }
  209.  
  210.  
  211.  
  212.  
  213. void moving(initial_tower,destination_tower,initial_row,destination_row)
  214.  
  215. {
  216.     int position_y, position_x, disk_value, direction;
  217.  
  218.     howManyDisks(initial_tower);
  219.  
  220.     disk_value = disks_tab[initial_row][initial_tower];
  221.     disks_tab[initial_row][initial_tower] = 0;
  222.  
  223.     position_y = screenHeight()-(FLOOR_HEIGHT+DISKS_HEIGHT*how_many_disks_on_the_current_tower_tab[initial_tower]);
  224.     position_x = (initial_tower*2+1)*screenWidth()/(TOWERS_NUMBER*2);
  225.  
  226.     if(initial_tower < destination_tower)
  227.         direction = 1;
  228.     if(initial_tower > destination_tower)
  229.         direction = -1;
  230.  
  231.     while((position_y+STEP_PER_ITERATION_OF_MOVING_DISC)>=(screenHeight()-(FLOOR_HEIGHT+(DISKS_NUMBER+4)*DISKS_HEIGHT)))
  232.         {
  233.        
  234.         drawingAll();
  235.    
  236.         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);
  237.         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);
  238.  
  239.         position_y-= STEP_PER_ITERATION_OF_MOVING_DISC;
  240.         updateScreen();
  241.         }
  242.  
  243.    
  244.     /*This while loop is responsible for the horizontal move */
  245.     while(abs(position_x-(destination_tower*2+1)*screenWidth()/(TOWERS_NUMBER*2))>=STEP_PER_ITERATION_OF_MOVING_DISC)
  246.         {
  247.         drawingAll();
  248.    
  249.         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);
  250.         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);
  251.        
  252.         position_x=position_x+direction*STEP_PER_ITERATION_OF_MOVING_DISC;
  253.         updateScreen();
  254.         }
  255.    
  256.     howManyDisks(destination_tower); /*Calculation of how many disks are on the destination tower*/
  257.  
  258.     /*This while loop is responsible for the second part of the vertical move of the disks*/
  259.     while(position_y<(screenHeight()-(FLOOR_HEIGHT+(how_many_disks_on_the_current_tower_tab[destination_tower]+1)*DISKS_HEIGHT)))
  260.         {
  261.        
  262.         drawingAll();
  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)
  295. {  
  296.     int initial_tower, destination_tower, initial_row, destination_row, occupied_row, row_counter;
  297.  
  298.  
  299.     initial_tower=getkey()-'1';
  300.     destination_tower=getkey()-'1';
  301.    
  302.     if(initial_tower==('0'-'1'))
  303.         initial_tower=9;
  304.  
  305.     if(destination_tower==('0'-'1'))
  306.         destination_tower=9;   
  307.    
  308.    
  309.     if((initial_tower!=destination_tower)&&(initial_tower>=0)&&(initial_tower<TOWERS_NUMBER)&&(destination_tower>=0)&&(destination_tower<TOWERS_NUMBER))
  310.    
  311.         {
  312.         initial_row=DISKS_NUMBER;
  313.         occupied_row=DISKS_NUMBER;
  314.         disks_tab[DISKS_NUMBER][initial_tower]=DISKS_NUMBER;
  315.         disks_tab[DISKS_NUMBER][destination_tower]=DISKS_NUMBER;
  316.         for(row_counter=DISKS_NUMBER-1;row_counter>=0;row_counter--)
  317.             {
  318.             if(disks_tab[row_counter][initial_tower]!=0)
  319.                 {
  320.                 initial_row = row_counter;
  321.                     }
  322.             }
  323.         for(row_counter=DISKS_NUMBER-1;row_counter>=0;row_counter--)
  324.             {
  325.             if(disks_tab[row_counter][destination_tower]!=0)
  326.                 {
  327.                 occupied_row=row_counter;
  328.                     }
  329.             }
  330.         if((initial_row!=DISKS_NUMBER)&&(disks_tab[initial_row][initial_tower]<=disks_tab[occupied_row][destination_tower]))
  331.             moving(initial_tower,destination_tower, initial_row, occupied_row-1);
  332.        
  333.         }
  334.    
  335. }
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