Advertisement
podsolnyxxx

+ остановка после победы/луза. нет кликов мыши 5

Apr 7th, 2024 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 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 = 4;
  8. const int TILE_SIZE = 100;
  9. int board[SIZE][SIZE] = { 0 };
  10. int score = 0; // Переменная для хранения очков
  11.  
  12. void init() {
  13. glClearColor(0.9, 0.9, 0.9, 1.0);
  14. glMatrixMode(GL_PROJECTION);
  15. glLoadIdentity();
  16. gluOrtho2D(0, SIZE * TILE_SIZE, 0, SIZE * TILE_SIZE);
  17.  
  18. glLineWidth(2.0);
  19. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  20. }
  21.  
  22. void drawTile(int x, int y, int value) {
  23. glPushMatrix();
  24. glTranslatef(x, y, 0);
  25.  
  26. switch (value) {
  27. case 2: glColor3f(0.9f, 0.9f, 0.9f); break;
  28. case 4: glColor3f(1.0f, 1.0f, 0.5f); break;
  29. case 8: glColor3f(1.0f, 0.5f, 0.0f); break;
  30. case 16: glColor3f(1.0f, 0.0f, 0.0f); break;
  31. case 32: glColor3f(0.8f, 0.6f, 1.0f); break;
  32. case 64: glColor3f(0.0f, 0.0f, 1.0f); break;
  33. case 128: glColor3f(0.0f, 1.0f, 1.0f); break;
  34. case 256: glColor3f(0.6f, 1.0f, 0.6f); break;
  35. case 512: glColor3f(0.0f, 1.0f, 0.0f); break;
  36. case 1024: glColor3f(0.0f, 0.5f, 0.0f); break;
  37. case 2048: glColor3f(1.0f, 0.0f, 1.0f); break;
  38. default: glColor3f(1.0f, 1.0f, 1.0f);
  39. }
  40.  
  41. glBegin(GL_QUADS);
  42. glVertex2i(0, 0);
  43. glVertex2i(TILE_SIZE, 0);
  44. glVertex2i(TILE_SIZE, TILE_SIZE);
  45. glVertex2i(0, TILE_SIZE);
  46. glEnd();
  47.  
  48. if (value != 0) {
  49. glColor3f(0.0f, 0.0f, 0.0f);
  50. std::string text = std::to_string(value);
  51. glRasterPos2i(TILE_SIZE / 2 - 8 * text.length(), TILE_SIZE / 2 + 8);
  52. for (char c : text) {
  53. glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
  54. }
  55. }
  56.  
  57. glColor3f(0.0f, 0.0f, 0.0f);
  58. glBegin(GL_LINE_LOOP);
  59. glVertex2i(0, 0);
  60. glVertex2i(TILE_SIZE, 0);
  61. glVertex2i(TILE_SIZE, TILE_SIZE);
  62. glVertex2i(0, TILE_SIZE);
  63. glEnd();
  64.  
  65. glPopMatrix();
  66. }
  67.  
  68. void generateTile() {
  69. std::vector<std::pair<int, int>> emptyCells;
  70. for (int i = 0; i < SIZE; ++i) {
  71. for (int j = 0; j < SIZE; ++j) {
  72. if (board[i][j] == 0) {
  73. emptyCells.push_back(std::make_pair(i, j));
  74. }
  75. }
  76. }
  77. if (emptyCells.empty()) {
  78. return;
  79. }
  80. int position = rand() % emptyCells.size();
  81. int x = emptyCells[position].first;
  82. int y = emptyCells[position].second;
  83. board[x][y] = 2;
  84. }
  85.  
  86. bool checkWin() {
  87. for (int i = 0; i < SIZE; ++i) {
  88. for (int j = 0; j < SIZE; ++j) {
  89. if (board[i][j] == 2048) {
  90. return true;
  91. }
  92. }
  93. }
  94. return false;
  95. }
  96.  
  97. bool checkLose() {
  98. for (int i = 0; i < SIZE; ++i) {
  99. for (int j = 0; j < SIZE; ++j) {
  100. if (board[i][j] == 0) {
  101. return false;
  102. }
  103. if (j > 0 && (board[i][j] == board[i][j - 1] || board[i][j - 1] == 0)) {
  104. return false;
  105. }
  106. if (j < SIZE - 1 && (board[i][j] == board[i][j + 1] || board[i][j + 1] == 0)) {
  107. return false;
  108. }
  109. if (i > 0 && (board[i][j] == board[i - 1][j] || board[i - 1][j] == 0)) {
  110. return false;
  111. }
  112. if (i < SIZE - 1 && (board[i][j] == board[i + 1][j] || board[i + 1][j] == 0)) {
  113. return false;
  114. }
  115. }
  116. }
  117. return true;
  118. }
  119.  
  120. void moveLeft() {
  121. for (int i = 0; i < SIZE; ++i) {
  122. for (int j = 0; j < SIZE - 1; ++j) {
  123. if (board[i][j] == 0) {
  124. for (int k = j + 1; k < SIZE; ++k) {
  125. if (board[i][k] != 0) {
  126. board[i][j] = board[i][k];
  127. board[i][k] = 0;
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135.  
  136. void mergeLeft() {
  137. for (int i = 0; i < SIZE; ++i) {
  138. for (int j = 0; j < SIZE - 1; ++j) {
  139. if (board[i][j] == board[i][j + 1] && board[i][j] != 0) {
  140. board[i][j] *= 2;
  141. board[i][j + 1] = 0;
  142. score += board[i][j];
  143. }
  144. }
  145. }
  146. }
  147.  
  148. void moveRight() {
  149. for (int i = 0; i < SIZE; ++i) {
  150. for (int j = SIZE - 1; j > 0; --j) {
  151. if (board[i][j] == 0) {
  152. for (int k = j - 1; k >= 0; --k) {
  153. if (board[i][k] != 0) {
  154. board[i][j] = board[i][k];
  155. board[i][k] = 0;
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163.  
  164. void mergeRight() {
  165. for (int i = 0; i < SIZE; ++i) {
  166. for (int j = SIZE - 1; j > 0; --j) {
  167. if (board[i][j] == board[i][j - 1] && board[i][j] != 0) {
  168. board[i][j] *= 2;
  169. board[i][j - 1] = 0;
  170. score += board[i][j];
  171. }
  172. }
  173. }
  174. }
  175.  
  176. void moveUp() {
  177. for (int j = 0; j < SIZE; ++j) {
  178. for (int i = 0; i < SIZE - 1; ++i) {
  179. if (board[i][j] == 0) {
  180. for (int k = i + 1; k < SIZE; ++k) {
  181. if (board[k][j] != 0) {
  182. board[i][j] = board[k][j];
  183. board[k][j] = 0;
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191.  
  192. void mergeUp() {
  193. for (int j = 0; j < SIZE; ++j) {
  194. for (int i = 0; i < SIZE - 1; ++i) {
  195. if (board[i][j] == board[i + 1][j] && board[i][j] != 0) {
  196. board[i][j] *= 2;
  197. board[i + 1][j] = 0;
  198. score += board[i][j];
  199. }
  200. }
  201. }
  202. }
  203.  
  204. void moveDown() {
  205. for (int j = 0; j < SIZE; ++j) {
  206. for (int i = SIZE - 1; i > 0; --i) {
  207. if (board[i][j] == 0) {
  208. for (int k = i - 1; k >= 0; --k) {
  209. if (board[k][j] != 0) {
  210. board[i][j] = board[k][j];
  211. board[k][j] = 0;
  212. break;
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219.  
  220. void mergeDown() {
  221. for (int j = 0; j < SIZE; ++j) {
  222. for (int i = SIZE - 1; i > 0; --i) {
  223. if (board[i][j] == board[i - 1][j] && board[i][j] != 0) {
  224. board[i][j] *= 2;
  225. board[i - 1][j] = 0;
  226. score += board[i][j];
  227. }
  228. }
  229. }
  230. }
  231.  
  232. void handleKeypress(unsigned char key, int x, int y) {
  233. if (key == 27) {
  234. exit(0);
  235. }
  236. }
  237.  
  238. void handleSpecialKeypress(int key, int x, int y) {
  239. switch (key) {
  240. case GLUT_KEY_LEFT:
  241. moveLeft();
  242. mergeLeft();
  243. moveLeft();
  244. break;
  245. case GLUT_KEY_RIGHT:
  246. moveRight();
  247. mergeRight();
  248. moveRight();
  249. break;
  250. case GLUT_KEY_UP:
  251. moveUp();
  252. mergeUp();
  253. moveUp();
  254. break;
  255. case GLUT_KEY_DOWN:
  256. moveDown();
  257. mergeDown();
  258. moveDown();
  259. break;
  260. }
  261. generateTile();
  262. glutPostRedisplay();
  263. if (checkWin()) {
  264. std::cout << "You win! Your score: " << score << std::endl;
  265. exit(0);
  266. }
  267. if (checkLose()) {
  268. std::cout << "Game over! Your score: " << score << std::endl;
  269. exit(0);
  270. }
  271. }
  272.  
  273. void drawBoard() {
  274. for (int i = 0; i < SIZE; ++i) {
  275. for (int j = 0; j < SIZE; ++j) {
  276. int x = j * TILE_SIZE;
  277. int y = (SIZE - 1 - i) * TILE_SIZE;
  278. drawTile(x, y, board[i][j]);
  279. }
  280. }
  281. }
  282.  
  283. void timer(int value) {
  284. glutTimerFunc(1000, timer, 0);
  285. glutPostRedisplay();
  286. }
  287.  
  288. void display() {
  289. glClear(GL_COLOR_BUFFER_BIT);
  290. drawBoard();
  291. glutSwapBuffers();
  292. }
  293.  
  294. int main(int argc, char** argv) {
  295. srand(time(nullptr));
  296. glutInit(&argc, argv);
  297. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  298. glutInitWindowSize(SIZE * TILE_SIZE, SIZE * TILE_SIZE);
  299. glutCreateWindow("2048 Game");
  300. glutDisplayFunc(display);
  301. glutKeyboardFunc(handleKeypress);
  302. glutSpecialFunc(handleSpecialKeypress);
  303. init();
  304. generateTile();
  305. generateTile();
  306. glutTimerFunc(1000, timer, 0);
  307. glutMainLoop();
  308. return 0;
  309. }
  310.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement