在运行的窗口实现,如上图,该代码也是编写图形模式的基础
详细解释看我之前的那篇
使用Easyx图形库制作的2048游戏_曦樂~的博客-CSDN博客
不过本文多了记分功能,还有历史最高分对比
头文件
int end = 0;//标记得到2048结束
int sum = 0;//分数
int max = 0;//最高分
void random(int [4][4]);//生成随机数
void move(int [4][4], char);//移动
void add(int [4][4], char);//相加
void out(int [4][4]);//输出
bool canNotMove(int [4][4]);//判断是否无法移动
bool full(int [4][4]);//是否存满
void run();//运行
void gotoxy(int x, int y);//光标移动到(x,y)位置,cls闪的我难受捏
void HideCursor(); // 用于隐藏光标
主函数
int main()
{
HideCursor();
printf("请输入wsad或上下左右键控制移动\n");
Sleep(2000);
system("cls");
run();
system("cls");
printf("结束");
}
处理闪烁问题
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);//检索指定标准设备(标准输出)的句柄
COORD pos;//坐标结构包含x,y
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);//set设置consol控制台cursor光标position位置
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 }; // 光标信息的结构体,{光标填充的字符单元格的百分比(1~100),是否显示}第二个值为0表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);//设置指定控制台屏幕缓冲区的光标的大小和可见性
}
生成随机数
void random(int arr[4][4])
{
time_t t1 = time(NULL);
srand((unsigned int)time(NULL));
int n;//赋值
if (rand() % 4 == 0)//四分之一的可能是4,其余是2
n = 4;
else
n = 2;
while (1)//找空位置赋值
{
int f=0;
int x = rand() % 4;//横坐标
int y = rand() % 4;//纵坐标
if (arr[x][y] == 0)
{
arr[x][y] = n;
break;
}
if (full(arr))//满了不加数字
{
break;
}
}
}
移动
void move(int arr[4][4], char in)
{
int c = 0;//要改变的坐标
int fnull = 0;//标记第一个空位
if (in == 'w'||in=='W'||in==72)//向上
{
for (int i = 0; i < 4; i++)//列
{
c = 0;
fnull = 0;
for (int j = 0; j < 4; j++)//行
{
if (arr[j][i] == 0 && fnull == 0)
{
fnull = 1;
c = j;
continue;
}
if (arr[j][i] != 0)
{
int t = arr[j][i];
arr[j][i] = 0;
arr[c][i] = t;
c++;
}
}
}
}
if (in == 's'||in=='S' || in == 80)//向下
{
for (int i = 0; i < 4; i++)//列
{
c = 3;
fnull = 0;
for (int j = 3; j >= 0; j--)//行
{
if (arr[j][i] == 0 && fnull == 0)
{
fnull = 1;
c = j;
continue;
}
if (arr[j][i] != 0)
{
int t = arr[j][i];
arr[j][i] = 0;
arr[c][i] = t;
c--;
}
}
}
}
if (in == 'a'||in=='A' || in == 75)//向左
{
for (int i = 0; i < 4; i++)//行
{
c = 0;
fnull = 0;
for (int j = 0; j < 4; j++)//列
{
if (arr[i][j] == 0 && fnull == 0)
{
fnull = 1;
c = j;
continue;
}
if (arr[i][j] != 0)
{
int t = arr[i][j];
arr[i][j] = 0;
arr[i][c] = t;
c++;
}
}
}
}
if (in == 'd'||in=='D' || in == 77)//向右
{
for (int i = 0; i < 4; i++)//行
{
c = 3;
fnull = 0;
for (int j = 3; j >= 0; j--)//列
{
if (arr[i][j] == 0 && fnull == 0)
{
fnull = 1;
c = j;
continue;
}
if (arr[i][j] != 0)
{
int t = arr[i][j];
arr[i][j] = 0;
arr[i][c] = t;
c--;
}
}
}
}
}
计算
void add(int arr[4][4], char in)
{
if (in == 'w' || in == 'W' || in == 72)
{
for (int i = 0; i < 4; i++)//向上加
{
for (int j = 0; j < 3; j++)
{
if (arr[j][i] == arr[j + 1][i])
{
arr[j][i] *= 2;
sum += arr[j][i];
if (arr[j][i] == 2048)
end = 1;
arr[j + 1][i] = 0;
move(arr, in);
}
}
}
}
if (in == 's' || in == 'S' || in == 80)
{
for (int i = 0; i < 4; i++)//向下加
{
for (int j = 3; j > 0; j--)
{
if (arr[j][i] == arr[j - 1][i])
{
arr[j][i] *= 2;
sum += arr[j][i];
if (arr[j][i] == 2048)
end = 1;
arr[j - 1][i] = 0;
move(arr, in);
}
}
}
}
if (in == 'a' || in == 'A' || in == 75)
{
for (int j = 0; j < 4; j++)//向左加
{
for (int i = 0; i < 3; i++)
{
if (arr[j][i] == arr[j][i + 1])
{
arr[j][i] *= 2;
sum += arr[j][i];
if (arr[j][i] == 2048)
end = 1;
arr[j][i + 1] = 0;
move(arr, in);
}
}
}
}
if (in == 'd'||in=='D' || in == 77)
{
for (int j = 0; j < 4; j++)//向右加
{
for (int i = 3; i > 0; i--)
{
if (arr[j][i] == arr[j][i - 1])
{
arr[j][i] *= 2;
sum += arr[j][i];
if (arr[j][i] == 2048)
end = 1;
arr[j][i - 1] = 0;
move(arr, in);
}
}
}
}
}
判满、是否能移动、输出
bool canNotMove(int arr[4][4])
{
if (!full(arr))
return false;
for (int i = 0; i < 4; i++)//行
{
for (int j = 0; j < 3; j++)//列
{
if (arr[i][j] == arr[i][j + 1])
{
return false;
}
}
}
for (int i = 0; i < 4; i++)//列
{
for (int j = 0; j < 3; j++)//行
{
if (arr[j][i] == arr[j+1][i])
{
return false;
}
}
}
return true;
}
bool full(int arr[4][4])
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (arr[i][j] == 0)
{
return false;
}
}
}
return true;
}
void out(int arr[4][4])//输出
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
printf("%-5d", arr[i][j]);
}
printf("\n\n");
}
}
运行
void run()
{
while (1)
{
gotoxy(0, 0);
int arr[4][4];//一个框
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
arr[i][j] = 0;
}
}
end = 0;
sum = 0;
printf("得分:%d\n", sum);
printf("-----------------\n");
random(arr);
while (end == 0)//一次
{
random(arr);
out(arr);
if (canNotMove(arr))
{
Sleep(1000);
break;
}
char in = 0;
Sleep(200);//防止乱按
while (1)
{
in = _getch();//输入
if ((in != 72) && (in != 80) && (in != 75) && (in != 77) && (in != 'w') && (in != 's') && (in != 'a') && (in != 'd') && (in != 'W') && (in != 'S') && (in != 'A') && (in != 'D'))
continue;
else
break;
}
move(arr, in);
add(arr, in);
gotoxy(0, 0);
printf("得分:%d\n", sum);
printf("-----------------\n");
if (sum > max)
max = sum;
}
system("cls");
if (end == 1)//通关
{
printf("通关!\n");
}
else
{
printf("失败\n");
}
printf("当前得分:%d\n", sum);
printf("历史最高分:%d\n", max);
printf("输入y再试一次,按n结束\n");
FLAG:
char a = _getch();
if (a == 'y' || a == 'Y')
{
system("cls");
continue;
}
if (a == 'n' || a == 'N')
{
break;
}
else
goto FLAG;
}
}