2048游戏(控制台版)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/gyhguoge01234/article/details/51793179


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <string.h>
int map[4][4];
int bmap[4][4];
int score,maxScore,flag;
FILE* pfile;
int UP();
int DOWN();
int LEFT();
int RIGHT();

int checkMap()
{
    int i,j;
    for(i = 0; i < 4; ++i)
        for(j = 0; j < 4; ++j)
            if(bmap[i][j] != map[i][j])
                return 1;
    return 0;
}

void saveMaxScore(int n)
{
    if(n > maxScore)
        maxScore = n;
    pfile = fopen("record.txt","w");
    fprintf(pfile,"%d",maxScore);
    fclose(pfile);
}
//设置地图
int appear(int n)
{
	int num = rand();
	int x, y, i;
	//计算随机数字x和y,确定下一次出现的坐标
	int SZ = num % 2;
	x = num % 4;
	srand((unsigned int)time(0) + (unsigned int)num);
	y = rand();
	y %= 4;
	//决定num的值,也就是下一个出现的数字
	if (SZ != 0)
		num = 2;
	else
		num = 4;
	if (n == 0)
	{
	    //这个是初始化的地图
		map[x][y] = num;
	}
	else if (n == 1)
	{
	    //上移后的地图
	    //上移之后,先找第四行的为0的格子,如果有,则设为num,否则,进行下边循环
		if (map[3][y] == 0)
			map[3][y] = num;
		else
            //在整个第四行寻找空格
			for (i = 0; i < 4; i++)
				if (map[3][i] == 0)
				{
					map[3][i] = num;
					break;
				}
	}
	else if (n == 2)
	{
	    //下移后的地图
		if (map[0][y] == 0)
			map[0][y] = num;
		else
			for (i = 0; i < 4; i++)
				if (map[0][i] == 0)
				{
					map[0][i] = num;
					break;
				}
	}
	else if (n == 3)
	{
	    //左移后的地图
		if (map[x][3] == 0)
			map[x][3] = num;
		else
			for (i = 0; i < 4; i++)
				if (map[i][3] == 0)
				{
					map[i][3] = num;
					break;
				}
	}
	else if (n == 4)
	{
	    //右移后的地图
		if (map[x][0] == 0)
			map[x][0] = num;
		else
			for (i = 0; i < 4; i++)
				if (map[i][0] == 0)
				{
					map[i][0] = num;
					break;
				}
	}
	return 0;
}
//移动 地图中的数字
int move()
{
	fflush(stdin);
	char key;
	key = getch();
	int SZ = 1;
	if (key == 72)
		SZ = UP();
	else if (key == 80)
		SZ = DOWN();
	else if (key == 75)
		SZ = LEFT();
	else if (key == 77)
		SZ = RIGHT();
    else if(key == 27)
    {
        flag = 1;
        return 0;
    }
	if (SZ == 0)
	{
		printf("\n\tCongratulations!!!You have reach 2048!\n");
		saveMaxScore(score);
	}
	return SZ;
}
//把数字整体向上移动
int UP()
{
    memcpy(bmap,map,sizeof(bmap));
	int i, j;
	int t, k;
	int SZ = 0;
	//j代表列,每列都向上移动
	for (j = 0; j < 4; j++)//对四列都进行
	{
		for (int n = 0; n < 4; n++)
		    //i代表行
			for (i = 0; i < 3; i++)//第0行到第3行(每行)
			    //第i行第j列如果等于0,则可移动
				if (map[i][j] == 0)
				{
					for (t = i; t < 3; t++)
					{
					    //把下一行移动到当前行,因为第一行移动了,所下边每行都要移动
						map[t][j] = map[t + 1][j];
						map[t + 1][j] = 0;
						SZ = 1;
					}
				}
		for (i = 0; i < 3; i++)
		    //如果当前列当前行的数字和下一行的数字相等,则合并数字
			if (map[i][j] == map[i + 1][j])
			{
			    //因为是向上移动,所以合并到靠上的那个数字上
				map[i][j] *= 2;
				score += map[i][j];
				if (map[i][j] == 2048)
					return 0;
                //因为上边的数字向上移动了,所以下边的数字都要向上移动
				for (k = i + 1; k < 3; k++)
				{
					map[k][j] = map[k + 1][j];
					map[k + 1][j] = 0;
					SZ = 1;
				}
			}
	}
	int bmap[4][4];
	if (SZ && checkMap())
		appear(1);
	return 1;
}
int DOWN()
{
    memcpy(bmap,map,sizeof(bmap));
	int i, j;
	int t, k;
	int SZ = 0;
	for (j = 0; j < 4; j++)
	{
		for (int n = 0; n < 4; n++)
			for (i = 3; i > 0; i--)
				if (map[i][j] == 0)
				{
					for (t = i; t>0; t--)
					{
						map[t][j] = map[t - 1][j];
						map[t - 1][j] = 0;
						SZ = 1;
					}
				}
		for (i = 3; i > 0; i--)
			if (map[i][j] == map[i - 1][j])
			{
				map[i][j] *= 2;
				score += map[i][j];
				if (map[i][j] == 2048)
					return 0;
				for (k = i - 1; k > 0; k--)
				{
					map[k][j] = map[k - 1][j];
					map[k - 1][j] = 0;
					SZ = 1;
				}
			}
	}
	if (SZ && checkMap())
		appear(2);
	return 1;
}
int LEFT()
{
    memcpy(bmap,map,sizeof(bmap));
	int i, j;
	int t, k;
    int SZ = 0;
	for (i = 0; i < 4; i++)
	{
		for (int n = 0; n < 4; n++)
			for (j = 0; j < 3; j++)
				if (map[i][j] == 0)
				{
					for (t = j; t < 3; t++)
					{
						map[i][t] = map[i][t + 1];
						map[i][t + 1] = 0;
						SZ = 1;
					}
				}
		for (j = 0; j < 3; j++)
			if (map[i][j] == map[i][j + 1])
			{
				map[i][j] *= 2;
				score += map[i][j];
				if (map[i][j] == 2048)
					return 0;
				for (k = j + 1; k < 3; k++)
				{
					map[i][k] = map[i][k + 1];
					map[i][k + 1] = 0;
				}
			}
	}
	if(SZ && checkMap())
        appear(3);
	return 1;
}
int RIGHT()
{
    memcpy(bmap,map,sizeof(bmap));
	int i, j;
	int t, k;
	int SZ = 0;
	for (i = 0; i < 4; i++)
	{
		for (int n = 0; n < 4; n++)
			for (j = 3; j > 0; j--)
				if (map[i][j] == 0)
				{
					for (t = j; t > 0; t--)
					{
						map[i][t] = map[i][t - 1];
						map[i][t - 1] = 0;
						SZ = 1;
					}
				}
		for (j = 3; j > 0; j--)
			if (map[i][j] == map[i][j - 1])
			{
				map[i][j] *= 2;
				score += map[i][j];
				if (map[i][j] == 2048)
					return 0;
				for (k = j - 1; k > 0; k--)
				{
					map[i][k] = map[i][k - 1];
					map[i][k - 1] = 0;
				}
			}
	}
	if(SZ && checkMap())
        appear(4);
	return 1;
}
int printmap()
{
	printf("Your Score:%d\n\n", score);
	printf("\t\t --------------------\n");
	printf("\t\t|%4d|%4d|%4d|%4d|\n",map[0][0],map[0][1],map[0][2], map[0][3]);
	printf("\t\t|----+----+----+----|\n");
	printf("\t\t|%4d|%4d|%4d|%4d|\n",map[1][0],map[1][1],map[1][2], map[1][3]);
	printf("\t\t|----+----+----+----|\n");
	printf("\t\t|%4d|%4d|%4d|%4d|\n",map[2][0],map[2][1],map[2][2], map[2][3]);
	printf("\t\t|----+----+----+----|\n");
	printf("\t\t|%4d|%4d|%4d|%4d|\n",map[3][0],map[3][1],map[3][2], map[3][3]);
	printf("\t\t --------------------\n");
	return 0;
}

//判断相邻数字是否相等或者还有空格,如果有相等的或者还有空格,游戏继续,否则结束游戏
int judge()
{
    int k,i,j,tx,ty;
    int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
    int vis[5][5];
    memset(vis,0,sizeof(vis));
    if(map[0][0] == 0)
        return 0;
    for(i = 0; i < 4; ++i)
        for(j = 0; j < 4; ++j)
        {
            vis[i][j] = 1;
            for(k = 0; k < 4; ++k)
            {
                tx = i + next[k][0];
                ty = j + next[k][1];
                if(tx >= 0 && ty >= 0 && tx < 4 && ty < 4 && !vis[tx][ty])
                {
                    if(map[i][j] == map[tx][ty] || map[tx][ty] == 0)
                        return 0;
                }
            }
        }

	printf("                           GAME OVER!\n                           Your score is %2d\n", score);
	saveMaxScore(score);
	return 1;
}

int main()
{
    srand((unsigned int)time(NULL));
    pfile = fopen("record.txt","r");
    if(pfile)
        fscanf(pfile,"%d",&maxScore);
    else
        maxScore = 0;
    fclose(pfile);
    printf("\t\t\t==================================================\n");
	printf("\t\t\t||                    2048                      ||\n");
	printf("\t\t\t|| Through the keyboard direction key operation.||\n");
	printf("\t\t\t||     Enter any charactor except 0 to start.   ||\n");
	printf("\t\t\t||         Press ESC to finish the game.        ||\n");
	printf("\t\t\t||  Press the H key to query the highest score  ||\n");
	printf("\t\t\t==================================================\n");
	char start[100];
	while (scanf("%s", start), strcmp(start, "0"))
	{
	    if(start[0] == 'h' || start[0] == 'H')
        {
            printf("Highest Score:%d\n",maxScore);
            continue;
        }
	    //清空地图
		memset(map, 0, sizeof(map));
		score = 0;
		//设置初始地图
		appear(0);
		appear(0);
		system("CLS");
		//显示地图
		printmap();
		while (move() != 0)
		{
			fflush(stdin);
			system("CLS");
			printmap();
			if (judge() != 0)
				break;
		}
		if(flag)
            break;
		printf("\t\t Enter any charactor except 0 to start.Press 0 to quit!\n");
	}
    return 0;
}


展开阅读全文

没有更多推荐了,返回首页