It took me two hours to write in the stupidest way ... The most humble version ...
I'm not sure if this logic is right ...
#include <iostream> #include <cstdio> #include <cstdlib> #include <ctime> #include <conio.h >using namespace Std;int map[5][5];int score;int Move (int& A, int& B, int& C, int& D)//move nonzero di git to front{int vis_num = 0; The number of visited digit, most are 4 int zero_num = 0; The number of zero digit while (1) {if (A! = 0) {if (++vis_num >= 4) return zero_num; Break } ++zero_num; A = b; b = C; c = D; d = 0; if (++vis_num >= 4) return zero_num; } while (1) {if (b! = 0) {if (++vis_num >= 4) return zero_num; Break } ++zero_num; b = C; c = D; d = 0; if (++vis_num >= 4) return zero_num; } while (1) {if (c! = 0) {++vis_num; if (Vis_num >= 4) return zero_num; Break } ++zero_num; c = D; d = 0; if (++vis_num >= 4) return zero_num; } if (d = = 0) ++zero_num; return zero_num;} void Change (int& A, int& B, int& C, int& d) {int zero_num = Move (A, B, C, D); if (Zero_num = = 4 | | zero_num = = 3) return; if (Zero_num = = 2) {if (a = = b) {score + = A * 10; a <<= 1; b = 0; } return; } if (zero_num = = 1) {if (a = = b) {score + = A * 10; a <<= 1; b = C; c = 0; } else if (c = = b) {score + = c * 10; b <<= 1; c = 0; } return; if (a = = b) {if (c = = d) {score + = c * 10; Score + = A * 10; a <<= 1; b = C << 1; c = d = 0; } else {score + = A * 10; a <<= 1; b = C; c = D; d = 0; }} else if (b = = C) {score + = b * 10; b <<= 1; c = D; d = 0; } else if (c = = d) {score + = c * 10; C <<= 1; d = 0; }}int Rand_2_or_4 () {if (rand () & 1) return 2; return 4;} void Rand_pos () {int x, y; do {x = rand ()% 4; y = rand ()% 4; } while (Map[x][y]); Map[x][y] = Rand_2_or_4 ();} void print () {int i, J; for (i = 0; i < 4; ++i) {for (j = 0; j < 4; ++j) {if (map[i][j] = = 0) printf ("-"); else printf ("%4d", Map[i][j]); } printf ("\ n"); } printf ("score:%d\n", score);} BOOL Is_full () {int i, J; for (i = 0; i < 4; ++i) for (j = 0; j < 4; ++j) if (map[i][j] = = 0) return false; return true;} BOOL Is_over_row (int A, int b, int c, int d) {if (A! = b && B! = c && c! = d) return true; return false;} BOOL Is_over () {if (!is_full ()) return false; int i; for (i = 0; i < 4; ++i) {if (!is_over_row (map[i][0], map[i][1], map[i][2], map[i][3])) return false; } for (i = 0; i < 4; ++i) {if (!is_over_row (Map[0][i], map[1][i], map[2][i], Map[3][i])) return F Alse; } return true; int main () {//int a,b,c,d;//while (scanf ("%d%d%d%d", &a, &b, &c, &d)! = EOF) {//Change (A,B,C, d);//printf ("%d%d%d%d\n", A, B, C, D);/} srand (Unsigned (Time (NULL))); Rand_pos (); Rand_pos (); Print (); while (!is_over ()) {char dir = getch (); int i; if (dir = = ' W ') {for (i = 0; i < 4; ++i) {change (Map[0][i], map[1][i], map[2][i], Map[3][i] ); }} else if (dir = = ' s ') {for (i = 0; i < 4; ++i) change (Map[3][i], map[2][i], map[1] [i], map[0][i]); } else if (dir = = ' a ') {for (i = 0; i < 4; ++i) change (map[i][0], map[i][1], map[i][2], map[ I][3]); } else if (dir = = ' d ') {for (i = 0; i < 4; ++i) change (map[i][3], map[i][2], map[i][1], map[i][0]); } else continue; if (!is_full ()) Rand_pos (); System ("CLS"); Print (); } printf ("lose!"); return 0;}
C Language Character Edition easy 2048