Advertisement
Mary_99

hanoi bez skalowania

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