Advertisement
Vince14

2048 on C++ Updated

Apr 10th, 2023
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.42 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. int WIN_VALUE = 2048;
  26.  
  27. int board[4][4];
  28. map<int, string> blank_space;
  29. int length(int x){
  30.     string s = to_string(x);
  31.     return (int)s.size();
  32. }
  33.  
  34. void print_board(int x, int y){
  35.     for(int i = 0; i < 4; i++){
  36.         for(int j = 0; j < 4; j++){
  37.             if(i == x and j == y){
  38.                 cout << board[i][j] << "*";
  39.                 for(int k = 0; k < 4 - length(board[i][j]); k++){
  40.                     cout << " ";
  41.                 }
  42.             }
  43.             else{
  44.                 cout << board[i][j];
  45.                 for(int k = 0; k < 5 - length(board[i][j]); k++){
  46.                     cout << " ";
  47.                 }
  48.             }
  49.         }
  50.         cout << "\n";
  51.     }
  52. }
  53.  
  54. void random_add(){
  55.     vector<int> empty_cells;
  56.     for(int i = 0; i < 16; i++){
  57.         if(board[i / 4 ][i % 4] == 0){
  58.             empty_cells.push_back(i);
  59.         }
  60.     }
  61.     if(empty_cells.size() == 0){
  62.         cout << "Game over! There are no more empty cells!\n";
  63.         return;
  64.     }
  65.     int r1 = empty_cells[rand() % (int)empty_cells.size()];
  66.     int r2 = rand() % 2 + 1;
  67.     board[r1 / 4][r1 % 4] = r2 * 2;
  68.     print_board(r1 / 4, r1 % 4);
  69.     empty_cells.clear();
  70. }
  71.  
  72. vector<int> move_D(){
  73.     int ret[4][4] = {};
  74.     for(int i = 0; i < 4; i++){
  75.         vector<int> v;
  76.         for(int j = 3; j >= 0; j--){
  77.             if(board[i][j] == 0){
  78.                 continue;
  79.             }
  80.             v.push_back(board[i][j]);
  81.         }
  82.         if(v.empty()){
  83.             continue;
  84.         }
  85.         else if(v.size() == 1){
  86.             ret[i][3] = v[0];
  87.         }
  88.         else if(v.size() == 2){
  89.             if(v[0] == v[1]){
  90.                 ret[i][3] = v[0] * 2;
  91.             }
  92.             else{
  93.                 ret[i][2] = v[1];
  94.                 ret[i][3] = v[0];
  95.             }
  96.         }
  97.         else if(v.size() == 3){
  98.             if(v[0] == v[1]){
  99.                 ret[i][2] = v[2];
  100.                 ret[i][3] = v[0] * 2;
  101.             }
  102.             else if(v[1] == v[2]){
  103.                 ret[i][2] = v[1] * 2;
  104.                 ret[i][3] = v[0];
  105.             }
  106.             else{
  107.                 for(int k = 0; k < 3; k++) ret[i][3 - k] = v[k];
  108.             }
  109.         }
  110.         else{
  111.             if(v[0] == v[1]){
  112.                 if(v[2] == v[3]){
  113.                     ret[i][2] = v[2] * 2;
  114.                     ret[i][3] = v[0] * 2;
  115.                 }
  116.                 else{
  117.                     ret[i][1] = v[3];
  118.                     ret[i][2] = v[2];
  119.                     ret[i][3] = v[0] * 2;
  120.                 }
  121.             }
  122.             else if(v[1] == v[2]){
  123.                 ret[i][1] = v[3];
  124.                 ret[i][2] = v[1] * 2;
  125.                 ret[i][3] = v[0];
  126.             }
  127.             else if(v[2] == v[3]){
  128.                 ret[i][1] = v[2] * 2;
  129.                 ret[i][2] = v[1];
  130.                 ret[i][3] = v[0];
  131.             }
  132.             else{
  133.                 for(int k = 0; k < 4; k++) ret[i][3 - k] = v[k];
  134.             }
  135.         }
  136.     }
  137.     vector<int> tmp;
  138.     for(int i = 0; i < 4; i++){
  139.         for(int j = 0; j < 4; j++){
  140.             tmp.push_back(ret[i][j]);
  141.         }
  142.     }
  143.     return tmp;
  144. }
  145.  
  146. vector<int> move_A(){
  147.     int ret[4][4] = {};
  148.     for(int i = 0; i < 4; i++){
  149.         vector<int> v;
  150.         for(int j = 0; j < 4; j++){
  151.             if(board[i][j] == 0){
  152.                 continue;
  153.             }
  154.             v.push_back(board[i][j]);
  155.         }
  156.         if(v.empty()){
  157.             continue;
  158.         }
  159.         else if(v.size() == 1){
  160.             ret[i][0] = v[0];
  161.         }
  162.         else if(v.size() == 2){
  163.             if(v[0] == v[1]){
  164.                 ret[i][0] = v[0] * 2;
  165.             }
  166.             else{
  167.                 ret[i][1] = v[1];
  168.                 ret[i][0] = v[0];
  169.             }
  170.         }
  171.         else if(v.size() == 3){
  172.             if(v[0] == v[1]){
  173.                 ret[i][1] = v[2];
  174.                 ret[i][0] = v[0] * 2;
  175.             }
  176.             else if(v[1] == v[2]){
  177.                 ret[i][1] = v[1] * 2;
  178.                 ret[i][0] = v[0];
  179.             }
  180.             else{
  181.                 for(int k = 0; k < 3; k++) ret[i][k] = v[k];
  182.             }
  183.         }
  184.         else{
  185.             if(v[0] == v[1]){
  186.                 if(v[2] == v[3]){
  187.                     ret[i][1] = v[2] * 2;
  188.                     ret[i][0] = v[0] * 2;
  189.                 }
  190.                 else{
  191.                     ret[i][2] = v[3];
  192.                     ret[i][1] = v[2];
  193.                     ret[i][0] = v[0] * 2;
  194.                 }
  195.             }
  196.             else if(v[1] == v[2]){
  197.                 ret[i][2] = v[3];
  198.                 ret[i][1] = v[1] * 2;
  199.                 ret[i][0] = v[0];
  200.             }
  201.             else if(v[2] == v[3]){
  202.                 ret[i][2] = v[2] * 2;
  203.                 ret[i][1] = v[1];
  204.                 ret[i][0] = v[0];
  205.             }
  206.             else{
  207.                 for(int k = 0; k < 4; k++) ret[i][k] = v[k];
  208.             }
  209.         }
  210.     }
  211.     vector<int> tmp;
  212.     for(int i = 0; i < 4; i++){
  213.         for(int j = 0; j < 4; j++){
  214.             tmp.push_back(ret[i][j]);
  215.         }
  216.     }
  217.     return tmp;
  218. }
  219. vector<int> move_W(){
  220.     int ret[4][4] = {};
  221.     for(int j = 0; j < 4; j++){
  222.         vector<int> v;
  223.         for(int i = 0; i < 4; i++){
  224.             if(board[i][j] == 0){
  225.                 continue;
  226.             }
  227.             v.push_back(board[i][j]);
  228.         }
  229.         if(v.empty()){
  230.             continue;
  231.         }
  232.         else if(v.size() == 1){
  233.             ret[0][j] = v[0];
  234.         }
  235.         else if(v.size() == 2){
  236.             if(v[0] == v[1]){
  237.                 ret[0][j] = v[0] * 2;
  238.             }
  239.             else{
  240.                 ret[1][j] = v[1];
  241.                 ret[0][j] = v[0];
  242.             }
  243.         }
  244.         else if(v.size() == 3){
  245.             if(v[0] == v[1]){
  246.                 ret[1][j] = v[2];
  247.                 ret[0][j] = v[0] * 2;
  248.             }
  249.             else if(v[1] == v[2]){
  250.                 ret[1][j] = v[1] * 2;
  251.                 ret[0][j] = v[0];
  252.             }
  253.             else{
  254.                 for(int k = 0; k < 3; k++) ret[k][j] = v[k];
  255.             }
  256.         }
  257.         else{
  258.             if(v[0] == v[1]){
  259.                 if(v[2] == v[3]){
  260.                     ret[1][j] = v[2] * 2;
  261.                     ret[0][j] = v[0] * 2;
  262.                 }
  263.                 else{
  264.                     ret[2][j] = v[3];
  265.                     ret[1][j] = v[2];
  266.                     ret[0][j] = v[0] * 2;
  267.                 }
  268.             }
  269.             else if(v[1] == v[2]){
  270.                 ret[2][j] = v[3];
  271.                 ret[1][j] = v[1] * 2;
  272.                 ret[0][j] = v[0];
  273.             }
  274.             else if(v[2] == v[3]){
  275.                 ret[2][j] = v[2] * 2;
  276.                 ret[1][j] = v[1];
  277.                 ret[0][j] = v[0];
  278.             }
  279.             else{
  280.                 for(int k = 0; k < 4; k++) ret[k][j] = v[k];
  281.             }
  282.         }
  283.     }
  284.     vector<int> tmp;
  285.     for(int i = 0; i < 4; i++){
  286.         for(int j = 0; j < 4; j++){
  287.             tmp.push_back(ret[i][j]);
  288.         }
  289.     }
  290.     return tmp;
  291. }
  292.  
  293. vector<int> move_S(){
  294.     int ret[4][4] = {};
  295.     for(int j = 0; j < 4; j++){
  296.         vector<int> v;
  297.         for(int i = 3; i >= 0; i--){
  298.             if(board[i][j] == 0){
  299.                 continue;
  300.             }
  301.             v.push_back(board[i][j]);
  302.         }
  303.         if(v.empty()){
  304.             continue;
  305.         }
  306.         else if(v.size() == 1){
  307.             ret[3][j] = v[0];
  308.         }
  309.         else if(v.size() == 2){
  310.             if(v[0] == v[1]){
  311.                 ret[3][j] = v[0] * 2;
  312.             }
  313.             else{
  314.                 ret[2][j] = v[1];
  315.                 ret[3][j] = v[0];
  316.             }
  317.         }
  318.         else if(v.size() == 3){
  319.             if(v[0] == v[1]){
  320.                 ret[2][j] = v[2];
  321.                 ret[3][j] = v[0] * 2;
  322.             }
  323.             else if(v[1] == v[2]){
  324.                 ret[2][j] = v[1] * 2;
  325.                 ret[3][j] = v[0];
  326.             }
  327.             else{
  328.                 for(int k = 0; k < 3; k++) ret[3 - k][j] = v[k];
  329.             }
  330.         }
  331.         else{
  332.             if(v[0] == v[1]){
  333.                 if(v[2] == v[3]){
  334.                     ret[2][j] = v[2] * 2;
  335.                     ret[3][j] = v[0] * 2;
  336.                 }
  337.                 else{
  338.                     ret[1][j] = v[3];
  339.                     ret[2][j] = v[2];
  340.                     ret[3][j] = v[0] * 2;
  341.                 }
  342.             }
  343.             else if(v[1] == v[2]){
  344.                 ret[1][j] = v[3];
  345.                 ret[2][j] = v[1] * 2;
  346.                 ret[3][j] = v[0];
  347.             }
  348.             else if(v[2] == v[3]){
  349.                 ret[1][j] = v[2] * 2;
  350.                 ret[2][j] = v[1];
  351.                 ret[3][j] = v[0];
  352.             }
  353.             else{
  354.                 for(int k = 0; k < 4; k++) ret[3 - k][j] = v[k];
  355.             }
  356.         }
  357.     }
  358.     vector<int> tmp;
  359.     for(int i = 0; i < 4; i++){
  360.         for(int j = 0; j < 4; j++){
  361.             tmp.push_back(ret[i][j]);
  362.         }
  363.     }
  364.     return tmp;
  365. }
  366.  
  367. bool check(vector<int> vec){
  368.     for(auto x : vec){
  369.         if(x == 0){
  370.             return true;
  371.         }
  372.     }
  373.     return false;
  374. }
  375.  
  376. bool check2(){
  377.     for(int i = 0; i < 4; i++){
  378.         for(int j = 0; j < 4; j++){
  379.             if(board[i][j] == 0){
  380.                 return true;
  381.             }
  382.         }
  383.     }
  384.     return false;
  385. }
  386.  
  387. bool check3(vector<int> vec){
  388.     vector<int> tmp;
  389.     for(int i = 0; i < 4; i++){
  390.         for(int j = 0; j < 4; j++){
  391.             tmp.push_back(board[i][j]);
  392.         }
  393.     }
  394.     for(int i = 0; i < 16; i++){
  395.         if(vec[i] != tmp[i]){
  396.             return true;
  397.         }
  398.     }
  399.     return false;
  400. }
  401.  
  402. void apply(vector<int> vec){
  403.     for(int i = 0; i < 16; i++){
  404.         board[i / 4][i % 4] = vec[i];
  405.     }
  406. }
  407.  
  408. bool check_power(int x){
  409.     set<int> s;
  410.     int val = 8;
  411.     for(int i = 0; i < 11; i++){
  412.         s.insert(val);
  413.         val *= 2;
  414.     }
  415.     if(s.find(x) != s.end()){
  416.         return true;
  417.     }
  418.     else{
  419.         return false;
  420.     }
  421. }
  422.  
  423. int main() {
  424.     //freopen("hayfeast.in", "r", stdin);
  425.     //freopen("hayfeast.out", "w", stdout);
  426.     FAST;
  427.     cout << "Welcome to 2048!\n\n";
  428.    
  429.     cout << "Before you begin, enter a \"win value\" that is a positive power of 2 (between 8 and 8192) to reach!\nIf the board clogs up before you reach this value, you will lose the game, but if you successfully create this number, you win!\n\n";
  430.     cout << "Enter your win value: ";
  431.     while(true){
  432.         int x;
  433.         cin >> x;
  434.         cout << "\n";
  435.         if(!check_power(x)){
  436.             cout << "This number is not a positive power of 2 between 8 and 8192!\n\nRe-enter a new win value: ";
  437.         }
  438.         else{
  439.             WIN_VALUE = x;
  440.             cout << "Win value successfully set to " << x << "!\n\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!\nA new number, randomly decided between 2 and 4, will be added to the board after each move.\nNewly added numbers have a \"*\" behind it.\nIf the board does not have an empty tile for a new number to be added, the board is considered to be clogged, and you lose the game!\n\nType \"EXIT\" to exit the game.\n\n";
  441.             break;
  442.         }
  443.     }
  444.     random_add();
  445.     while(true){
  446.         string c;
  447.         map<string, bool> m;
  448.         bool ck = check2();
  449.         bool pos = false;
  450.         map<string, vector<int>> m2;
  451.         m2["D"] = move_D();
  452.         m2["A"] = move_A();
  453.         m2["W"] = move_W();
  454.         m2["S"] = move_S();
  455.         if(!ck){
  456.             m["D"] = check(m2["D"]);
  457.             m["A"] = check(m2["A"]);
  458.             m["W"] = check(m2["W"]);
  459.             m["S"] = check(m2["S"]);
  460.             pos = m["D"] || m["A"] || m["W"] || m["S"];
  461.             if(!pos){
  462.                 cout << "You lost!!!\n";
  463.                 return 0;
  464.             }
  465.         }
  466.         cin >> c;
  467.         if(c == "EXIT"){
  468.             cout << "You have successfully exited 2048!\n";
  469.             break;
  470.         }
  471.  
  472.         if(c != "D" and c!= "A" and c != "W" and c != "S"){
  473.             cout << "You have to type in W, A, S, or D in their capital forms!\n";
  474.         }
  475.         else{
  476.             if(!ck and pos and !m[c]){
  477.                 cout << "You will lose if you swipe this way!\nChoose a different direction to swipe!\n";
  478.             }
  479.             else if(!check3(m2[c])){
  480.                 cout << "This move doesn't do anything!\nChoose a different direction to swipe!\n";
  481.             }
  482.             else{
  483.                 apply(m2[c]);
  484.                 random_add();
  485.             }
  486.         }
  487.         for(int i = 0; i < 4; i++){
  488.             for(int j = 0; j < 4; j++){
  489.                 if(board[i][j] == WIN_VALUE){
  490.                     cout << "Congratulations!\nYou won!!!\n";
  491.                     return 0;
  492.                 }
  493.             }
  494.         }
  495.     }
  496. }
  497.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement