只因房没网,无聊写的。
Upd 2023.9.14: 游戏有 bug,在修复中。
#include <bits/stdc++.h>
#include <conio.h>
#define int long long
using namespace std;
inline int read(){
int now=0,nev=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')nev=-1;c=getchar();}
while(c>='0'&&c<='9'){now=(now<<1)+(now<<3)+(c&15);c=getchar(); }
return now*nev;
}
int random(int x){ return rand() * rand() % x + 1; }
const int N = 55;
bool vis[N][N];
int a[N][N], n = 4, sc = 0, autoplay = 0;
vector<int> p;
bool pd(){
if (p.size() == 0) return 1;
int ju = 1;
for (int i = 0; i < p.size() - 1; i ++)
if (p[i] == p[i + 1]) ju = 0;
return ju;
}
void work(){
while (!pd()){
stack<int> s;
for (int i = 0; i < p.size(); i ++){
while (!s.empty() && p[i] == s.top()){
p[i] *= 2;
s.pop();
sc += p[i];
}
s.push(p[i]);
}
p.clear();
while (!s.empty()){
p.push_back(s.top());
s.pop();
}
reverse(p.begin(), p.end());
}
}
void Left(){
for (int i = 1; i <= n; i ++){
p.clear();
for (int j = 1; j <= n; j ++)
if (a[i][j]) p.push_back(a[i][j]);
work();
for (int j = 1; j <= n; j ++)
a[i][j] = 0;
for (int j = 0; j < p.size(); j ++)
a[i][j + 1] = p[j];
}
}
void Down(){
for (int i = 1; i <= n; i ++){
p.clear();
for (int j = n; j >= 1; j --)
if (a[j][i]) p.push_back(a[j][i]);
work();
for (int j = 1; j <= n; j ++)
a[j][i] = 0;
for (int j = 0; j < p.size(); j ++)
a[n - j][i] = p[j];
}
}
void Up(){
for (int i = 1; i <= n; i ++){
p.clear();
for (int j = 1; j <= n; j ++)
if (a[j][i]) p.push_back(a[j][i]);
work();
for (int j = 1; j <= n; j ++)
a[j][i] = 0;
for (int j = 0; j < p.size(); j ++)
a[j + 1][i] = p[j];
}
}
void Right(){
for (int i = 1; i <= n; i ++){
p.clear();
for (int j = n; j >= 1; j --)
if (a[i][j]) p.push_back(a[i][j]);
work();
for (int j = 1; j <= n; j ++)
a[i][j] = 0;
for (int j = 0; j < p.size(); j ++)
a[i][n - j] = p[j];
}
}
void print(){
for (int i = 1; i <= n; i ++){
for (int j = 1; j <= n; j ++){
cout << setw(6);
if (a[i][j] == 0) cout << " ";
else{
cout << a[i][j];
}
}
puts("\n");
}
cout << " Total: " << sc << "\n";
}
bool fail(){
bool p = 1;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
if (a[i][j] == 0) p = 0;
for (int i = 1; i <= n; i ++)
for (int j = 1; j < n; j ++)
if (a[i][j] == a[i][j + 1]) p = 0;
for (int i = 1; i <= n; i ++)
for (int j = 1; j < n; j ++)
if (a[j][i] == a[j + 1][i]) p = 0;
return p;
}
void start(){
cout << " \n";
cout << " \n";
cout << " 2048 GAME \n";
cout << " \n";
cout << " \n";
cout << " \n";
cout << "Touch to Start......";
}
void loading(){
int st = 0;
while (st < 100){
system("cls");
int step = random(5);
st += step;
if (st > 100) st = 100;
for (int i = 1; i <= 100; i += 3)
if (i <= st) putchar('#');
else putchar(32);
printf("\n %d%%", st);
}
_sleep(500);
}
signed main()
{
system("color f1");
start();
getch();
system("cls");
loading();
srand(time(0));
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++)
a[i][j] = 0;
for (int i = 1; i <= n; i ++){
int t = random(n * n);
int tx = (t - 1) / n + 1;
int ty = t - (tx - 1) * n;
a[tx][ty] = 2;
if (vis[tx][ty]) i --;
vis[tx][ty] = 1;
}
while (!fail()){
system("cls");
print();
char op;
if (!autoplay) op = getch();
else{
int kp = random(/*10*/4);
if (kp == 4) op = 'w';
else if (kp == 1/* || kp == 5*/) op = 's';
else if (kp == 2) op = 'a';
else op = 'd';
}
if (op >= 'A' && op <= 'Z') op += 32;
if (op == 'w') Up();
if (op == 's') Down();
if (op == 'a') Left();
if (op == 'd') Right();
int ct = 0;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++){
if (a[i][j]) vis[i][j] = 1, ct ++;
else vis[i][j] = 0;
}
if (ct == n * n)
break;
int t = random(n);
int tx = (t - 1) / n + 1;
int ty = t - (tx - 1) * n;
while (vis[tx][ty]){
t = random(n * n);
tx = (t - 1) / n + 1;
ty = t - (tx - 1) * n;
}
int rfg = random(4);
if (rfg <= 3) a[tx][ty] = 2;
else a[tx][ty] = 4;
}
system("cls");
cout << "Final score: " << sc << "\n";
_sleep(1000);
return 0;
}