Advertisement
Vince14

2048 on C++

Mar 19th, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.99 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <vector>
  7. #include <set>
  8. #include <map>
  9. #include <stack>
  10. #include <queue>
  11. #include <deque>
  12. #include <unordered_map>
  13. #include <iomanip>
  14. #include <regex>
  15. #include <cstdlib>
  16. #include <cstdio>
  17. #include <ctime>
  18. #include <numeric>
  19. using namespace std;
  20. #define pii pair<long long , long long>
  21. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
  22. const long long dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  23. const long long MAX = 1000005;
  24. const long long MOD = 1000000009;
  25. const int WIN_VALUE = 16;
  26.  
  27. int board[4][4];
  28.  
  29. void print_board(){
  30.     for(int i = 0; i < 4; i++){
  31.         for(int j = 0; j < 4; j++){
  32.             cout << board[i][j] << "   ";
  33.         }
  34.         cout << "\n";
  35.     }
  36. }
  37.  
  38. void print_board(int x, int y){
  39.     for(int i = 0; i < 4; i++){
  40.         for(int j = 0; j < 4; j++){
  41.             if(i == x and j == y){
  42.                 cout << board[i][j] << "*  ";
  43.             }
  44.             else cout << board[i][j] << "   ";
  45.         }
  46.         cout << "\n";
  47.     }
  48. }
  49.  
  50. void random_add(){
  51.     vector<int> empty_cells;
  52.     for(int i = 0; i < 16; i++){
  53.         if(board[i / 4 ][i % 4] == 0){
  54.             empty_cells.push_back(i);
  55.         }
  56.     }
  57.     if(empty_cells.size() == 0){
  58.         cout << "Game over! There are no more empty cells!\n";
  59.         return;
  60.     }
  61.     int r1 = empty_cells[rand() % (int)empty_cells.size()];
  62.     int r2 = rand() % 2 + 1;
  63.     board[r1 / 4][r1 % 4] = r2 * 2;
  64.     print_board(r1 / 4, r1 % 4);
  65.     empty_cells.clear();
  66. }
  67.  
  68. vector<int> move_D(){
  69.     int ret[4][4] = {};
  70.     for(int i = 0; i < 4; i++){
  71.         vector<int> v;
  72.         for(int j = 3; j >= 0; j--){
  73.             if(board[i][j] == 0){
  74.                 continue;
  75.             }
  76.             v.push_back(board[i][j]);
  77.         }
  78.         if(v.empty()){
  79.             continue;
  80.         }
  81.         else if(v.size() == 1){
  82.             ret[i][3] = v[0];
  83.         }
  84.         else if(v.size() == 2){
  85.             if(v[0] == v[1]){
  86.                 ret[i][3] = v[0] * 2;
  87.             }
  88.             else{
  89.                 ret[i][2] = v[1];
  90.                 ret[i][3] = v[0];
  91.             }
  92.         }
  93.         else if(v.size() == 3){
  94.             if(v[0] == v[1]){
  95.                 ret[i][2] = v[2];
  96.                 ret[i][3] = v[0] * 2;
  97.             }
  98.             else if(v[1] == v[2]){
  99.                 ret[i][2] = v[1] * 2;
  100.                 ret[i][3] = v[0];
  101.             }
  102.             else{
  103.                 for(int k = 0; k < 3; k++) ret[i][3 - k] = v[k];
  104.             }
  105.         }
  106.         else{
  107.             if(v[0] == v[1]){
  108.                 if(v[2] == v[3]){
  109.                     ret[i][2] = v[2] * 2;
  110.                     ret[i][3] = v[0] * 2;
  111.                 }
  112.                 else{
  113.                     ret[i][1] = v[3];
  114.                     ret[i][2] = v[2];
  115.                     ret[i][3] = v[0] * 2;
  116.                 }
  117.             }
  118.             else if(v[1] == v[2]){
  119.                 ret[i][1] = v[3];
  120.                 ret[i][2] = v[1] * 2;
  121.                 ret[i][3] = v[0];
  122.             }
  123.             else if(v[2] == v[3]){
  124.                 ret[i][1] = v[2] * 2;
  125.                 ret[i][2] = v[1];
  126.                 ret[i][3] = v[0];
  127.             }
  128.             else{
  129.                 for(int k = 0; k < 4; k++) ret[i][3 - k] = v[k];
  130.             }
  131.         }
  132.     }
  133.     vector<int> tmp;
  134.     for(int i = 0; i < 4; i++){
  135.         for(int j = 0; j < 4; j++){
  136.             tmp.push_back(ret[i][j]);
  137.         }
  138.     }
  139.     return tmp;
  140. }
  141.  
  142. vector<int> move_A(){
  143.     int ret[4][4] = {};
  144.     for(int i = 0; i < 4; i++){
  145.         vector<int> v;
  146.         for(int j = 0; j < 4; j++){
  147.             if(board[i][j] == 0){
  148.                 continue;
  149.             }
  150.             v.push_back(board[i][j]);
  151.         }
  152.         if(v.empty()){
  153.             continue;
  154.         }
  155.         else if(v.size() == 1){
  156.             ret[i][0] = v[0];
  157.         }
  158.         else if(v.size() == 2){
  159.             if(v[0] == v[1]){
  160.                 ret[i][0] = v[0] * 2;
  161.             }
  162.             else{
  163.                 ret[i][1] = v[1];
  164.                 ret[i][0] = v[0];
  165.             }
  166.         }
  167.         else if(v.size() == 3){
  168.             if(v[0] == v[1]){
  169.                 ret[i][1] = v[2];
  170.                 ret[i][0] = v[0] * 2;
  171.             }
  172.             else if(v[1] == v[2]){
  173.                 ret[i][1] = v[1] * 2;
  174.                 ret[i][0] = v[0];
  175.             }
  176.             else{
  177.                 for(int k = 0; k < 3; k++) ret[i][k] = v[k];
  178.             }
  179.         }
  180.         else{
  181.             if(v[0] == v[1]){
  182.                 if(v[2] == v[3]){
  183.                     ret[i][1] = v[2] * 2;
  184.                     ret[i][0] = v[0] * 2;
  185.                 }
  186.                 else{
  187.                     ret[i][2] = v[3];
  188.                     ret[i][1] = v[2];
  189.                     ret[i][0] = v[0] * 2;
  190.                 }
  191.             }
  192.             else if(v[1] == v[2]){
  193.                 ret[i][2] = v[3];
  194.                 ret[i][1] = v[1] * 2;
  195.                 ret[i][0] = v[0];
  196.             }
  197.             else if(v[2] == v[3]){
  198.                 ret[i][2] = v[2] * 2;
  199.                 ret[i][1] = v[1];
  200.                 ret[i][0] = v[0];
  201.             }
  202.             else{
  203.                 for(int k = 0; k < 4; k++) ret[i][k] = v[k];
  204.             }
  205.         }
  206.     }
  207.     vector<int> tmp;
  208.     for(int i = 0; i < 4; i++){
  209.         for(int j = 0; j < 4; j++){
  210.             tmp.push_back(ret[i][j]);
  211.         }
  212.     }
  213.     return tmp;
  214. }
  215. vector<int> move_W(){
  216.     int ret[4][4] = {};
  217.     for(int j = 0; j < 4; j++){
  218.         vector<int> v;
  219.         for(int i = 0; i < 4; i++){
  220.             if(board[i][j] == 0){
  221.                 continue;
  222.             }
  223.             v.push_back(board[i][j]);
  224.         }
  225.         if(v.empty()){
  226.             continue;
  227.         }
  228.         else if(v.size() == 1){
  229.             ret[0][j] = v[0];
  230.         }
  231.         else if(v.size() == 2){
  232.             if(v[0] == v[1]){
  233.                 ret[0][j] = v[0] * 2;
  234.             }
  235.             else{
  236.                 ret[1][j] = v[1];
  237.                 ret[0][j] = v[0];
  238.             }
  239.         }
  240.         else if(v.size() == 3){
  241.             if(v[0] == v[1]){
  242.                 ret[1][j] = v[2];
  243.                 ret[0][j] = v[0] * 2;
  244.             }
  245.             else if(v[1] == v[2]){
  246.                 ret[1][j] = v[1] * 2;
  247.                 ret[0][j] = v[0];
  248.             }
  249.             else{
  250.                 for(int k = 0; k < 3; k++) ret[k][j] = v[k];
  251.             }
  252.         }
  253.         else{
  254.             if(v[0] == v[1]){
  255.                 if(v[2] == v[3]){
  256.                     ret[1][j] = v[2] * 2;
  257.                     ret[0][j] = v[0] * 2;
  258.                 }
  259.                 else{
  260.                     ret[2][j] = v[3];
  261.                     ret[1][j] = v[2];
  262.                     ret[0][j] = v[0] * 2;
  263.                 }
  264.             }
  265.             else if(v[1] == v[2]){
  266.                 ret[2][j] = v[3];
  267.                 ret[1][j] = v[1] * 2;
  268.                 ret[0][j] = v[0];
  269.             }
  270.             else if(v[2] == v[3]){
  271.                 ret[2][j] = v[2] * 2;
  272.                 ret[1][j] = v[1];
  273.                 ret[0][j] = v[0];
  274.             }
  275.             else{
  276.                 for(int k = 0; k < 4; k++) ret[k][j] = v[k];
  277.             }
  278.         }
  279.     }
  280.     vector<int> tmp;
  281.     for(int i = 0; i < 4; i++){
  282.         for(int j = 0; j < 4; j++){
  283.             tmp.push_back(ret[i][j]);
  284.         }
  285.     }
  286.     return tmp;
  287. }
  288.  
  289. vector<int> move_S(){
  290.     int ret[4][4] = {};
  291.     for(int j = 0; j < 4; j++){
  292.         vector<int> v;
  293.         for(int i = 3; i >= 0; i--){
  294.             if(board[i][j] == 0){
  295.                 continue;
  296.             }
  297.             v.push_back(board[i][j]);
  298.         }
  299.         if(v.empty()){
  300.             continue;
  301.         }
  302.         else if(v.size() == 1){
  303.             ret[3][j] = v[0];
  304.         }
  305.         else if(v.size() == 2){
  306.             if(v[0] == v[1]){
  307.                 ret[3][j] = v[0] * 2;
  308.             }
  309.             else{
  310.                 ret[2][j] = v[1];
  311.                 ret[3][j] = v[0];
  312.             }
  313.         }
  314.         else if(v.size() == 3){
  315.             if(v[0] == v[1]){
  316.                 ret[2][j] = v[2];
  317.                 ret[3][j] = v[0] * 2;
  318.             }
  319.             else if(v[1] == v[2]){
  320.                 ret[2][j] = v[1] * 2;
  321.                 ret[3][j] = v[0];
  322.             }
  323.             else{
  324.                 for(int k = 0; k < 3; k++) ret[3 - k][j] = v[k];
  325.             }
  326.         }
  327.         else{
  328.             if(v[0] == v[1]){
  329.                 if(v[2] == v[3]){
  330.                     ret[2][j] = v[2] * 2;
  331.                     ret[3][j] = v[0] * 2;
  332.                 }
  333.                 else{
  334.                     ret[1][j] = v[3];
  335.                     ret[2][j] = v[2];
  336.                     ret[3][j] = v[0] * 2;
  337.                 }
  338.             }
  339.             else if(v[1] == v[2]){
  340.                 ret[1][j] = v[3];
  341.                 ret[2][j] = v[1] * 2;
  342.                 ret[3][j] = v[0];
  343.             }
  344.             else if(v[2] == v[3]){
  345.                 ret[1][j] = v[2] * 2;
  346.                 ret[2][j] = v[1];
  347.                 ret[3][j] = v[0];
  348.             }
  349.             else{
  350.                 for(int k = 0; k < 4; k++) ret[3 - k][j] = v[k];
  351.             }
  352.         }
  353.     }
  354.     vector<int> tmp;
  355.     for(int i = 0; i < 4; i++){
  356.         for(int j = 0; j < 4; j++){
  357.             tmp.push_back(ret[i][j]);
  358.         }
  359.     }
  360.     return tmp;
  361. }
  362.  
  363. bool check(vector<int> vec){
  364.     for(auto x : vec){
  365.         if(x == 0){
  366.             return true;
  367.         }
  368.     }
  369.     return false;
  370. }
  371.  
  372. bool check2(){
  373.     for(int i = 0; i < 4; i++){
  374.         for(int j = 0; j < 4; j++){
  375.             if(board[i][j] == 0){
  376.                 return true;
  377.             }
  378.         }
  379.     }
  380.     return false;
  381. }
  382.  
  383. bool check3(vector<int> vec){
  384.     vector<int> tmp;
  385.     for(int i = 0; i < 4; i++){
  386.         for(int j = 0; j < 4; j++){
  387.             tmp.push_back(board[i][j]);
  388.         }
  389.     }
  390.     for(int i = 0; i < 16; i++){
  391.         if(vec[i] != tmp[i]){
  392.             return true;
  393.         }
  394.     }
  395.     return false;
  396. }
  397.  
  398. void apply(vector<int> vec){
  399.     for(int i = 0; i < 16; i++){
  400.         board[i / 4][i % 4] = vec[i];
  401.     }
  402. }
  403.  
  404. int main() {
  405.     //freopen("hayfeast.in", "r", stdin);
  406.     //freopen("hayfeast.out", "w", stdout);
  407.     FAST;
  408.     cout << "Welcome to 2048!\nType W, A, S, or D to move the tiles.\nTiles with the same number merge into one when they touch.\nAdd them up to reach 2048!\nNewly added numbers have a \"*\" behind it.\nType \"EXIT\" to exit the game\n";
  409.     random_add();
  410.     while(true){
  411.         string c;
  412.         cin >> c;
  413.         if(c == "EXIT"){
  414.             cout << "You have successfully exited 2048!\n";
  415.             break;
  416.         }
  417.         map<string, bool> m;
  418.         bool ck = check2();
  419.         bool pos = false;
  420.         map<string, vector<int>> m2;
  421.         m2["D"] = move_D();
  422.         m2["A"] = move_A();
  423.         m2["W"] = move_W();
  424.         m2["S"] = move_S();
  425.         if(!ck){
  426.             m["D"] = check(m2["D"]);
  427.             m["A"] = check(m2["A"]);
  428.             m["W"] = check(m2["W"]);
  429.             m["S"] = check(m2["S"]);
  430.             pos = m["D"] || m["A"] || m["W"] || m["S"];
  431.         }
  432.         if(c != "D" and c!= "A" and c != "W" and c != "S"){
  433.             cout << "You have to type in W, A, S, or D in their capital forms!\n";
  434.         }
  435.         else{
  436.             if(!ck and pos and !m[c]){
  437.                 cout << "You will lose if you swipe this way!\nChoose a different direction to swipe!\n";
  438.             }
  439.             else if(!check3(m2[c])){
  440.                 cout << "This move doesn't do anything!\nChoose a different direction to swipe!\n";
  441.             }
  442.             else{
  443.                 apply(m2[c]);
  444.                 random_add();
  445.             }
  446.         }
  447.         for(int i = 0; i < 4; i++){
  448.             for(int j = 0; j < 4; j++){
  449.                 if(board[i][j] == WIN_VALUE){
  450.                     cout << "Congratulations!\nYou won!!!\n";
  451.                     return 0;
  452.                 }
  453.             }
  454.         }
  455.     }
  456. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement