SHARE
TWEET

HANOI SIE POSRAŁO

Mary_99 Mar 31st, 2019 90 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.  
  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. double disksHeight = TOWER_HEIGHT / (DISKS_NUMBER + 3);
  30. double disksWidth = DISK_WIDTH / TOWER_NUMBER
  31.  
  32. void initialingPositionOfDisks();
  33. void checkingBoundry();
  34. void drawingAll();
  35. int winning();
  36. void array_transport();
  37.  
  38. void howManyDisks(int);
  39. void moving(int,int,int,int);
  40. void verticalMove();
  41. void horizontalMove();
  42. void secondVerticalMove();
  43. void drawingBackgraund();
  44. void drawingTowers();
  45. void drawingDisks();
  46.  
  47. int main(int argc, char* argv[])
  48. {  
  49.     initialingPositionOfDisks();
  50.     if(initGraph())
  51.     {
  52.        exit(3);
  53.     }
  54.     do
  55.     {
  56.         checkingBoundry();
  57.         drawingAll();
  58.         updateScreen();
  59.         winning();
  60.         if(winning() == 1)
  61.         {
  62.             break;
  63.         }
  64.         array_transport();
  65.         updateScreen();
  66.     }while(isKeyDown(SDLK_ESCAPE) != 1);
  67.     return 0;
  68. }
  69.  
  70. void initialingPositionOfDisks(void)
  71. {
  72.     int row_counter, number_of_column;
  73.  
  74.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
  75.     {  
  76.         for(row_counter = 0; row_counter < DISKS_NUMBER; row_counter ++)  
  77.         {    
  78.             if(number_of_column == 0)
  79.             {
  80.                 disks_tab[row_counter][number_of_column] = row_counter + 1;
  81.             }
  82.             else
  83.             {
  84.             disks_tab[row_counter][number_of_column] = 0;
  85.             }
  86.         }
  87.     }
  88.  
  89. }
  90.  
  91. void checkingBoundry()
  92. {
  93.     if(DISKS_NUMBER < 3 || TOWERS_NUMBER > 10)
  94.     {
  95.         printf("Invalid number of Towers \n");
  96.         exit(3);
  97.     }
  98.  
  99.     if(DISKS_NUMBER < 3 || DISKS_NUMBER >15)
  100.     {
  101.         printf("Invalid number of Disks.\n");
  102.         exit(3);
  103.     }
  104.  
  105. }
  106.  
  107. void drawingAll(void)
  108. {
  109.     drawingBackgraund();
  110.     drawingTowers();
  111.     drawingDisks();
  112. }
  113.  
  114.    
  115. void drawingBackgraund(void)
  116. {
  117.     int row_counter, number_of_column;
  118.    
  119.     filledRect(0, 0, screenWidth(), screenHeight(), BACKGROUND_COLOR);
  120.     filledRect(0, screenHeight()-FLOOR_HEIGHT, screenWidth(), screenHeight(), FLOOR_COLOR);
  121.    
  122. }
  123.  
  124. void drawingTowers(void)
  125. {
  126.     int row_counter, number_of_column;
  127.    
  128.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)  
  129.     {
  130.         filledRect((number_of_column * 2 + 1) * screenWidth() / (number_of_towers * 2)-TOWERS_WIDTH / 2,
  131.         screenHeight()-((number_of_disks + 1) * DISKS_HEIGHT+FLOOR_HEIGHT),
  132.         (number_of_column * 2 + 1) * screenWidth() /(number_of_towers * 2) + TOWERS_WIDTH / 2, screenHeight() - FLOOR_HEIGHT - 1, TOWERS_COLOR);
  133.     }
  134. }
  135.  
  136.  
  137. void drawingDisks(void)
  138. {
  139.     int row_counter, number_of_column;
  140.    
  141.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
  142.     {
  143.         for(row_counter = 0; row_counter < DISKS_NUMBER; row_counter ++)
  144.         {
  145.             if(disks_tab[row_counter][number_of_column] != 0)
  146.             {
  147.                 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,
  148.                 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);
  149.            
  150.                 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);
  151.             }
  152.         }
  153.     }
  154.    
  155. }
  156.  
  157. int winning(void)
  158. {  
  159.     int exit;
  160.     int number_column;
  161.     exit = 0;
  162.     for(number_column = 1; number_column < TOWERS_NUMBER; number_column ++)
  163.     {
  164.         howManyDisks(number_column);
  165.         if (how_many_disks_on_the_current_tower_tab[number_column] == DISKS_NUMBER)
  166.         {
  167.             textout(screenWidth() / 2 - 30, screenHeight() / 2 - 50, "YOU WON !!!", MAGENTA);
  168.             updateScreen();
  169.             getkey();
  170.             exit = 1;
  171.         }
  172.     }  
  173.     return exit;      
  174. }
  175.  
  176. void array_transport(void)
  177. {  
  178.     int start_tower, end_tower;
  179.     int start_row;
  180.     int occupied_row, row_counter;
  181.  
  182.     start_tower = getkey() - '1';
  183.     end_tower = getkey() - '1';
  184.    
  185.     if(start_tower == ('0' - '1'))
  186.     {
  187.         start_tower = 9;
  188.     }
  189.     if(end_tower == ('0' - '1'))
  190.     {
  191.         end_tower = 9;  
  192.     }
  193.    
  194.     if((start_tower != end_tower) && (start_tower >= 0) && (start_tower < TOWERS_NUMBER) && (end_tower >= 0) && (end_tower < TOWERS_NUMBER))
  195.     {
  196.         start_row = DISKS_NUMBER;
  197.         occupied_row = DISKS_NUMBER;
  198.         disks_tab[DISKS_NUMBER][start_tower] = DISKS_NUMBER;
  199.         disks_tab[DISKS_NUMBER][end_tower] = DISKS_NUMBER;
  200.        
  201.         for(row_counter=DISKS_NUMBER - 1; row_counter >=0; row_counter --)//looks for last disk on initial tower
  202.         {
  203.             if(disks_tab[row_counter][start_tower]!=0)
  204.             {
  205.                 start_row = row_counter;
  206.             }
  207.         }
  208.         for(row_counter = DISKS_NUMBER - 1; row_counter >= 0; row_counter --)//looks for last disk on last tower
  209.         {
  210.             if(disks_tab[row_counter][end_tower] != 0)
  211.             {
  212.                 occupied_row = row_counter;
  213.             }
  214.         }
  215.         if((start_row != DISKS_NUMBER) && (disks_tab[start_row][start_tower] <= disks_tab[occupied_row][end_tower]))
  216.         {
  217.             moving(start_tower, end_tower, start_row, occupied_row - 1);
  218.         }
  219.     }
  220. }
  221.  
  222. void howManyDisks(int number_of_towers)
  223. {
  224.     int row_counter;
  225.    
  226.     how_many_disks_on_the_current_tower_tab[number_of_towers] = 0;
  227.        
  228.     for(row_counter = 0; row_counter < DISKS_NUMBER; row_counter ++)//check if current tower is occupied
  229.     {
  230.         if(disks_tab[row_counter][number_of_towers]!= 0)
  231.         {
  232.             how_many_disks_on_the_current_tower_tab[number_of_towers] += 1;
  233.         }
  234.     }
  235. }
  236.  
  237. void moving(start_tower, end_tower, start_row, end_row)
  238.  
  239. {
  240.     int position_y, position_x, disk_value, direction;
  241.  
  242.     howManyDisks(start_tower);
  243.  
  244.     disk_value = disks_tab[start_row][start_tower];
  245.     disks_tab[start_row][start_tower] = 0;
  246.  
  247.     position_y = screenHeight() - (FLOOR_HEIGHT + DISKS_HEIGHT * how_many_disks_on_the_current_tower_tab[start_tower]);
  248.     position_x = (start_tower * 2 + 1) * screenWidth() / (TOWERS_NUMBER * 2);
  249.    
  250.     if(start_tower < end_tower)
  251.     {
  252.         direction = 1;
  253.     }
  254.     if(start_tower > end_tower)
  255.     {
  256.         direction = -1;
  257.     }
  258.     //verticalMove(start_tower, end_tower, start_row, end_row);
  259.     while((position_y + STEP_PER_ITERATION_OF_MOVING_DISC) >= (screenHeight() - (FLOOR_HEIGHT + (DISKS_NUMBER + 4) * DISKS_HEIGHT)))
  260.     {
  261.         drawingAll();
  262.        
  263.         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);
  264.         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);
  265.  
  266.         position_y -= STEP_PER_ITERATION_OF_MOVING_DISC;
  267.         updateScreen();
  268.         //horizontalMove(start_tower, end_tower, start_row, end_row);
  269.     }
  270.    
  271.     while(abs(position_x - (end_tower * 2 + 1) * screenWidth() / (TOWERS_NUMBER * 2)) >= STEP_PER_ITERATION_OF_MOVING_DISC)
  272.     {
  273.         drawingAll();
  274.    
  275.         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);
  276.         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);
  277.        
  278.         position_x = position_x + direction * STEP_PER_ITERATION_OF_MOVING_DISC;
  279.         updateScreen();
  280.     }
  281.    
  282.     howManyDisks(end_tower);
  283.    // secondVerticalMove(start_tower, end_tower, start_row, end_row);
  284.     while(position_y < (screenHeight() - (FLOOR_HEIGHT + (how_many_disks_on_the_current_tower_tab[end_tower] + 1) * DISKS_HEIGHT)))
  285.     {
  286.         drawingAll();
  287.    
  288.         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);
  289.         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);
  290.  
  291.         position_y += STEP_PER_ITERATION_OF_MOVING_DISC;
  292.         updateScreen();
  293.     }
  294.     disks_tab[end_row][end_tower] = disk_value;
  295.    
  296.    
  297.    
  298. }
  299.  
  300. /*void verticalMove(start_tower, end_tower, start_row, end_row)
  301. {
  302.     int position_y, position_x, disk_value, direction;
  303.  
  304.     howManyDisks(start_tower);
  305.  
  306.     disk_value = disks_tab[start_row][start_tower];
  307.     disks_tab[start_row][start_tower] = 0;
  308.  
  309.     position_y = screenHeight() - (FLOOR_HEIGHT + DISKS_HEIGHT * how_many_disks_on_the_current_tower_tab[start_tower]);
  310.     position_x = (start_tower * 2 + 1) * screenWidth() / (TOWERS_NUMBER * 2);
  311.     while((position_y + STEP_PER_ITERATION_OF_MOVING_DISC) >= (screenHeight() - (FLOOR_HEIGHT + (DISKS_NUMBER + 4) * DISKS_HEIGHT)))
  312.     {
  313.         drawingAll();
  314.        
  315.         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);
  316.         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);
  317.  
  318.         position_y -= STEP_PER_ITERATION_OF_MOVING_DISC;
  319.         updateScreen();
  320.         horizontalMove(start_tower, end_tower, start_row, end_row);
  321.     }
  322. }
  323.  
  324. void horizontalMove(start_tower, end_tower, start_row, end_row)
  325. {
  326.     int position_y, position_x, disk_value, direction;
  327.  
  328.     howManyDisks(start_tower);
  329.  
  330.     disk_value = disks_tab[start_row][start_tower];
  331.     disks_tab[start_row][start_tower] = 0;
  332.  
  333.     position_y = screenHeight() - (FLOOR_HEIGHT+DISKS_HEIGHT * how_many_disks_on_the_current_tower_tab[start_tower]);
  334.     position_x = (start_tower * 2 + 1) * screenWidth() / (TOWERS_NUMBER * 2);
  335.     while(abs(position_x - (end_tower * 2 + 1) * screenWidth() / (TOWERS_NUMBER * 2)) >= STEP_PER_ITERATION_OF_MOVING_DISC)
  336.     {
  337.         drawingAll();
  338.    
  339.         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);
  340.         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);
  341.        
  342.         position_x = position_x + direction * STEP_PER_ITERATION_OF_MOVING_DISC;
  343.         updateScreen();
  344.     }
  345.    
  346.     howManyDisks(end_tower);
  347.     secondVerticalMove(start_tower, end_tower, start_row, end_row);
  348. }
  349.  
  350. void secondVerticalMove(start_tower, end_tower, start_row, end_row)
  351. {
  352.     int position_y, position_x, disk_value, direction;
  353.  
  354.     howManyDisks(start_tower);
  355.  
  356.     disk_value = disks_tab[start_row][start_tower];
  357.     disks_tab[start_row][start_tower] = 0;
  358.  
  359.     position_y = screenHeight() - (FLOOR_HEIGHT + DISKS_HEIGHT * how_many_disks_on_the_current_tower_tab[start_tower]);
  360.     position_x = (start_tower * 2 + 1) * screenWidth() / (TOWERS_NUMBER * 2);
  361.     while(position_y < (screenHeight() - (FLOOR_HEIGHT + (how_many_disks_on_the_current_tower_tab[end_tower] + 1) * DISKS_HEIGHT)))
  362.     {
  363.         drawingAll();
  364.    
  365.         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);
  366.         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);
  367.  
  368.         position_y += STEP_PER_ITERATION_OF_MOVING_DISC;
  369.         updateScreen();
  370.     }
  371.     disks_tab[end_row][end_tower] = disk_value;
  372. }
  373. */
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