SHARE
    TWEET
    Vince14

    2048 on C++ Updated

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

    AltStyle によって変換されたページ (->オリジナル) /