Advertisement
podsolnyxxx

+таймер 14

Apr 7th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <sstream>
  6. #include <cstring>
  7. #include "glut.h"
  8. const int SIZE = 6;
  9. const int TILE_SIZE = 100;
  10. int board[SIZE][SIZE] = { 0 };
  11. int originalBoard[SIZE][SIZE] = { 0 }; // Переменная для хранения копии доски
  12. int score = 0; // Переменная для хранения очков
  13. time_t startTime;
  14. time_t endTime;
  15.  
  16. void init() {
  17. glClearColor(0.9, 0.9, 0.9, 1.0);
  18. glMatrixMode(GL_PROJECTION);
  19. glLoadIdentity();
  20. gluOrtho2D(0, SIZE * TILE_SIZE, 0, SIZE * TILE_SIZE);
  21.  
  22. glLineWidth(2.0);
  23. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  24. }
  25.  
  26. void drawTile(int x, int y, int value) {
  27. glPushMatrix();
  28. glTranslatef(x, y, 0);
  29.  
  30. bool isCornerTile = (x == 0 && y == 0) || (x == 0 && y == (SIZE - 1) * TILE_SIZE) ||
  31. (x == (SIZE - 1) * TILE_SIZE && y == 0) || (x == (SIZE - 1) * TILE_SIZE && y == (SIZE - 1) * TILE_SIZE);
  32.  
  33. if (isCornerTile) {
  34. // Для угловых клеток используем черный цвет
  35. glColor3f(0.0f, 0.0f, 0.0f);
  36. }
  37. else if (x == 0 || y == 0 || x == (SIZE - 1) * TILE_SIZE || y == (SIZE - 1) * TILE_SIZE) {
  38. // Для внешних клеток, кроме угловых, используем темно-синий цвет
  39. glColor3f(0.1f, 0.1f, 0.5f);
  40. }
  41. else {
  42. switch (value) {
  43. case 2: glColor3f(0.9f, 0.9f, 0.9f); break;
  44. case 4: glColor3f(1.0f, 1.0f, 0.5f); break;
  45. case 8: glColor3f(1.0f, 0.5f, 0.0f); break;
  46. case 16: glColor3f(1.0f, 0.0f, 0.0f); break;
  47. case 32: glColor3f(0.8f, 0.6f, 1.0f); break;
  48. case 64: glColor3f(0.0f, 0.0f, 1.0f); break;
  49. case 128: glColor3f(0.0f, 1.0f, 1.0f); break;
  50. case 256: glColor3f(0.6f, 1.0f, 0.6f); break;
  51. case 512: glColor3f(0.0f, 1.0f, 0.0f); break;
  52. case 1024: glColor3f(0.0f, 0.5f, 0.0f); break;
  53. case 2048: glColor3f(1.0f, 0.0f, 1.0f); break;
  54. default: glColor3f(1.0f, 1.0f, 1.0f);
  55. }
  56. }
  57.  
  58. glBegin(GL_QUADS);
  59. glVertex2i(0, 0);
  60. glVertex2i(TILE_SIZE, 0);
  61. glVertex2i(TILE_SIZE, TILE_SIZE);
  62. glVertex2i(0, TILE_SIZE);
  63. glEnd();
  64.  
  65. if (value != 0) {
  66. glColor3f(0.0f, 0.0f, 0.0f);
  67. std::string text = std::to_string(value);
  68. glRasterPos2i(TILE_SIZE / 2 - 8 * text.length(), TILE_SIZE / 2 + 8);
  69. for (char c : text) {
  70. glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
  71. }
  72. }
  73.  
  74. glColor3f(0.0f, 0.0f, 0.0f);
  75. glBegin(GL_LINE_LOOP);
  76. glVertex2i(0, 0);
  77. glVertex2i(TILE_SIZE, 0);
  78. glVertex2i(TILE_SIZE, TILE_SIZE);
  79. glVertex2i(0, TILE_SIZE);
  80. glEnd();
  81.  
  82. glPopMatrix();
  83. }
  84.  
  85.  
  86. void generateTile() {
  87. std::vector<std::pair<int, int>> emptyCells;
  88. for (int i = 1; i < SIZE - 1; ++i) {
  89. for (int j = 1; j < SIZE - 1; ++j) {
  90. if (board[i][j] == 0) {
  91. emptyCells.push_back(std::make_pair(i, j));
  92. }
  93. }
  94. }
  95. if (emptyCells.empty()) {
  96. return;
  97. }
  98.  
  99. int value = (rand() % 10 == 0) ? 4 : 2;
  100.  
  101. int position = rand() % emptyCells.size();
  102. int x = emptyCells[position].first;
  103. int y = emptyCells[position].second;
  104. board[x][y] = value;
  105. }
  106.  
  107. bool checkWin() {
  108. for (int i = 0; i < SIZE; ++i) {
  109. for (int j = 0; j < SIZE; ++j) {
  110. if (board[i][j] == 2048) {
  111. return true;
  112. }
  113. }
  114. }
  115. return false;
  116. }
  117.  
  118. bool checkLose() {
  119. for (int i = 1; i < SIZE - 1; ++i) {
  120. for (int j = 1; j < SIZE - 1; ++j) {
  121. if (board[i][j] == 0) {
  122. return false; // Если есть пустая клетка внутри внешнего слоя, игра не закончена
  123. }
  124. if (board[i][j] == board[i][j - 1] || board[i][j] == board[i][j + 1] ||
  125. board[i][j] == board[i - 1][j] || board[i][j] == board[i + 1][j]) {
  126. return false; // Если соседние клетки имеют такое же значение, игра не закончена
  127. }
  128. }
  129. }
  130. return true; // Если все клетки внутри внешнего слоя заполнены и нет возможности объединить клетки, игра закончена
  131. }
  132.  
  133. void moveLeft() {
  134. for (int i = 1; i < SIZE - 1; ++i) {
  135. for (int j = 1; j < SIZE - 1; ++j) {
  136. if (board[i][j] == 0) {
  137. for (int k = j + 1; k < SIZE - 1; ++k) {
  138. if (board[i][k] != 0) {
  139. board[i][j] = board[i][k];
  140. board[i][k] = 0;
  141. break;
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148.  
  149. void mergeLeft() {
  150. for (int i = 1; i < SIZE - 1; ++i) {
  151. for (int j = 1; j < SIZE - 1; ++j) {
  152. if (board[i][j] == board[i][j + 1] && board[i][j] != 0) {
  153. board[i][j] *= 2;
  154. board[i][j + 1] = 0;
  155. score += board[i][j];
  156. }
  157. }
  158. }
  159. }
  160.  
  161. void moveRight() {
  162. for (int i = 1; i < SIZE - 1; ++i) {
  163. for (int j = SIZE - 2; j > 0; --j) {
  164. if (board[i][j] == 0) {
  165. for (int k = j - 1; k > 0; --k) {
  166. if (board[i][k] != 0) {
  167. board[i][j] = board[i][k];
  168. board[i][k] = 0;
  169. break;
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176.  
  177. void mergeRight() {
  178. for (int i = 1; i < SIZE - 1; ++i) {
  179. for (int j = SIZE - 2; j > 0; --j) {
  180. if (board[i][j] == board[i][j - 1] && board[i][j] != 0) {
  181. board[i][j] *= 2;
  182. board[i][j - 1] = 0;
  183. score += board[i][j];
  184. }
  185. }
  186. }
  187. }
  188.  
  189. void moveUp() {
  190. for (int j = 1; j < SIZE - 1; ++j) {
  191. for (int i = 1; i < SIZE - 1; ++i) {
  192. if (board[i][j] == 0) {
  193. for (int k = i + 1; k < SIZE - 1; ++k) {
  194. if (board[k][j] != 0) {
  195. board[i][j] = board[k][j];
  196. board[k][j] = 0;
  197. break;
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204.  
  205. void mergeUp() {
  206. for (int j = 1; j < SIZE - 1; ++j) {
  207. for (int i = 1; i < SIZE - 1; ++i) {
  208. if (board[i][j] == board[i + 1][j] && board[i][j] != 0) {
  209. board[i][j] *= 2;
  210. board[i + 1][j] = 0;
  211. score += board[i][j];
  212. }
  213. }
  214. }
  215. }
  216.  
  217. void moveDown() {
  218. for (int j = 1; j < SIZE - 1; ++j) {
  219. for (int i = SIZE - 2; i > 0; --i) {
  220. if (board[i][j] == 0) {
  221. for (int k = i - 1; k > 0; --k) {
  222. if (board[k][j] != 0) {
  223. board[i][j] = board[k][j];
  224. board[k][j] = 0;
  225. break;
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }
  232.  
  233. void mergeDown() {
  234. for (int j = 1; j < SIZE - 1; ++j) {
  235. for (int i = SIZE - 2; i > 0; --i) {
  236. if (board[i][j] == board[i - 1][j] && board[i][j] != 0) {
  237. board[i][j] *= 2;
  238. board[i - 1][j] = 0;
  239. score += board[i][j];
  240. }
  241. }
  242. }
  243. }
  244.  
  245. void handleKeypress(unsigned char key, int x, int y) {
  246. if (key == 27) {
  247. exit(0);
  248. }
  249. }
  250.  
  251. void updateOriginalBoard() {
  252. std::memcpy(originalBoard, board, SIZE * SIZE * sizeof(int));
  253. }
  254.  
  255. void handleSpecialKeypress(int key, int x, int y) {
  256. int originalBoard[SIZE][SIZE];
  257. std::memcpy(originalBoard, board, SIZE * SIZE * sizeof(int));
  258.  
  259. switch (key) {
  260. case GLUT_KEY_LEFT:
  261. moveLeft();
  262. mergeLeft();
  263. moveLeft();
  264. break;
  265. case GLUT_KEY_RIGHT:
  266. moveRight();
  267. mergeRight();
  268. moveRight();
  269. break;
  270. case GLUT_KEY_UP:
  271. moveUp();
  272. mergeUp();
  273. moveUp();
  274. break;
  275. case GLUT_KEY_DOWN:
  276. moveDown();
  277. mergeDown();
  278. moveDown();
  279. break;
  280. }
  281.  
  282. bool moved = false;
  283. for (int i = 0; i < SIZE; ++i) {
  284. for (int j = 0; j < SIZE; ++j) {
  285. if (originalBoard[i][j] != board[i][j]) {
  286. moved = true;
  287. break;
  288. }
  289. }
  290. if (moved)
  291. break;
  292. }
  293.  
  294. if (moved) {
  295. generateTile();
  296. glutPostRedisplay();
  297. if (checkWin()) {
  298. endTime = time(nullptr);
  299. std::cout << "You win! Your score: " << score << std::endl;
  300. double timeSpent = difftime(endTime, startTime);
  301. int minutes = static_cast<int>(timeSpent) / 60;
  302. int seconds = static_cast<int>(timeSpent) % 60;
  303. std::cout << "Time spent: " << minutes << " minutes " << seconds << " seconds" << std::endl;
  304. exit(0);
  305. }
  306. if (checkLose()) {
  307. endTime = time(nullptr);
  308. std::cout << "Game over! Your score: " << score << std::endl;
  309. double timeSpent = difftime(endTime, startTime);
  310. int minutes = static_cast<int>(timeSpent) / 60;
  311. int seconds = static_cast<int>(timeSpent) % 60;
  312. std::cout << "Time spent: " << minutes << " minutes " << seconds << " seconds" << std::endl;
  313. exit(0);
  314. }
  315. }
  316. }
  317.  
  318. void drawBoard() {
  319. for (int i = 0; i < SIZE; ++i) {
  320. for (int j = 0; j < SIZE; ++j) {
  321. int x = j * TILE_SIZE;
  322. int y = (SIZE - 1 - i) * TILE_SIZE;
  323. drawTile(x, y, board[i][j]);
  324. }
  325. }
  326. }
  327.  
  328. void timer(int value) {
  329. glutTimerFunc(1000, timer, 0);
  330. glutPostRedisplay();
  331. }
  332.  
  333. void display() {
  334. glClear(GL_COLOR_BUFFER_BIT);
  335. drawBoard();
  336. glutSwapBuffers();
  337. }
  338.  
  339. void handleMouseClick(int button, int state, int x, int y) {
  340. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  341. updateOriginalBoard(); // Сохраняем оригинальную доску перед перемещением клеток
  342.  
  343. int row = SIZE - 1 - (y / TILE_SIZE);
  344. int col = x / TILE_SIZE;
  345.  
  346. if ((row == 0 && col == 0) || // левый верхний угол
  347. (row == 0 && col == SIZE - 1) || // правый верхний угол
  348. (row == SIZE - 1 && col == 0) || // левый нижний угол
  349. (row == SIZE - 1 && col == SIZE - 1)) // правый нижний угол
  350. {
  351. // Если нажата угловая клетка внешнего слоя, ничего не делаем
  352. glutPostRedisplay();
  353. return;
  354. }
  355.  
  356. if (row == 0 || row == SIZE - 1 || col == 0 || col == SIZE - 1) {
  357. // Нажатие на остальные клетки внешнего слоя
  358. if (row == 0) {
  359. // Нажатие на верхние клетки внешнего слоя, двигаем клетки вверх
  360. moveDown();
  361. mergeDown();
  362. moveDown();
  363. }
  364. else if (row == SIZE - 1) {
  365. // Нажатие на нижние клетки внешнего слоя, двигаем клетки вниз
  366. moveUp();
  367. mergeUp();
  368. moveUp();
  369. }
  370. else if (col == 0) {
  371. // Нажатие на левые клетки внешнего слоя, двигаем клетки влево
  372. moveLeft();
  373. mergeLeft();
  374. moveLeft();
  375. }
  376. else {
  377. // Нажатие на правые клетки внешнего слоя, двигаем клетки вправо
  378. moveRight();
  379. mergeRight();
  380. moveRight();
  381. }
  382.  
  383. bool moved = false;
  384. for (int i = 0; i < SIZE; ++i) {
  385. for (int j = 0; j < SIZE; ++j) {
  386. if (board[i][j] != originalBoard[i][j]) {
  387. moved = true;
  388. break;
  389. }
  390. }
  391. if (moved)
  392. break;
  393. }
  394.  
  395. if (moved) {
  396. generateTile();
  397. glutPostRedisplay();
  398. if (checkWin()) {
  399. endTime = time(nullptr);
  400. std::cout << "You win! Your score: " << score << std::endl;
  401. double timeSpent = difftime(endTime, startTime);
  402. int minutes = static_cast<int>(timeSpent) / 60;
  403. int seconds = static_cast<int>(timeSpent) % 60;
  404. std::cout << "Time spent: " << minutes << " minutes " << seconds << " seconds" << std::endl;
  405. exit(0);
  406. }
  407. if (checkLose()) {
  408. endTime = time(nullptr);
  409. std::cout << "Game over! Your score: " << score << std::endl;
  410. double timeSpent = difftime(endTime, startTime);
  411. int minutes = static_cast<int>(timeSpent) / 60;
  412. int seconds = static_cast<int>(timeSpent) % 60;
  413. std::cout << "Time spent: " << minutes << " minutes " << seconds << " seconds" << std::endl;
  414. exit(0);
  415. }
  416. }
  417. }
  418. }
  419. }
  420.  
  421. int main(int argc, char** argv) {
  422. srand(time(nullptr));
  423. glutInit(&argc, argv);
  424. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  425. glutInitWindowSize(SIZE * TILE_SIZE, SIZE * TILE_SIZE);
  426. glutCreateWindow("2048 Game");
  427. glutDisplayFunc(display);
  428. glutKeyboardFunc(handleKeypress);
  429. glutSpecialFunc(handleSpecialKeypress);
  430. glutMouseFunc(handleMouseClick); // Добавленная строка для обработки щелчков мыши
  431. init();
  432. generateTile();
  433. generateTile();
  434. startTime = time(nullptr);
  435. glutTimerFunc(1000, timer, 0);
  436. glutMainLoop();
  437. return 0;
  438. }
  439.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement