2048游戏C语言代码

#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>

// 4*4方格
int a[4][4] = {0};
// 方格里空格的个数
int empty;
int old_y, old_x;


void draw();
void play();
void init();
void draw_one(int y, int x);
void cnt_value(int *new_y, int *new_x);
int game_over();
int cnt_one(int y, int x);

int main()
{
    init();
    play();
    endwin();

    return 0;
}
void init()
{
    int x, y;

    initscr();	//初始化屏幕
    cbreak();	//如果没有设置CBREAK模式,
				//从键盘输入的字符都将被存储在缓冲区里面
				//直到输入回车键或者行结束符
    noecho();	//ECHO模式用来决定用户的输入是否立即回显
    curs_set(0);//0、1、2,分别表示光标的状态为隐藏、正常、高亮显示

    empty = 15;
    srand(time(0));
    x = rand() % 4;
    y = rand() % 4;
    a[y][x] = 2;
    draw();
}
void draw()
{
    int n, m, x, y;
    char c[4] = {'0', '0', '0', '0'};

    clear();
    for(n = 0; n < 9; n += 2)   //横线
        for(m = 0; m < 21; m++) {
            move(n, m);	//光标移动到给定的位置,这个位置是相对与屏幕的左上角而言,
						//左上角的坐标为(0,0)
            addch('-');	//a函数在当前光标处输出一个字符,
						//同时光标将向右移动一个位置
            refresh();	//刷新屏幕
        }
    for(m = 0; m < 22; m += 5)  //竖线
        for(n = 1; n < 8; n++) {
            move(n, m);
            addch('|');
            refresh();
        }
    for(y = 0; y < 4; y++)     //数字
        for(x = 0; x < 4; x++) {
            draw_one(y, x);
        }
}
//将a[y][x]完整的显示在屏幕上
//例如值为16,则c[0]=6,c[1]=1,逐位输出
void draw_one(int y, int x)
{
    int i, m, k, j;
    char c[4] = {'0', '0', '0', '0'};

    i = a[y][x];
    m = 0;
    do {
        j = i % 10;
        c[m++] = j + '0';
        i = i / 10;
    }while(i > 0);
    m = 0;
    k = (x + 1) * 5 - 1;
    while(c[m] != '0') {
        move(2*y+1, k);
        addch(c[m++]);
        k--;//
    }
}

void play()
{
    int x, y, i, new_x, new_y, tmp;
    int old_empty, move;
    char ch;

    while(1) {
        move = 0;
        old_empty = empty;
        //draw();
        ch = getch();
        switch(ch) {
            case 'A':
            case 'a':
                //从左向右消去相同方块
                for(y = 0; y < 4; y++)
                    for(x = 0; x < 4; ) {
                        if(a[y][x] == 0) {
                            x++;
                            continue;
                        } else {
                            for(i = x + 1; i < 4; i++) {
                                if(a[y][i] == 0) {
                                    continue;
                                }
                                else {
                                    if(a[y][x] == a[y][i]) {
                                        a[y][x] += a[y][i];
                                        a[y][i] = 0;
                                        x = i + 1;
                                        empty++;
                                        break;
                                    }
                                    else {
                                        x = i;
                                        break;
                                    }
                                }
                            }
                            x = i;
                        }
                    }
                //向左移动方块
                for(y = 0; y < 4; y++)
                    for(x = 0; x < 4; x++) {
                        if(a[y][x] == 0) {
                            continue;
                        } else {
                            for(i = x; (i > 0) && (a[y][i-1] == 0); i--) {
                                a[y][i-1] = a[y][i];
                                a[y][i] = 0;
                            move = 1;
                            }
                        }
                    }
                break;
            case 'D':
            case 'd':
                //从右向左消去相同方块
                for(y = 0; y < 4; y++)
                    for(x = 3; x >= 0; ) {
                        if(a[y][x] == 0) {
                            x--;
                            continue;
                        } else {
                            for(i = x - 1; i >= 0; i--) {
                                if(a[y][i] == 0) {
                                    continue;
                                } else if(a[y][x] == a[y][i]) {
                                    a[y][x] += a[y][i];
                                    a[y][i] = 0;
                                    x = i - 1;
                                    empty++;
                                    break;
                                } else {
                                    x = i;
                                    break;
                                }
                            }
                            x = i;
                        }
                    }
                //向右移动方块
                for(y = 0; y < 4; y++)
                    for(x = 3; x >= 0; x--) {
                        if(a[y][x] == 0) {
                            continue;
                        } else {
                            for(i = x; (i < 3) && (a[y][i+1] == 0); i++) {
                                a[y][i+1] = a[y][i];
                                a[y][i] = 0;
                            move = 1;
                            }
                        }
                    }
                break;
            case 'W':
            case 'w':
                //从上向下消去相同方块
                for(x = 0; x < 4; x++)
                    for(y = 0; y < 4; ) {
                        if(a[y][x] == 0) {
                            y++;
                            continue;
                        } else {
                            for(i = y + 1; i < 4; i++) {
                                if(a[i][x] == 0) {
                                    continue;
                                } else if(a[y][x] == a[i][x]) {
                                    a[y][x] += a[i][x];
                                    a[i][x] = 0;
                                    y = i + 1;
                                    empty++;
                                    break;
                                } else {
                                    y = i;
                                    break;
                                }
                            }
                            y = i;
                        }
                    }
                //向上移动方块
                for(x = 0; x < 4; x++)
                    for(y = 0; y < 4; y++) {
                        if(a[y][x] == 0) {
                            continue;
                        } else {
                            for(i = y; (i > 0) && (a[i-1][x] == 0); i--) {
                                a[i-1][x] = a[i][x];
                                a[i][x] = 0;
                            move = 1;
                            }
                        }
                    }
                break;
            case 'S':
            case 's':
                //从下向上消去相同方块
                for(x = 0; x < 4; x++)
                    for(y = 3; y >= 0; ) {
                        if(a[y][x] == 0) {
                            y--;
                            continue;
                        } else {
                            for(i = y - 1; i >= 0; i--) {
                                if(a[i][x] == 0) {
                                    continue;
                                } else if(a[y][x] == a[i][x]) {
                                    a[y][x] += a[i][x];
                                    a[i][x] = 0;
                                    y = i -1;
                                    empty++;
                                    break;
                                } else {
                                    y = i;
                                    break;
                                }
                            }
                            y = i;
                        }
                    }
                //向下移动方块
                for(x = 0; x < 4; x++)
                    for(y = 3; y >= 0; y--) {
                        if(a[y][x] == 0) {
                            continue;
                        } else {
                            for(i = y; (i < 3) && (a[i+1][x] == 0); i++) {
                                a[i+1][x] = a[i][x];
                                a[i][x] = 0;
                            move = 1;
                            }
                        }
                    }
                break;
            case 'Q':
            case 'q':
                game_over();
                break;
            default:
                continue;
                break;
        }

        if(empty <= 0)
            game_over();
        draw();
        //生成新方块
        if((empty != old_empty) || (move == 1)) {  //修复了不移动或消除方块也生成新方块的bug
            do {
                new_x = rand() % 4;
                new_y = rand() % 4;
            }while(a[new_y][new_x] != 0);


            do {
                tmp = rand() % 4;
            }while(tmp == 0 || tmp == 2);//生成1或3
            a[new_y][new_x] = tmp + 1;//插入2或4
            empty--;

            draw_one(new_y, new_x);//将生成的值显示
        }
    }
}

int game_over()
{
    sleep(1);
    endwin();
    exit(0);
}

建议在linux环境下编译,编译前请安装ncurses库,安装命令:sudo apt-get install libncurses5-dev。然后gcc编译执行
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值