Advertisement
Monika__

hanoi new new

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