SHARE
TWEET

hanoi try

Mary_99 Mar 26th, 2019 14 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 BLUE
  10. #define TEXT_COLOR MAGENTA
  11. #define TOWERS_NUMBER 5
  12. #define DISKS_NUMBER 4
  13. #define TOWERS_WIDTH 6
  14. #define FLOOR_HEIGHT 30
  15. #define DISKS_WIDTH 3
  16. /*The width of the smallest disk-tower's width/2  */
  17. #define STEP_PER_ITERATION_OF_MOVING_DISC 2
  18. #define DISKS_HEIGHT 10
  19.  
  20.  
  21.  
  22. /* global variables */
  23.  
  24. int how_many_disks_on_the_current_tower_tab[TOWERS_NUMBER];
  25.  
  26. int disks_sizes_on_current_position_tab[DISKS_NUMBER + 1][TOWERS_NUMBER];
  27.  
  28. float number_of_towers = TOWERS_NUMBER;
  29.  
  30. float number_of_disks = DISKS_NUMBER;
  31.  
  32. void filledRect(int x1, int y1, int x2, int y2, enum color c);
  33.  
  34.  
  35.  
  36.  
  37. void drawing(void) /*This function is drawing on the screen everything that is going on in the programm: backgroung, floor, disks, towers */
  38. {
  39.     int number_of_rows;
  40.     int number_of_column;
  41.    
  42.     filledRect(0, 0, screenWidth(), screenHeight(), BACKGROUND_COLOR);
  43.    
  44.     filledRect(0, screenHeight()-FLOOR_HEIGHT, screenWidth(), screenHeight(), FLOOR_COLOR);
  45.    
  46.    
  47.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column++) /* drawing towers*/
  48.         {
  49.             filledRect((number_of_column*2+1)*screenWidth()/(number_of_towers*2)-TOWERS_WIDTH/2, screenHeight()-((number_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);
  50.         }
  51.  
  52.     for(number_of_column=0;number_of_column<TOWERS_NUMBER;number_of_column++)  /*This two loops create disks and their frames according to the current position of disks written in the array*/
  53.         {
  54.         for(number_of_rows=0;number_of_rows<DISKS_NUMBER;number_of_rows++)
  55.                 {
  56.             if(disks_sizes_on_current_position_tab[number_of_rows][number_of_column]!=0)
  57.                 {
  58.                 filledRect(screenWidth()/(number_of_towers*2)*(1+2*number_of_column)-DISKS_WIDTH*disks_sizes_on_current_position_tab[number_of_rows[number_of_column]-TOWERS_WIDTH/2 screenHeight()-(number_of_disks-number_of_rows)*DISKS_HEIGHT-FLOOR_HEIGHT, screenWidth()/(number_of_towers*2)*(1+2*number_of_column)+DISKS_WIDTH*disks_sizes_on_current_position_tab[number_of_rows][number_of_column]+TOWERS_WIDTH/2, screenHeight()-((number_of_disks-number_of_rows-1)*DISKS_HEIGHT)-FLOOR_HEIGHT-1, DISKS_COLOR);
  59.                
  60.                 rect(screenWidth()/(number_of_towers*2)*(1+2*number_of_column)-DISKS_WIDTH*disks_sizes_on_current_position_tab[number_of_rows][number_of_column]-TOWERS_WIDTH/2,      screenHeight()-(number_of_disks - number_of_rows)*DISKS_HEIGHT - FLOOR_HEIGHT, screenWidth()/(number_of_towers*2)*(1+2*number_of_column)+DISKS_WIDTH*disks_sizes_on_current_position_tab[number_of_rows][number_of_column]+TOWERS_WIDTH/2, screenHeight()-((number_of_disks-number_of_rows-1)*DISKS_HEIGHT)-FLOOR_HEIGHT-1, FRAME_COLOR);
  61.                 }
  62.                 }
  63.             }
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. /* functions declarations */
  76.  
  77.  
  78. //void array_transport(void);
  79. /*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*/
  80.  
  81.  
  82. //void moving(int,int,int,int); /*This function is responsible for the movement of the disks*/
  83.  
  84. //oid how_many_disks_on_the_current_tower(int);
  85. /*This function calculates how many disks are on the current tower and writes it into the array how_many_disks[]*/
  86.  
  87. //void initial_state(void); /*This function writes the initial position of the disks into the array*/
  88.  
  89. //int winning(void); /* This function prints the text when the user wins the game*/
  90.  
  91.  
  92.  
  93. int main(int argc, char* argv[])
  94. {  
  95.  
  96.     //initial_state(); /*This function writes the initial position of the disks into the array*/
  97.  
  98.     if(initGraph()) /*Initialization of graphics. Exit on error*/
  99.        exit(3);
  100.    
  101.     do
  102.         {
  103.         drawing(); /*This function is drawing on the screen everything that is going on in the programm: backgroung, floor, disks, towers */
  104.         updateScreen();
  105.    
  106.         //winning(); /* This function prints the text when the user wins the game*/
  107.         //if(winning()==1) /*Function 'winning()' returns value 1 if the user won the game so the main loop of the game is terminated by the 'break' expression*/
  108.         //  break;
  109.  
  110.         //array_transport(); /*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*/
  111.         //updateScreen();
  112.  
  113.         }while(pollkey()!=SDLK_ESCAPE); /*This loop makes the programm to work until the user will close the window*/
  114.    
  115.     return 0;
  116. }
  117.  
  118.  
  119. /*
  120. void initial_state(void) /*This function writes the initial position of the disks into the array*/
  121. //{
  122.     //int number_of_rows, column;
  123.     /*row - counter in operations concerning number of rows
  124.       column - counter in operations concerning number of columns*/
  125.  
  126.     //for(column=0;column<TOWERS_NUMBER;column++)
  127.     //  {  
  128.     //  for(number_of_rows=0;number_of_rows<DISKS_NUMBER;number_of_rows++)  /*This loop writes the initial state of disks on towers into the array 'disks_arr'*/
  129.             //  {    
  130.             //  if(column==0)
  131.             //  disks_sizes_on_current_position_tab[number_of_rows][column]=number_of_rows+1;
  132.             ///else
  133.             //  disks_sizes_on_current_position_tab[number_of_rows][column]=0;
  134.         //  }
  135.     //  }
  136.  
  137. //}*/
  138.  
  139.  
  140.  
  141.  
  142.  
  143. //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*/
  144. //{
  145.     //int initial_tower, destination_tower, initial_row, destination_row, occupied_row, number_of_rows;
  146.  
  147.     /*initial_row - the first row from above occupied by disk on the initial tower
  148.       occupied_row - the first occupied row(from above) on the destination tower
  149.       destination_row - row on the destination tower where the disk will be moved
  150.       row - counter when operating on rows*/
  151.  
  152.     /*initial_tower=getkey()-'1'; /*This variable represents the initial tower on which was located the disk that will be moved*/
  153.     //destination_tower=getkey()-'1'; /*This variable represents the tower on which the disk will be placed after the movement*/
  154.    
  155.     /*if(initial_tower==('0'-'1'))
  156.         initial_tower=9;
  157.  
  158.     if(destination_tower==('0'-'1'))
  159.         destination_tower=9;    */
  160.     /*These if functions make the programm to work on the 10th tower when the user presses '0'*/
  161.    
  162.     //if((initial_tower!=destination_tower)&&(initial_tower>=0)&&(initial_tower<TOWERS_NUMBER)&&(destination_tower>=0)&&(destination_tower<TOWERS_NUMBER))
  163.     /*This if function ckecks whether the initial and destination towers aren't the same and belong to the interval between 0 and TOWERS_NUMBER*/
  164.         //{
  165.         //initial_row=DISKS_NUMBER;
  166.         //occupied_row=DISKS_NUMBER;
  167.         //disks_sizes_on_current_position_tab[DISKS_NUMBER][initial_tower]=DISKS_NUMBER;
  168.         //disks_sizes_on_current_position_tab[DISKS_NUMBER][destination_tower]=DISKS_NUMBER;
  169.         //for(number_of_rows=DISKS_NUMBER-1;number_of_rows>=0;number_of_rows--)/*This loop looks for the last (from below) disk on the initial tower and writes its row coordinate as initial_row variable*/
  170.            
  171.             //{
  172.             //if(disks_sizes_on_current_position_tab[number_of_rows][initial_tower]!=0)
  173.                 //{
  174.                 //initial_row=number_of_rows;
  175.                     //}
  176.             //}
  177.         //for(number_of_rows=DISKS_NUMBER-1;number_of_rows>=0;number_of_rows--)/*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*/
  178.         //  {
  179.         //  if(disks_sizes_on_current_position_tab[number_of_rows][destination_tower]!=0)
  180.           //        {
  181.           //        occupied_row=number_of_rows;
  182.                //       }
  183.         //  }
  184.     //  if((initial_row!=DISKS_NUMBER)&&(disks_sizes_on_current_position_tab[initial_row][initial_tower]<=disks_sizes_on_current_position_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 w//hich we want to place it*/
  185.         //  moving(initial_tower,destination_tower, initial_row, occupied_row-1);
  186.         //
  187.         //}
  188.    
  189. //}
  190.  
  191.  
  192.  
  193. //void NumberOfDisksOnTheCurrentTower(int tower)/*This function calculates how many disks are on the current tower*/
  194. //{
  195.         //int number_of_rows
  196.    
  197.    
  198.     //how_many_disks_on_the_current_tower_tab[tower]=0;
  199.        // for(number_of_rows = 0; number_of_rows < DISKS_NUMBER; number_of_rows++)
  200.           //    {
  201.            //   if(disks_sizes_on_current_position_tab[number_of_rows][tower]!=0)
  202.                 //  {
  203.                 //  how_many_disks_on_the_current_tower_tab[tower]+=1;
  204.                 //  }
  205.            //   }
  206. //}
  207.  
  208.  
  209. //int winning(void) /* This function prints the text when the user wins the game*/
  210. //{
  211.     ///int exit,column;
  212.     //exit=0;
  213.     /*column - counter when operating on columns
  214.       exit - this variable is equal to 1 if the user has won the game and this value is returned by the function*/
  215.  
  216.     //for(column=1;column<TOWERS_NUMBER;column++) /*This loop calls the fucntion that calculates how many disks are on a current tower for every tower except the first one. If the number of disks on a tower is equal to the number of all disks then it prints on the screen 'YOU WON'*/
  217.         //{
  218.         ///how_many_disks(column);
  219.         //if (how_many_disks_on_the_current_tower_tab[column]==DISKS_NUMBER)
  220.             /*{textout(screenWidth()/2-30, screenHeight()/2-50, "YOU WON !!!", TEXT_COLOR);
  221.             updateScreen();
  222.             getkey();
  223.             exit=1;
  224.             }
  225.             }   */
  226.     //return exit;     
  227. //}
  228.  
  229.  
  230.  
  231. //void moving(initial_tower,destination_tower,initial_row,destination_row)
  232. /*This function is responsible for the movement of the disks*/
  233. //{
  234.     //int position_y, position_x, disk_value, direction;
  235.     /*position_y - This variable represents the vertical position of the bottom of moving disk
  236.       position_x - This variable represents the horizontal position of the centre of moving disk
  237.       disk_value - This variable represents the value stored in 'disks_arr' for the disk we want to move
  238.       direction - this variable is responsible for determining the direction in which the disk will be moving on the horizontal axes. It acquires only two values: '1' - for movement from left to right, '-1' - for movement from right to left*/
  239.  
  240.     /*how_many_disks_on_the_current_tower(initial_tower); /*Calculation of how many disks are on the initial tower */
  241.  
  242.     /*disk_value=disks_sizes_on_current_position_tab[initial_row][initial_tower];
  243.     disks_sizes_on_current_position_tab[initial_row][initial_tower]=0; /*The disk which will be moved is ereased from the array so it would not appear on its initial static position during screen updating*/
  244.  
  245.     /*position_y=screenHeight()-(FLOOR_HEIGHT+DISKS_HEIGHT*how_many_disks_on_the_current_tower_tab[initial_tower]);
  246.     position_x=(initial_tower*2+1)*screenWidth()/(TOWERS_NUMBER*2);
  247.  
  248.     if(initial_tower<destination_tower)
  249.         direction=1;
  250.     if(initial_tower>destination_tower)
  251.         direction=-1;*/
  252.  
  253.     /*This while loop is responsible for the first part of the vertical move of the disks*/
  254.     /*while((position_y+STEP_PER_ITERATION_OF_MOVING_DISC)>=(screenHeight()-(FLOOR_HEIGHT+(DISKS_NUMBER+4)*DISKS_HEIGHT)))
  255.         {
  256.         drawing(); /*This function is drawing on the screen everything that is going on in the programm: backgroung, floor, disks, towers */
  257.         /*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);
  258.         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);
  259.  
  260.         position_y-=STEP_PER_ITERATION_OF_MOVING_DISC;
  261.         updateScreen();
  262.         } */
  263.  
  264.    
  265.     /*This while loop is responsible for the horizontal move */
  266.     /*while(abs(position_x-(destination_tower*2+1)*screenWidth()/(TOWERS_NUMBER*2))>=STEP_PER_ITERATION_OF_MOVING_DISC)
  267.         {
  268.         drawing(); /*This function is drawing on the screen everything that is going on in the programm: backgroung, floor, disks, towers */
  269.         /*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);
  270.         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);
  271.        
  272.         position_x=position_x+direction*STEP_PER_ITERATION_OF_MOVING_DISC;
  273.         updateScreen();
  274.         }
  275.    
  276.     how_many_disks(destination_tower); *//*Calculation of how many disks are on the destination tower*/
  277.  
  278.     /*This while loop is responsible for the second part of the vertical move of the disks*/
  279.     /*while(position_y<(screenHeight()-(FLOOR_HEIGHT+(how_many_disks_on_the_current_tower_tab[destination_tower]+1)*DISKS_HEIGHT)))
  280.         {
  281.         drawing(); /*This function is drawing on the screen everything that is going on in the programm: backgroung, floor, disks, towers */
  282.         /*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);
  283.         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);
  284.  
  285.         position_y+=STEP_PER_ITERATION_OF_MOVING_DISC;
  286.         updateScreen();
  287.         }
  288.     disks_sizes_on_current_position_tab[destination_row][destination_tower]=disk_value; */
  289.     /*This line changes the values of the disks_arr after the movement is done so that the disks that has been moved are also printed on the screen on their new static positions by drawing function*/
  290.  
  291. //}
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