自己写的一个控制台2048小游戏
#include <stdio.h>
#include <stdlib.h>
#include <conio.h> //提供 getch()
#include <windows.h>
#include <time.h> //提供 time() 用作随机数种子
int arr[4][4], copy[4][4]; //copy数组用于确认一次操作之后是否发生变化,如果没有变化则不生成新的数
int temp[4];
int empty; //空格数,如果 empty = 0 那么将结束本次游戏
void init(); //初始化游戏
void show(); //将游戏画面输出
int isDifferent(); //判断输入的操作是否改变数组的值,若不改变,则不添加新的数
void op_up(); //上移操作
void op_down(); //下移操作
void op_left(); //左移操作
void op_right(); //右移操作
void addNum(); //添加新的数
void play(); //提供游戏操作输入
int main()
{
printf("Enter any key to play game\n");
getch();
while (1)
{
system("cls");
init();
show();
while (empty) play();
printf("Game Over\n Please enter 'q' to quit, or any other keys to continue\n");
if (getch() == 'q') exit(0);
}
return 0;
}
void init()
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
copy[i][j] = arr[i][j] = 0;
srand(time(0));
int x = rand() % 4;
int y = rand() % 4;
copy[x][y] = arr[x][y] = 2; //在随机位置生成数字 2 作为开局
empty = 15; //更新 empty
}
void show()
{
for (int i = 0; i < 4; i++)
{
printf("_________________________\n");