Advertisement
podsolnyxxx

+больше не появляются. нет цвета 10

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