Advertisement
podsolnyxxx

+цвет. нет углового цвета 12

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