Advertisement
Monika__

hanoi_new

Mar 27th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.33 KB | None | 0 0
  1. #include "primlib.h"
  2. #define NUMBER_OF_RODS 4
  3. #define HEIGHT_OF_GROUND  (screenHeight() - WIDTH_GROUND)
  4. #define WIDTH_GROUND 50
  5. #define WIDTH_OF_ROD 5
  6. #define NUMBER_OF_DISKS 4
  7. #define STEP 2
  8. #define DISKS_WIDTH 10
  9. #define HEIGHT_OF_ROD 200
  10. #define DISKS_HEIGHT ((HEIGHT_OF_ROD - 15) / NUMBER_OF_DISKS)
  11. #define FRAME_COLOR BLACK
  12. #define DISKS_COLOR BLUE
  13. #define ROD_COLOR RED
  14. #define TOWERS_COLOR GREEN
  15. #define WIDTH_DIFFRENCE ((((screenWidth() / NUMBER_OF_RODS) - WIDTH_OF_ROD) / 2 - DISKS_WIDTH) / NUMBER_OF_DISKS)
  16.  
  17. int gameArray[NUMBER_OF_DISKS + 1][NUMBER_OF_RODS];
  18. int howManyDisksArr[NUMBER_OF_RODS];
  19.  
  20. double DistanceBetweenRods();
  21. void DrawRods();
  22. void DrawDisks();
  23. void Draw();
  24. void InitialGame();
  25. int FindingTopDisc(int tower);
  26. void MovingConditions();
  27. int Win();
  28. void HowManyDisks(int tower);
  29. void Animation(int startTower, int stopTower, int startRow, int stopRow);
  30. void MovingUp(int position_x, int position_y, int diskValue, int stopTower, int direction);
  31. void MovingHorizontally(int position_x, int position_y, int stopTower, int diskValue, int direction);
  32. void MovingDown(int position_x, int position_y, int stopTower, int diskValue);
  33.  
  34. int main()
  35. {
  36.     if(initGraph())
  37.     {
  38.         exit(3);
  39.     }
  40.  
  41.     InitialGame();
  42.  
  43.     while(1){
  44.  
  45.         filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
  46.         filledRect(0, screenHeight(), screenWidth(), screenHeight() - 50 , YELLOW);
  47.         DrawRods();
  48.         DrawDisks();
  49.         updateScreen();
  50.  
  51.  
  52.         if(Win() == 1)
  53.         {
  54.             break;
  55.         }
  56.         MovingConditions();
  57.         updateScreen();
  58.         if (isKeyDown(SDLK_SPACE) == 1){
  59.             break;
  60.         }
  61.     }
  62.  
  63.         SDL_Delay(40);
  64.  
  65.     return 0;
  66. }
  67.  
  68. void Draw()
  69. {
  70.   filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
  71.   filledRect(0, screenHeight(), screenWidth(), screenHeight() - 50 , YELLOW);
  72.   DrawRods();
  73.   DrawDisks();
  74.  
  75. }
  76.  
  77. double DistanceBetweenRods()
  78. {
  79.     return (screenWidth()/(NUMBER_OF_RODS + 1));
  80. }
  81.  
  82. void DrawRods()
  83. {
  84.     int column;
  85.     for(column = 0; column < NUMBER_OF_RODS; column++){
  86.  
  87.       filledRect((column * 2 + 1) * screenWidth() / (NUMBER_OF_RODS * 2) - WIDTH_OF_ROD / 2,
  88.                   screenHeight() - ((NUMBER_OF_DISKS + 1) * DISKS_HEIGHT + WIDTH_GROUND),
  89.                   (column * 2 + 1) * screenWidth() / (NUMBER_OF_RODS * 2) + WIDTH_OF_ROD / 2,
  90.                   screenHeight() - WIDTH_GROUND - 1, TOWERS_COLOR);
  91.         /*filledRect((DistanceBetweenRods() * iterationNumber) - WIDTH_OF_ROD, HEIGHT_OF_GROUND - 1,
  92.         (DistanceBetweenRods() * iterationNumber) + WIDTH_OF_ROD, HEIGHT_OF_GROUND - HEIGHT_OF_ROD , ROD_COLOR);
  93.     */}
  94. }
  95.  
  96. void DrawDisks()
  97. { //policzymy roznice szerokosci jaka maja krazki, podzielimy przez ich ilosc i to bedzie wartosc o jaką się bedą poszerzać
  98.     int row, column, individualWidth;
  99.         for(column = 0; column < NUMBER_OF_RODS; column++){
  100.       for(row = 0; row < NUMBER_OF_DISKS; row++){
  101.                 individualWidth = DISKS_WIDTH + (WIDTH_DIFFRENCE * gameArray[row][column]);
  102.         if(gameArray[row][column] != 0){
  103.                     /*if(individualWidth > ((screenWidth() / NUMBER_OF_RODS) - WIDTH_OF_ROD) / 2){
  104.                         individualWidth = ((screenWidth() / NUMBER_OF_RODS) - WIDTH_OF_ROD) / 2 + increment;
  105.                         increment += 2;
  106.                     }*/
  107.           filledRect(screenWidth() / (NUMBER_OF_RODS * 2) * ( 1 + 2 * column) - individualWidth - WIDTH_OF_ROD / 2,
  108.                     //(DistanceBetweenRods() * (column + 1)) - (DISKS_WIDTH * gameArray[row][column]),
  109.                                         HEIGHT_OF_GROUND - (NUMBER_OF_DISKS - row) * DISKS_HEIGHT,
  110.                                         //screenHeight() - (NUMBER_OF_DISKS - row) * DISKS_HEIGHT - WIDTH_GROUND,
  111.                     //                   screenHeight()-(NUMBER_OF_DISKS-row)*DISKS_HEIGHT- WIDTH_GROUND,
  112.                     screenWidth() / (NUMBER_OF_RODS * 2) * (1+2*column)+ individualWidth + WIDTH_OF_ROD/2,
  113.                     //(DistanceBetweenRods() * (column + 1)) + (DISKS_WIDTH * gameArray[row][column]),
  114.                     HEIGHT_OF_GROUND - ((NUMBER_OF_DISKS - row - 1) * DISKS_HEIGHT) - 1, DISKS_COLOR);
  115.                     //                   screenHeight()-((NUMBER_OF_DISKS-row-1)*DISKS_HEIGHT)- WIDTH_GROUND -1, DISKS_COLOR);
  116.  
  117.           rect(screenWidth() / (NUMBER_OF_RODS * 2) * (1 + 2 * column) - individualWidth - WIDTH_OF_ROD / 2,
  118.           //(DistanceBetweenRods() * (column + 1)) - (DISKS_WIDTH * gameArray[row][column]),
  119.           HEIGHT_OF_GROUND - (NUMBER_OF_DISKS - row) * DISKS_HEIGHT,
  120.           //              screenHeight()-(NUMBER_OF_DISKS-row)*DISKS_HEIGHT- WIDTH_GROUND,
  121.           screenWidth() / (NUMBER_OF_RODS * 2) * (1 + 2 * column) + individualWidth + WIDTH_OF_ROD / 2,
  122.           //              (DistanceBetweenRods() * (column + 1)) + (DISKS_WIDTH * gameArray[row][column]),
  123.           HEIGHT_OF_GROUND - ((NUMBER_OF_DISKS - row - 1) * DISKS_HEIGHT) - 1, FRAME_COLOR);
  124.           //              screenHeight()-((NUMBER_OF_DISKS-row-1)*DISKS_HEIGHT)- WIDTH_GROUND -1, FRAME_COLOR);
  125.  
  126.     }
  127.   }
  128. }
  129.  
  130.  
  131. }
  132.  
  133. void InitialGame()
  134. {
  135.   int i, j;
  136.   for(i = 0; i < NUMBER_OF_DISKS; i++)
  137.   {
  138.     for(j = 0; j < NUMBER_OF_RODS; j++)
  139.     {
  140.       if(j==0){
  141.         gameArray[i][j] = i + 1;
  142.       } else {
  143.         gameArray[i][j] = 0;
  144.       }
  145.     }
  146.   }
  147. }
  148.  
  149. int FindingTopDisc(int tower)
  150. {
  151.     int row, topDisc;
  152.     for(row = NUMBER_OF_DISKS; row >= 0; row--)
  153.     {
  154.         if(gameArray[row][tower] != 0)
  155.         {
  156.             topDisc = row;
  157.         }
  158.     }
  159.     return topDisc;
  160. }
  161.  
  162. void MovingConditions()
  163. {
  164.   int startTower, stopTower, startRow, stopRow;
  165.   startTower = getkey() - '1';
  166.   stopTower = getkey() - '1';
  167.  
  168.         if(startTower == ('0'-'1')){
  169.             startTower = 9;
  170.         }
  171.  
  172.         if(stopTower == ('0'-'1')){
  173.             stopTower = 9;
  174.         }
  175.  
  176.   if((startTower < NUMBER_OF_RODS) && (startTower >= 0) && (startTower != stopTower) &&
  177.      (stopTower >= 0) && (stopTower < NUMBER_OF_RODS))
  178.   {
  179.     startRow = NUMBER_OF_DISKS;
  180.     stopRow = NUMBER_OF_DISKS;
  181.     gameArray[NUMBER_OF_DISKS][startTower] = NUMBER_OF_DISKS;
  182.     gameArray[NUMBER_OF_DISKS][stopTower] = NUMBER_OF_DISKS;
  183.  
  184.     startRow = FindingTopDisc(startTower);
  185.     stopRow = FindingTopDisc(stopTower);
  186.  
  187.   if((startRow != NUMBER_OF_DISKS) && (gameArray[startRow][startTower] <= gameArray[stopRow][stopTower]))
  188.   {
  189.     Animation(startTower, stopTower, startRow, stopRow - 1 );
  190.   }
  191. }
  192. }
  193.  
  194. int Win()
  195. {
  196.   if(gameArray[0][NUMBER_OF_RODS - 1] == 1){
  197.     textout((screenWidth()/2) - 100, (screenHeight()/2) - 50, "YOU WON", RED);
  198.     updateScreen();
  199.     getkey();
  200.     return 1;
  201.   }
  202.   return 0;
  203. }
  204.  
  205. void HowManyDisks(int tower)
  206. {
  207.   int row;
  208.   howManyDisksArr[tower] = 0;
  209.   for(row = 0; row < NUMBER_OF_DISKS; row++)
  210.   {
  211.     if(gameArray[row][tower] != 0)
  212.     {
  213.       howManyDisksArr[tower] += 1;
  214.     }
  215.   }
  216. }
  217.  
  218. void Animation(int startTower, int stopTower, int startRow, int stopRow)
  219. {
  220.     int position_y, position_x, diskValue, direction;
  221.  
  222.     HowManyDisks(startTower);
  223.  
  224.     diskValue = gameArray[startRow][startTower];
  225.     gameArray[startRow][startTower] = 0;
  226.  
  227.     position_y = screenHeight() - (WIDTH_GROUND + DISKS_HEIGHT * howManyDisksArr[startTower]);
  228.     position_x = (startTower * 2 + 1) * screenWidth() / (NUMBER_OF_RODS * 2);
  229. printf("pozycja y zabierajac: %d\n", position_y);
  230.     if(startTower < stopTower)
  231.   {
  232.         direction = 1;
  233.   }
  234.   if(startTower > stopTower)
  235.     {
  236.     direction = -1;
  237.   }
  238.   MovingUp( position_x, position_y, diskValue, stopTower, direction);
  239.     HowManyDisks(stopTower);
  240.  
  241.   gameArray[stopRow][stopTower] = diskValue;
  242. }
  243.  
  244. void MovingUp(int position_x, int position_y, int diskValue, int stopTower, int direction)
  245. {
  246.   while((position_y + STEP) >= (screenHeight() - (WIDTH_GROUND + (NUMBER_OF_DISKS + 4) * DISKS_HEIGHT)))
  247.     {
  248.     Draw();
  249.     filledRect(position_x - (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE),
  250.                         position_y,
  251.                             position_x + (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE),
  252.                         position_y + DISKS_HEIGHT, DISKS_COLOR);
  253.  
  254.         rect(position_x - (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE), //(WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
  255.              position_y,
  256.          position_x + (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE), //(WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
  257.                  position_y + DISKS_HEIGHT, FRAME_COLOR);
  258.  
  259.     position_y -= STEP;
  260.     updateScreen();
  261.     }
  262.     MovingHorizontally(position_x, position_y, stopTower, diskValue, direction);
  263. }
  264.  
  265. void MovingHorizontally(int position_x, int position_y, int stopTower, int diskValue, int direction)
  266. {
  267.   while(abs(position_x - (stopTower * 2 + 1) * screenWidth() / (NUMBER_OF_RODS * 2)) >= STEP)
  268.         {
  269.         Draw();
  270.         filledRect(position_x - (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE),// (WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
  271.                             position_y,
  272.                         position_x + (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE), //(WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
  273.                         position_y + DISKS_HEIGHT, DISKS_COLOR);
  274.         rect(position_x - (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE), //(WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
  275.                  position_y,
  276.          position_x + (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE),//(WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
  277.                  position_y + DISKS_HEIGHT, FRAME_COLOR);
  278.  
  279.         position_x = position_x + direction * STEP;
  280.         updateScreen();
  281.         }
  282.     MovingDown(position_x, position_y, stopTower, diskValue);
  283. }
  284.  
  285. void MovingDown(int position_x, int position_y, int stopTower, int diskValue)
  286. {
  287.     printf("tyle jest dyskow na tej wiezy: %d\n", howManyDisksArr[stopTower]);
  288.     while((position_y + DISKS_HEIGHT) <= (screenHeight() - (WIDTH_GROUND + (howManyDisksArr[stopTower] + 1) * DISKS_HEIGHT)))
  289.         {
  290.         Draw();
  291.         filledRect(position_x - (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE), //(WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
  292.             position_y,
  293.       position_x + (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE), //(WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
  294.       position_y + DISKS_HEIGHT, DISKS_COLOR);
  295.         rect(position_x - (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE),
  296.             position_y,
  297.       position_x + (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE),
  298.       position_y + DISKS_HEIGHT, FRAME_COLOR);
  299.  
  300.         position_y += STEP;
  301.         updateScreen();
  302.         }
  303. printf("upuszczajac: %d\n", position_y );
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement