Advertisement
gguuppyy

2048

Sep 20th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include "cppgraphics.hpp"
  2.  
  3. #include <array>
  4.  
  5. using Grid = std::array<std::array<int, 4>, 4>;
  6.  
  7. Grid new_grid()
  8. {
  9.     Grid grid;
  10.     for (int x=0; x<4; ++x)
  11.         for (int y=0; y<4; ++y)
  12.             grid[x][y] = 0;
  13.     return grid;
  14. }
  15.  
  16.  
  17. void new_number(Grid& grid)
  18. {
  19.     while (true) {
  20.         int x = cg::random_int(3);
  21.         int y = cg::random_int(3);
  22.         if (grid[x][y] == 0) {
  23.             grid[x][y] = 2;
  24.             return;
  25.         }
  26.     }
  27. }
  28.  
  29. bool is_full(const Grid& grid)
  30. {
  31.     for (int x=0; x<4; ++x)
  32.         for (int y=0; y<4; ++y)
  33.             if (grid[x][y] == 0)
  34.                 return false;
  35.     return true;
  36. }
  37.  
  38.  
  39. void draw(const Grid& grid)
  40. {
  41.     cg::set_color(cg::Black);
  42.     cg::set_fill_color(cg::Orange);
  43.     cg::set_background_color(cg::Brown);
  44.  
  45.     cg::clear();
  46.     for (int x=0; x<4; ++x) {
  47.         for (int y=0; y<4; ++y) {
  48.             cg::rectangle(100 + x*150, 100 + y*150, 150, 150);
  49.             if (grid[x][y] != 0)
  50.                 cg::text_centered(std::to_string(grid[x][y]), 175+x*150, 175+y*150, 50);
  51.         }
  52.     }
  53. }
  54.  
  55.  
  56. void squash_column(std::array<int, 4>& col)
  57. {
  58.     for (int dest=3; dest>=0; --dest) {
  59.         int src = dest-1;
  60.         while (src >= 0 && col[src] == 0)
  61.             --src;
  62.         if (src < 0)
  63.             break;
  64.         if (col[dest] == col[src]) {
  65.             col[dest] = col[dest] * 2;
  66.             col[src] = 0;
  67.         }
  68.         if (col[dest] == 0) {
  69.             std::swap(col[dest], col[src]);
  70.             ++dest;
  71.         }
  72.     }
  73. }
  74.  
  75.  
  76. void flip_vertically(Grid& grid)
  77. {
  78.     for (int x=0; x<4; ++x) {
  79.         std::swap(grid[x][0], grid[x][3]);
  80.         std::swap(grid[x][1], grid[x][2]);
  81.     }
  82. }
  83.  
  84.  
  85. void flip_diagonally(Grid& grid)
  86. {
  87.     std::swap(grid[1][0], grid[0][1]);
  88.     std::swap(grid[2][0], grid[0][2]);
  89.     std::swap(grid[3][0], grid[0][3]);
  90.     std::swap(grid[2][1], grid[1][2]);
  91.     std::swap(grid[3][1], grid[1][3]);
  92.     std::swap(grid[3][2], grid[2][3]);
  93. }
  94.  
  95.  
  96. void squash(Grid& grid, int key)
  97. {
  98.     if (key == cg::KeyLeft || key == cg::KeyRight)
  99.         flip_diagonally(grid);
  100.     if (key == cg::KeyUp || key == cg::KeyLeft)
  101.         flip_vertically(grid);
  102.  
  103.     for (int x=0; x<4; ++x)
  104.         squash_column(grid[x]);
  105.  
  106.     if (key == cg::KeyUp || key == cg::KeyLeft)
  107.         flip_vertically(grid);
  108.     if (key == cg::KeyLeft || key == cg::KeyRight)
  109.         flip_diagonally(grid);
  110. }
  111.  
  112.  
  113. int main()
  114. {
  115.     cg::create_window("2048", 800,800);
  116.  
  117.     Grid grid = new_grid();
  118.     new_number(grid);
  119.     while (cg::is_window_open()) {
  120.         new_number(grid);
  121.         draw(grid);
  122.         int key = cg::wait_until_keypressed();
  123.         squash(grid, key);
  124.         if (is_full(grid))
  125.             break;
  126.     }
  127.  
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement