Advertisement
Mary_99

HanOI

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