在C语言中,实现2048的位移其实很简单

C语言下的命令行游戏:2048


源代码:下载

提取码:l6o3

 

 

 

 

 

 

 

全局变量的引用:

int NULLnumber;//空位查找
int Game_user;//游戏结束时分数
int MaxUser;//最高分
int Game_eazynumber[4][4] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };//游戏数据

 

主菜单绘制:

void main()//main函数游戏开始
{
	char Game_temp;
	char c;
	srand(time(NULL));
	Game_user = 0;
	while (1)
	{
		system("CLS");
		printf("2048 Gaming\n\n");
		printf("-------------------------\n");
		printf("|  8  | 2048|  64 |  64 |\n");
		printf("-------------------------\n");
		printf("|  0  |  2  |  8  |  0  |\n");
		printf("-------------------------\n");
		printf("|  0  |  0  |  0  |  0  |\n");
		printf("-------------------------\n");
		printf("|  0  |  0  |  0  |  0  |\n");
		printf("-------------------------\n");
		printf("欢迎游玩2048 C++编制\n\n");
		printf("请输入指令:\n\tA.开始 \n\tB.帮助\n\tC.退出\n指令:");//开始菜单
		fflush(stdin);
		Game_temp=getchar();
		c = getchar();
		switch (Game_temp)
		{
		case 'A':
		case 'a':Game_start();break;
		case 'B':
		case 'b':Game_help();break;
		case 'C':
		case 'c':exit(0);break;
		default:printf("无当前指令,请重试\n");break;
		}
	}
}

游戏菜单界面

void Draw()//绘制按键
{
	printf("方向键如下:\n");
	printf("\t\t-----\n\t\t| W |\n\t\t-----\n");
	printf("\t  ----- ----- -----\n\t  | A | | S | | D |\n\t  ----- ----- -----\n");
	printf("请输入按键字母:");
}
void Game_start()//游戏开始
{
	char Game_temp;
	char c;
	int numm;
	system("CLS");
	printf("2048游戏开始\n\n");
	while (1)
	{
		Game_numberNUll();
		Game_random();
		numm=Game_over();
		if(numm==1)
		{
			if(Game_user>MaxUser)
			{
				MaxUser=Game_user;
			}
			printf("\nGame Over\n\n");
			printf("您本次得分为%d分,历史最高是%d分!\n如要重玩,请重置游戏!\n",Game_user,MaxUser);
		}
		printf("(R:重置游戏,E:返回上一层,F:退出)\n");
		printf("请输入指令:\n\n");//开始菜单
		Game_now();
		Draw();
		fflush(stdin);
		scanf("%c",&Game_temp);
		c = getchar();
		switch (Game_temp)
		{
		case 'W':
		case 'w':system("CLS");Game_UP();break;
		case 'A':
		case 'a':system("CLS");Game_Left();break;
		case 'S':
		case 's':system("CLS");Game_DOWN();break;
		case 'D':
		case 'd':system("CLS");Game_right();break;
		case 'R':
		case 'r':system("CLS");Rest();printf("重置成功!\n");break;
		case 'E':
		case 'e':return;
		case 'F':
		case 'f':exit(0);
		default:system("CLS");printf("无当前指令,请重试\n");break;
		}
	}
}

上下左右(举例上):

void Game_UP()//S向上
{
	int x, y, Temp;
	for (y = 1;y < 4;y += 1)
	{
		for (x = 0;x < 4;x += 1)
		{
			if (Game_eazynumber[y-1][x] == 0)
			{
				for (Temp = y;Temp < 4;Temp++)
				{
						Game_eazynumber[Temp - 1][x] = Game_eazynumber[Temp][x];
				}
				Game_eazynumber[3][x]=0;

			}
		}
	}
	for (y = 1;y < 4;y += 1)
	{
		for (x = 0;x < 4;x += 1)
		{
			if (Game_eazynumber[y-1][x] != 0)
			{
				if(Game_eazynumber[y - 1][x] == Game_eazynumber[y][x])
				{
					Game_eazynumber[y - 1][x] =Game_eazynumber[y - 1][x]*2;
					Game_eazynumber[y][x]=0;
					Game_user+=Game_eazynumber[y - 1][x];
				}

			}
		}
	}
	for (y = 1;y < 4;y += 1)
	{
		for (x = 0;x < 4;x += 1)
		{
			if (Game_eazynumber[y-1][x] == 0)
			{
				for (Temp = y;Temp < 4;Temp++)
				{
						Game_eazynumber[Temp - 1][x] = Game_eazynumber[Temp][x];
				}
				Game_eazynumber[3][x]=0;
			}
		}
	}	
}

主要的随机数生成:

void Game_numberNUll()//查找空位
{
	int x, y;
	NULLnumber=0;
	for (y = 0;y < 4;y += 1)
	{
		for (x = 0;x < 4;x += 1)
		{
			if (Game_eazynumber[y][x] == 0)
			{
				NULLnumber += 1;
			}
		}
	}
}

void Game_random()//游戏随机数
{
	int x, y;
	if(NULLnumber==0)
	{
		return;
	}
	else
	{
		int num= rand() % NULLnumber + 1;
			for (y = 0;y < 4;y += 1)
			{
				for (x = 0;x < 4;x += 1)
				{
					if (Game_eazynumber[y][x] == 0)
					{
						num --;
					}
					if (num == 0)
					{
						int num2=rand() % 2 + 1;
						{
							if(num2==1)
							{
								num2=2;
							}else
							{
								num2=4;
							}
						}
						Game_eazynumber[y][x] =num2;
						return;
					}
				}
			}
	}
}