Advertisement
Mary_99

HANOI ODDANIE

Apr 3rd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.81 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 9
  13. #define TOWERS_WIDTH 5
  14. #define TOWERS_HIGHT 300
  15.  
  16. #define DISKS_NUMBER 15
  17. #define DISKS_WIDTH 15
  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 disks_height = TOWERS_HIGHT / (DISKS_NUMBER + 5);
  30. double disks_width = (DISKS_WIDTH + 6) / TOWERS_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 moveUp(int, int, int, int, int);
  40. void moveSide(int, int, int, int, int);
  41. void MoveDown(int, int, int, int, int);
  42. void moving(int,int,int,int);
  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. void checkingBoundry()
  91. {
  92.     if(DISKS_NUMBER < 3 || TOWERS_NUMBER > 10)
  93.     {
  94.         printf("Invalid number of Towers \n");
  95.         exit(3);
  96.     }
  97.  
  98.     if(DISKS_NUMBER < 3 || DISKS_NUMBER >15)
  99.     {
  100.         printf("Invalid number of Disks.\n");
  101.         exit(3);
  102.     }
  103. }
  104.  
  105. void drawingAll(void)
  106. {
  107.     drawingBackgraund();
  108.     drawingTowers();
  109.     drawingDisks();
  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. void drawingTowers(void)
  121. {
  122.     int row_counter, number_of_column;
  123.    
  124.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)  
  125.     {
  126.         filledRect((number_of_column * 2 + 1) * screenWidth() / (number_of_towers * 2)-TOWERS_WIDTH / 2,
  127.         screenHeight() - TOWERS_HIGHT + FLOOR_HEIGHT ,
  128.         (number_of_column * 2 + 1) * screenWidth() / (number_of_towers * 2) + TOWERS_WIDTH / 2, screenHeight() - FLOOR_HEIGHT - 1, TOWERS_COLOR);
  129.     }
  130. }
  131.  
  132.  
  133. void drawingDisks(void)
  134. {
  135.     int row_counter, number_of_column;
  136.    
  137.     for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
  138.     {
  139.         for(row_counter = 0; row_counter < DISKS_NUMBER; row_counter ++)
  140.         {
  141.             if(disks_tab[row_counter][number_of_column] != 0)
  142.             {
  143.                 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,
  144.                 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);
  145.            
  146.                 rect(screenWidth() / (number_of_towers * 2) * (1 + 2 * number_of_column) - disks_width * disks_tab[row_counter][number_of_column] -                TOWERS_WIDTH / 2,
  147.                      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,
  148.                      screenHeight() -((number_of_disks - row_counter - 1) * disks_height) - FLOOR_HEIGHT - 1, FRAME_COLOR);
  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, destination_tower, start_row, destination_row)
  235.  
  236. {
  237.     int vertical_position_of_the_bottom_of_moving_disk;
  238.     int horizontal_position_of_the_centre_of_moving_disk;
  239.     int valu_for_the_disk_to_move;
  240.     int direction_on_horizontal_axes;
  241.  
  242.     howManyDisks(start_tower);
  243.  
  244.     valu_for_the_disk_to_move = disks_tab[start_row][start_tower];
  245.     disks_tab[start_row][start_tower] = 0;
  246.  
  247.     vertical_position_of_the_bottom_of_moving_disk = screenHeight() - (FLOOR_HEIGHT + DISKS_HEIGHT *    how_many_disks_on_the_current_tower_tab[start_tower]);
  248.    
  249.     horizontal_position_of_the_centre_of_moving_disk  = (start_tower * 2 + 1 ) * screenWidth() / (TOWERS_NUMBER * 2);
  250.  
  251.     if(start_tower < destination_tower)
  252.     {
  253.         direction_on_horizontal_axes = 1;
  254.     }
  255.     if(start_tower > destination_tower)
  256.     {
  257.         direction_on_horizontal_axes = -1;
  258.     }
  259.     moveUp(vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk, valu_for_the_disk_to_move, direction_on_horizontal_axes, destination_tower);
  260.    
  261.     disks_tab[destination_row][destination_tower] = valu_for_the_disk_to_move;
  262. }
  263.  
  264. void moveUp(int vertical_position_of_the_bottom_of_moving_disk, int horizontal_position_of_the_centre_of_moving_disk, int valu_for_the_disk_to_move,
  265.             int direction_on_horizontal_axes, int destination_tower)
  266. {
  267.     while((vertical_position_of_the_bottom_of_moving_disk+STEP_PER_ITERATION_OF_MOVING_DISC) >= (screenHeight() - (FLOOR_HEIGHT + (DISKS_NUMBER + 4) *  disks_height)))
  268.     {
  269.         drawingAll();
  270.    
  271.         filledRect(horizontal_position_of_the_centre_of_moving_disk - (TOWERS_WIDTH / 2 + valu_for_the_disk_to_move * disks_width), vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk + (TOWERS_WIDTH / 2 + valu_for_the_disk_to_move * disks_width), vertical_position_of_the_bottom_of_moving_disk +  disks_height, DISKS_COLOR);
  272.        
  273.         rect(horizontal_position_of_the_centre_of_moving_disk - (TOWERS_WIDTH / 2 + valu_for_the_disk_to_move * disks_width), vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk + (TOWERS_WIDTH / 2 + valu_for_the_disk_to_move * disks_width), vertical_position_of_the_bottom_of_moving_disk +  disks_height, FRAME_COLOR);
  274.  
  275.         vertical_position_of_the_bottom_of_moving_disk -= STEP_PER_ITERATION_OF_MOVING_DISC;
  276.         updateScreen();
  277.     }
  278.     moveSide(vertical_position_of_the_bottom_of_moving_disk,  horizontal_position_of_the_centre_of_moving_disk, valu_for_the_disk_to_move, direction_on_horizontal_axes, destination_tower);
  279. }
  280.  
  281.  
  282. void moveSide(int vertical_position_of_the_bottom_of_moving_disk, int horizontal_position_of_the_centre_of_moving_disk, int valu_for_the_disk_to_move,
  283.               int direction_on_horizontal_axes, int destination_tower)
  284. {
  285.     while(abs(horizontal_position_of_the_centre_of_moving_disk - (destination_tower * 2 + 1) * screenWidth() /(TOWERS_NUMBER * 2)) >= STEP_PER_ITERATION_OF_MOVING_DISC)
  286.     {
  287.         drawingAll();
  288.    
  289.         filledRect(horizontal_position_of_the_centre_of_moving_disk - (TOWERS_WIDTH / 2 + valu_for_the_disk_to_move * disks_width), vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk + (TOWERS_WIDTH / 2 + valu_for_the_disk_to_move*disks_width), vertical_position_of_the_bottom_of_moving_disk + disks_height, DISKS_COLOR);
  290.        
  291.         horizontal_position_of_the_centre_of_moving_disk = horizontal_position_of_the_centre_of_moving_disk + direction_on_horizontal_axes * STEP_PER_ITERATION_OF_MOVING_DISC;
  292.        
  293.         updateScreen();
  294.     }
  295.     moveDown( vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk, valu_for_the_disk_to_move, direction_on_horizontal_axes, destination_tower);
  296. }
  297.  
  298. void moveDown(int vertical_position_of_the_bottom_of_moving_disk, int horizontal_position_of_the_centre_of_moving_disk, int valu_for_the_disk_to_move,
  299.               int direction_on_horizontal_axes, int destination_tower)
  300. {
  301.     howManyDisks(destination_tower);
  302.    
  303.     while(vertical_position_of_the_bottom_of_moving_disk < (screenHeight() - (FLOOR_HEIGHT + (how_many_disks_on_the_current_tower_tab[destination_tower] + 1) *  disks_height)))
  304.     {
  305.         drawingAll();
  306.    
  307.         filledRect(horizontal_position_of_the_centre_of_moving_disk - (TOWERS_WIDTH / 2 + valu_for_the_disk_to_move * disks_width), vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk + (TOWERS_WIDTH /2 + valu_for_the_disk_to_move * disks_width), vertical_position_of_the_bottom_of_moving_disk +  disks_height, DISKS_COLOR);
  308.        
  309.         vertical_position_of_the_bottom_of_moving_disk += STEP_PER_ITERATION_OF_MOVING_DISC;
  310.         updateScreen();
  311.     }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement