2048 v1.2.0: 修改边框大小以及数字颜色
代码见下,或前往团队下载
如果报错,打开工具

点击编译选项

添加-std=c++11

打钩

确定

编译运行即可
#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
#define CLS system("CLS")
#define scta SetConsoleTextAttribute
#define gsh GetStdHandle
#define soh STD_OUTPUT_HANDLE
#define fi FOREGROUND_INTENSITY
#define red FOREGROUND_RED
#define blue FOREGROUND_BLUE
#define green FOREGROUND_GREEN
#define common_col scta(gsh(soh), fi | red | blue | green);
using namespace std;
int mp[10][10];
char break_up[70] = "---------------------------------",
empty_line[70] = "| | | | |";
default_random_engine e;
uniform_int_distribution<int> u1(1,2), u2(0,15);
void set_rand()
{
e.seed(time(0));
}
void num_give()
{
int p = u2(e);
int x = p % 4, y = p / 4;
while (mp[x][y])
{
p++;
p %= 16;
x = p % 4;
y = p / 4;
}
mp[x][y] = pow(2, u1(e));
}
bool if_lose()
{
for(int i = 0; i < 4; i++)
{
for(int j = 1; j < 4; j++)
{
if(mp[i][j] == mp[i][j - 1] || mp[j][i] == mp[j - 1][i]) return false;
if(mp[i][j] == 0 || mp[i][j - 1] == 0) return false;
}
}
return true;
}
void num_print(int need_prt)
{
for(int i = 0; i <= 20; i++)
{
if (need_prt == (1 << 1 + i * 6)) {
scta(gsh(soh), fi | red | green);
break;
} else if (need_prt == (1 << 2 + i * 6)) {
scta(gsh(soh), fi | green);
break;
} else if (need_prt == (1 << 3 + i * 6)) {
scta(gsh(soh), fi | green | blue);
break;
} else if (need_prt == (1 << 4 + i * 6)) {
scta(gsh(soh), fi | blue);
break;
} else if (need_prt == (1 << 5 + i * 6)) {
scta(gsh(soh), fi | blue | red);
break;
} else if (need_prt == (1 << 6 + i * 6)) {
scta(gsh(soh), fi | red);
break;
}
}
printf("%d", need_prt);
common_col;
}
void lose()
{
CLS;
printf("You lose!\n\n");
getch();
}
bool move()
{
bool flag = false;
char c = getch();
int temp[10][10];
for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) temp[i][j] = mp[i][j];
if(c == 'w')
{
for(int i = 0; i < 4; i++)
{
queue<int> q1, q2;
int lst;
for(int j = 0; j < 4; j++)
{
if(mp[j][i]) q1.push(mp[j][i]);
}
if(q1.empty()) continue;
lst = q1.front();
q1.pop();
while(!q1.empty())
{
if(lst == -1) lst = q1.front();
else if(lst == q1.front()) q2.push(2 * lst), lst = -1;
else q2.push(lst), lst = q1.front();
q1.pop();
}
if(lst != -1) q2.push(lst);
for(int j = 0; j < 4; j++) mp[j][i] = 0;
for(int j = 0; j < 4 && !q2.empty(); j++)
{
mp[j][i] = q2.front();
q2.pop();
}
}
}
else if(c == 's')
{
for(int i = 0; i < 4; i++)
{
queue<int> q1, q2;
int lst;
for(int j = 3; j >= 0; j--)
{
if(mp[j][i]) q1.push(mp[j][i]);
}
if(q1.empty()) continue;
lst = q1.front();
q1.pop();
while(!q1.empty())
{
if(lst == -1) lst = q1.front();
else if(lst == q1.front()) q2.push(2 * lst), lst = -1;
else q2.push(lst), lst = q1.front();
q1.pop();
}
if(lst != -1) q2.push(lst);
for(int j = 0; j < 4; j++) mp[j][i] = 0;
for(int j = 3; j >= 0 && !q2.empty(); j--)
{
mp[j][i] = q2.front();
q2.pop();
}
}
}
else if(c == 'a')
{
for(int i = 0; i < 4; i++)
{
queue<int> q1, q2;
int lst;
for(int j = 0; j < 4; j++)
{
if(mp[i][j]) q1.push(mp[i][j]);
}
if(q1.empty()) continue;
lst = q1.front();
q1.pop();
while(!q1.empty())
{
if(lst == -1) lst = q1.front();
else if(lst == q1.front()) q2.push(2 * lst), lst = -1;
else q2.push(lst), lst = q1.front();
q1.pop();
}
if(lst != -1) q2.push(lst);
for(int j = 0; j < 4; j++) mp[i][j] = 0;
for(int j = 0; j < 4 && !q2.empty(); j++)
{
mp[i][j] = q2.front();
q2.pop();
}
}
}
else if(c == 'd')
{
for(int i = 0; i < 4; i++)
{
queue<int> q1, q2;
int lst;
for(int j = 3; j >= 0; j--)
{
if(mp[i][j]) q1.push(mp[i][j]);
}
if(q1.empty()) continue;
lst = q1.front();
q1.pop();
while(!q1.empty())
{
if(lst == -1) lst = q1.front();
else if(lst == q1.front()) q2.push(2 * lst), lst = -1;
else q2.push(lst), lst = q1.front();
q1.pop();
}
if(lst != -1) q2.push(lst);
for(int j = 0; j < 4; j++) mp[i][j] = 0;
for(int j = 3; j >= 0 && !q2.empty(); j--)
{
mp[i][j] = q2.front();
q2.pop();
}
}
}
else if(c == '0')
{
int x, y, val;
cin >> x >> y >> val;
mp[x][y] = val;
}
for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) if(mp[i][j] != temp[i][j]) return true;
return false;
}
void game_main()
{
int max_num = 0;
memset(mp, 0, sizeof(mp));
while(1)
{
CLS;
num_give();
for (int i = 0; i < 4; i++)
{
printf("%s\n%s\n", break_up, empty_line);
for (int j = 0; j < 4; j++)
{
printf("|");
if(mp[i][j])
{
max_num = max(max_num, mp[i][j]);
num_print(mp[i][j]);
}
printf("\t");
}
printf("|\n%s\n", empty_line);
}
printf("%s", break_up);
if(if_lose())
{
lose();
return;
}
while(!move()){}
}
}
int main()
{
set_rand();
while (1)
{
common_col;
game_main();
}
return 0;
}