C语言实现2048极其详细

一 游戏描述

2048是一款数字益智类游戏,玩家需要使用键盘控制数字方块的移动,合并相同数字的方块,最终达到数字方块上出现“2048”的目标。

        每次移动操作,所有数字方块会朝着指定方向同时滑动,并在靠近边界或其他数字方块时停止。如果两个相邻的数字方块数字相同,则它们会合并成一个方块,数字值为原先相同方块的数字值相加。

二 结果展示

  1. 游戏显示在终端中,可以通过w(向上)s(向下)a(向左)d(向右)进行上下左右四个方向的移动并且在上方显示游戏得分情况,每次移动都会显示移动方向和分数。
  2. 每次移动都会从2或4中生成两个随机数
  3. 当数据不能移动产生新的随机数时我们判定游戏失败。
  4. 当有数值到达2048显示游戏胜利

 三 项目代码介绍

   一 结构体介绍

struct data//结构体存数据
{
    int x;//存储x轴地址
    int y;//存储y轴地址
    int score;
};

我们定义一个结构进行存储每个空间的位置和数值

  二 显示模块

/*
显示当前棋盘数字的情况
struct data *p接收数组首地址
int *grade接收当前的分数
*/
void show(struct data *p,int *grade)
{
    printf("分数为%d\n",*grade);//输出当前的分数
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            printf("%d\t",(p+(4*i+j))->score);
        }
        printf("\n");
    }
    
}
  1. 我们定义一个函数进行接受结构体数组的首地址和当前的分数
  2. 通过当前结构体首地址进行遍历整个结构体然后进行输出每个结构体所在位置的数值

  三 随机数生成模块 

srand(time(NULL));
	while (1)
	{
		int x = rand() % 4;   
		int y = rand() % 4;   
		if ((p+4*x+y)->score == 0)      //查看随机的坐标是否等于0
		{

    	    if (rand() % 10 <5)    //判断随机数取10余数是否<5,来限制4出现的几率为50%
			{
				(p+4*x+y)->score = 4;
                break;
			}
			else
			{
				(p+4*x+y)->score = 2; //否则2出现的几率为50%  
                break;   
			}
			
		}
	}
  1. srand(time(NULL));用来进行随机数种子的改变,是生成的随机数不是相同的
  2. rand()%4随机生成0~3中的数字
  3. rand()%10生成0~9的随机数,如果小于5时生成4,大于等于5时生成2
  4. 随机选定点位,如果这个点位数值为0我们就随机从2或4中生成一个数

四 向上移动模块

我将移动模块分成两个部分,一是计算每个位置的数值,二根据计算结束的数值进行移动

        一数值计算模块

        for(int x=0;x<4;x++)
        {
            for(int y=0;y<3;y++)
            {                       //左右相邻格子数值相同时并且值小于2048将二者相加
                if(((p+4*x+y)->score==(p+4*(x-1)+y)->score)&&((p+4*x+y)->score<2048)&&((p+4*x+y)->score!=0))//两数值相等且不为0时进行计算
                {
                    *grade+=(p+4*x+y)->score;//计算当前的分数
                    (p+4*x+y)->score=0;//后面的一位置为0
                    (p+4*x+y+1)->score*=2;//前面的值是原来的二倍
                }
            }
        }
  1. 我们通过两个for循环来进行结构体数组的遍历
  2. (p+4*x+y)->score==(p+4*(x-1)+y)->用来判段上下两个数值是否相等

  3. (p+4*x+y)->score<2048相加的数值要小于2048

  4. (p+4*x+y)->score!=0当数值为0时不进行相加

  5. 当满足if条件时我们进行俩数值的相加,将前面的数据变成原来的二倍,后面的数据变成0

  6. *grade+=(p+4*x+y)->score每当有数值合并时我们将总分加上数值就是我们当前的得分

         二模块移动

        //位置移动
        for(int x=0;x<4;x++)
        {
            for(int y=0;y<3;y++)
            {                       
                if(((p+4*x+y+1)->score==0)&&((p+4*x+y)->score!=0))//当前面是数为0且后面数不为0时互换两者数值
                {   
                    (p+4*x+y+1)->score=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;
                }
            }
        }
  1. (p+4*x+y+1)->score==0判断前面的数据是否为0
  2. (p+4*x+y)->score!=0判断移动的数据是否为0
  3. 如果前面数据为0并且后面数据不为0时我们将前后位置的数值互换

 五 完整的移动模块代码

        一 向上移动模块
/*
    棋子向上移动
    struct data *p接收数组首地址
*/
void up(struct data*p,int *grade)//向上移动操作
{
    printf("向上移动\n");
    for(int k=0;k<3;k++)
    {   //数值移动
        for(int y=0;y<4;y++)
        {
            for(int x=3;x>0;x--)
            {                       //上下相邻格子数值相同时并且值小于2048将二者相加
                if((p+4*x+y)->score==(p+4*(x-1)+y)->score&&(p+4*x+y)->score<2048)
                {
                   *grade+=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;//后面的一位置为0
                    (p+4*(x-1)+y)->score*=2;//前面的值是原来的二倍
                }
            }
        }
        
        //位置移动
        for(int y=0;y<4;y++)
        {
            for(int x=3;x>0;x--)
            {                       
                if((p+4*(x-1)+y)->score==0)//当前面是数为0是互换两者数值
                {
                    (p+4*(x-1)+y)->score=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;
                }
            }
        }
    }
}
        二 向下移动模块
/*
    棋子向下移动
    struct data *p接收数组首地址
*/
void down(struct data*p,int *grade)//向下移动操作
{
    printf("向下移动\n");
    for(int k=0;k<3;k++)
    {   //数值移动
        for(int y=0;y<4;y++)
        {
            for(int x=0;x<3;x++)
            {                       //上下相邻格子数值相同时并且值小于2048将二者相加
                if((p+4*x+y)->score==(p+4*(1+x)+y)->score&&(p+4*x+y)->score<2048)
                {
                    *grade+=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;//后面的一位置为0
                    (p+4*(x+1)+y)->score*=2;//前面的值是原来的二倍
                }
            }
        }
        
        //位置移动
        for(int y=0;y<4;y++)
        {
            for(int x=0;x<4;x++)
            {                       
                if((p+4*(x+1)+y)->score==0)//当前面是数为0是互换两者数值
                {
                    (p+4*(x+1)+y)->score=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;
                }
            }
        }
    }
}
三 向左移动模块
/*
    棋子向左移动
    struct data *p接收数组首地址
*/
void left(struct data*p,int *grade)//向左移动操作
{
    printf("向左移动\n");
    for(int k=0;k<3;k++)
    {   //数值移动
        for(int x=0;x<4;x++)
        {
            for(int y=3;y>0;y--)
            {                       //左右相邻格子数值相同时并且值小于2048将二者相加
                if((p+4*x+y)->score==(p+4*x+y-1)->score&&(p+4*x+y)->score<2048)
                {
                    *grade+=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;//后面的一位置为0
                    (p+4*x+y-1)->score*=2;//前面的值是原来的二倍
                }
            }
        }
        
        //位置移动
        for(int x=0;x<4;x++)
        {
            for(int y=3;y>0;y--)
            {                       
                if((p+4*x+y-1)->score==0)//当前面是数为0是互换两者数值
                {
                    (p+4*x+y-1)->score=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;
                }
            }
        }
    }

}
        四向右移动模块
/*
    棋子向右移动
    struct data *p接收数组首地址
*/
void right(struct data*p,int *grade)//向右移动操作
{
    printf("向右移动\n");
    for(int k=0;k<3;k++)
    {   //数值移动
        for(int x=0;x<4;x++)
        {
            for(int y=0;y<3;y++)
            {                       //左右相邻格子数值相同时并且值小于2048将二者相加
                if((p+4*x+y)->score==(p+4*x+y+1)->score&&(p+4*x+y)->score<2048)
                {
                    *grade+=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;//后面的一位置为0
                    (p+4*x+y+1)->score*=2;//前面的值是原来的二倍
                }
            }
        }
        
        //位置移动
        for(int x=0;x<4;x++)
        {
            for(int y=0;y<3;y++)
            {                       
                if((p+4*x+y+1)->score==0)//当前面是数为0是互换两者数值
                {                     
                    (p+4*x+y+1)->score=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;
                }
            }
        }
    }

}

四 头节点mode.h

#ifndef     __MODE__H//每个函.c文件只能调用这个头节点一次
#define     __MODE__H

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


struct data//结构体存数据
{
    int x;
    int y;
    int score;
};


void show(struct data* p,int *grade);//显示棋盘数据


void right(struct data*p,int *grade);//向右移动


void left(struct data*p,int *grade);//向左移动

 
void up(struct data*p,int *grade);//向上移动


void down(struct data*p,int *grade);//向下移动


void newNum(struct data*p,int *grade);//产生随机数


#endif

五 main.c文件介绍

  一 数据初始化

 struct data *p = calloc(16,sizeof(struct data));//申请空间进行存放数据
    
    int grade = 0;

    for(int i=0;i<4;i++)//初始化数据
    {
        for (int j=0;j<4;j++)
        {
            (p+(4*i+j))->x=i; 
            (p+(4*i+j))->y=j;
            
            (p+(4*i+j))->score=0;
            //printf("score0 = %d\n", (p+4*i+j)->score);
        }
        //printf("\n");
    }
  1. 申请一个空间进行存储数据p是空间的首地址
  2. 定义一个全局变量grade来存储分数 
  3. 将每一个数据的值都赋值为0

 二  判断胜负


        //判断胜负
        int temp=0,win=0;
        for(int i=0;i<16;i++)
        {
            if((p+i)->score!=0)//判断格子有数据的个数
            {
                temp++;
            }
            if((p+i)->score==2048)//判断是否有数为2048
            {
                win++;
            }
        }
       
        if(win>0)
        {
            printf("游戏胜利");//数据达到2048游戏胜利
            break;
        }
        if(temp>14)//数据大于14就不能生成两个随机数了,失败
        {
            printf("游戏失败");
            break;
        }
  1. 申请两个变量temp记录已有数据的格子个数win记录是否有数为2048
  2. 若temp的值大于14,下次随机数生成将不能产生两个数判断游戏失败
  3. 若win的值大于0就表示有数据生成

 六 完整代码

 一 main.c


//show((struct data *)p);显示棋盘数据情况

//right((struct data*)p);向右移动

//left((struct data*)p);向左移动

//up(struct data*p);//向上移动

//down(struct data*p);//向下移动

#include "mode.h"

int main()
{
    
    struct data *p = calloc(16,sizeof(struct data));//申请空间进行存放数据
    
    int grade = 0;

    for(int i=0;i<4;i++)//初始化数据
    {
        for (int j=0;j<4;j++)
        {
            (p+(4*i+j))->x=i; 
            (p+(4*i+j))->y=j;
            
            (p+(4*i+j))->score=0;
            //printf("score0 = %d\n", (p+4*i+j)->score);
        }
        //printf("\n");
    }
    
    while(1)
    {
        newNum(p,&grade);//产生随机数
        char dierction;//存储空间移动的存储
        printf("请输入移动方向");
        scanf("%c",&dierction); 
       
        while(getchar()!='\n');//去除scanf的缓存空间
        
        

        if(dierction=='!')//方向移动
            break;//退出游戏
        else if(dierction=='w')
            up(p,&grade);
        else if(dierction=='s')
            down(p,&grade);
        else if(dierction=='a')
            left(p,&grade);
        else if(dierction=='d')
            right(p,&grade);

        //判断胜负
        int temp=0,win=0;
        for(int i=0;i<16;i++)
        {
            if((p+i)->score!=0)//判断格子有数据的个数
            {
                temp++;
            }
            if((p+i)->score==2048)//判断是否有数为2048
            {
                win++;
            }
        }
       
        if(win>0)
        {
            printf("游戏胜利");//数据达到2048游戏胜利
            break;
        }
        if(temp>14)//数据大于14就不能生成两个随机数了,失败
        {
            printf("游戏失败");
            break;
        }

    }
    
    return 0;
}

二mode.c

#include "mode.h"

void newNum(struct data *p,int *grade)
{
    srand(time(NULL));
	while (1)
	{
		int x = rand() % 4;   
		int y = rand() % 4;   
		if ((p+4*x+y)->score == 0)      //查看随机的坐标是否等于0
		{

    	    if (rand() % 10 <5)    //判断随机数取10余数是否<5,来限制4出现的几率为50%
			{
				(p+4*x+y)->score = 4;
                break;
			}
			else
			{
				(p+4*x+y)->score = 2; //否则2出现的几率为50%  
                break;   
			}
			
		}
	}

    while (1)
	{
		int x = rand() % 4;   
		int y = rand() % 4;   
		if ((p+4*x+y)->score == 0)      //查看随机的坐标是否等于0
		{

    	    if (rand() % 10 <5)    //判断随机数取10余数是否<5,来限制4出现的几率为50%
			{
				(p+4*x+y)->score = 4;
			}
			else
			{
				(p+4*x+y)->score = 2;    //否则2出现的几率为50%  
			}
			break;
		}
	}
    show(p, grade);
}



/*
显示当前棋盘数字的情况
struct data *p接收数组首地址
*/
void show(struct data *p,int *grade)
{
    printf("分数为%d\n",*grade);
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            printf("%d\t",(p+(4*i+j))->score);
        }
        printf("\n");
    }
    
}




/*
    棋子向右移动
    struct data *p接收数组首地址
*/
void right(struct data*p,int *grade)//向右移动操作
{
    printf("向右移动\n");
    for(int k=0;k<3;k++)
    {   //数值移动
        for(int x=0;x<4;x++)
        {
            for(int y=0;y<3;y++)
            {                       //左右相邻格子数值相同时并且值小于2048将二者相加
                if((p+4*x+y)->score==(p+4*x+y+1)->score&&(p+4*x+y)->score<2048)
                {
                    *grade+=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;//后面的一位置为0
                    (p+4*x+y+1)->score*=2;//前面的值是原来的二倍
                }
            }
        }
        
        //位置移动
        for(int x=0;x<4;x++)
        {
            for(int y=0;y<3;y++)
            {                       
                if((p+4*x+y+1)->score==0)//当前面是数为0是互换两者数值
                {                     
                    (p+4*x+y+1)->score=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;
                }
            }
        }
    }

}






/*
    棋子向左移动
    struct data *p接收数组首地址
*/
void left(struct data*p,int *grade)//向左移动操作
{
    printf("向左移动\n");
    for(int k=0;k<3;k++)
    {   //数值移动
        for(int x=0;x<4;x++)
        {
            for(int y=3;y>0;y--)
            {                       //左右相邻格子数值相同时并且值小于2048将二者相加
                if((p+4*x+y)->score==(p+4*x+y-1)->score&&(p+4*x+y)->score<2048)
                {
                    *grade+=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;//后面的一位置为0
                    (p+4*x+y-1)->score*=2;//前面的值是原来的二倍
                }
            }
        }
        
        //位置移动
        for(int x=0;x<4;x++)
        {
            for(int y=3;y>0;y--)
            {                       
                if((p+4*x+y-1)->score==0)//当前面是数为0是互换两者数值
                {
                    (p+4*x+y-1)->score=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;
                }
            }
        }
    }

}





/*
    棋子向下移动
    struct data *p接收数组首地址
*/
void down(struct data*p,int *grade)//向下移动操作
{
    printf("向下移动\n");
    for(int k=0;k<3;k++)
    {   //数值移动
        for(int y=0;y<4;y++)
        {
            for(int x=0;x<3;x++)
            {                       //上下相邻格子数值相同时并且值小于2048将二者相加
                if((p+4*x+y)->score==(p+4*(1+x)+y)->score&&(p+4*x+y)->score<2048)
                {
                    *grade+=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;//后面的一位置为0
                    (p+4*(x+1)+y)->score*=2;//前面的值是原来的二倍
                }
            }
        }
        
        //位置移动
        for(int y=0;y<4;y++)
        {
            for(int x=0;x<4;x++)
            {                       
                if((p+4*(x+1)+y)->score==0)//当前面是数为0是互换两者数值
                {
                    (p+4*(x+1)+y)->score=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;
                }
            }
        }
    }
}






/*
    棋子向上移动
    struct data *p接收数组首地址
*/
void up(struct data*p,int *grade)//向上移动操作
{
    printf("向上移动\n");
    for(int k=0;k<3;k++)
    {   //数值移动
        for(int y=0;y<4;y++)
        {
            for(int x=3;x>0;x--)
            {                       //上下相邻格子数值相同时并且值小于2048将二者相加
                if((p+4*x+y)->score==(p+4*(x-1)+y)->score&&(p+4*x+y)->score<2048)
                {
                   *grade+=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;//后面的一位置为0
                    (p+4*(x-1)+y)->score*=2;//前面的值是原来的二倍
                }
            }
        }
        
        //位置移动
        for(int y=0;y<4;y++)
        {
            for(int x=3;x>0;x--)
            {                       
                if((p+4*(x-1)+y)->score==0)//当前面是数为0是互换两者数值
                {
                    (p+4*(x-1)+y)->score=(p+4*x+y)->score;
                    (p+4*x+y)->score=0;
                }
            }
        }
    }
}



三 mode.h

#ifndef     __MODE__H
#define     __MODE__H

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


struct data//结构体存数据
{
    int x;
    int y;
    int score;
};


void show(struct data* p,int *grade);//显示棋盘数据


void right(struct data*p,int *grade);//向右移动


void left(struct data*p,int *grade);//向左移动

 
void up(struct data*p,int *grade);//向上移动


void down(struct data*p,int *grade);//向下移动


void newNum(struct data*p,int *grade);//产生随机数


#endif


运行时要三个文件一起运行

以下是2048小游戏的C语言完整版代码: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> #define ROW 4 #define COL 4 int board[ROW][COL] = {0}; int score = 0; void init_board() { int i, j; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { board[i][j] = 0; } } srand(time(NULL)); int r1 = rand() % ROW; int c1 = rand() % COL; board[r1][c1] = 2; int r2 = rand() % ROW; int c2 = rand() % COL; while (r2 == r1 && c2 == c1) { r2 = rand() % ROW; c2 = rand() % COL; } board[r2][c2] = 2; score = 0; } void draw_board() { system("cls"); printf("-----------------------------\n"); printf("| 2048 Game |\n"); printf("-----------------------------\n"); printf("Score: %d\n", score); int i, j; for (i = 0; i < ROW; i++) { printf("|"); for (j = 0; j < COL; j++) { if (board[i][j] == 0) { printf(" |"); } else { printf("%-6d|", board[i][j]); } } printf("\n"); printf("-----------------------------\n"); } } void merge(int arr[]) { int i, j; for (i = 0; i < COL - 1; i++) { if (arr[i] == 0) { continue; } for (j = i + 1; j < COL; j++) { if (arr[j] == 0) { continue; } if (arr[i] == arr[j]) { arr[i] += arr[j]; arr[j] = 0; score += arr[i]; break; } else { break; } } } } void shift_left() { int i, j; for (i = 0; i < ROW; i++) { merge(board[i]); for (j = 0; j < COL - 1; j++) { if (board[i][j] == 0) { int k; for (k = j + 1; k < COL; k++) { if (board[i][k] != 0) { board[i][j] = board[i][k]; board[i][k] = 0; break; } } } } } } void shift_right() { int i, j; for (i = 0; i < ROW; i++) { merge(board[i]); for (j = COL - 1; j > 0; j--) { if (board[i][j] == 0) { int k; for (k = j - 1; k >= 0; k--) { if (board[i][k] != 0) { board[i][j] = board[i][k]; board[i][k] = 0; break; } } } } } } void shift_up() { int i, j, k; for (j = 0; j < COL; j++) { int arr[ROW] = {0}; for (i = 0, k = 0; i < ROW; i++) { arr[k++] = board[i][j]; } merge(arr); for (i = 0, k = 0; i < ROW; i++) { board[i][j] = arr[k++]; } } } void shift_down() { int i, j, k; for (j = 0; j < COL; j++) { int arr[ROW] = {0}; for (i = ROW - 1, k = 0; i >= 0; i--) { arr[k++] = board[i][j]; } merge(arr); for (i = ROW - 1, k = 0; i >= 0; i--) { board[i][j] = arr[k++]; } } } int game_over() { int i, j; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { if (board[i][j] == 0) { return 0; } if (i < ROW - 1 && board[i][j] == board[i + 1][j]) { return 0; } if (j < COL - 1 && board[i][j] == board[i][j + 1]) { return 0; } } } return 1; } int get_key() { int ch = getch(); if (ch == 0xE0 || ch == 0) { ch = getch(); switch (ch) { case 72: return 'w'; case 80: return 's'; case 75: return 'a'; case 77: return 'd'; } } return ch; } int main() { init_board(); draw_board(); while (1) { int key = get_key(); switch (key) { case 'w': shift_up(); break; case 's': shift_down(); break; case 'a': shift_left(); break; case 'd': shift_right(); break; case 'q': exit(0); default: continue; } if (game_over()) { printf("Game Over!\n"); break; } int r = rand() % ROW; int c = rand() % COL; while (board[r][c] != 0) { r = rand() % ROW; c = rand() % COL; } board[r][c] = 2; draw_board(); } return 0; } ``` 可以在命令行中编译运行,实现了基本的2048游戏功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值