当前位置: 代码迷 >> 综合 >> 2048游戏C语言代码
  详细解决方案

2048游戏C语言代码

热度:5   发布时间:2024-01-21 00:01:25.0
#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)) {  //修复了不移动或消除方块也生成新方块的bugdo {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或3a[new_y][new_x] = tmp + 1;//插入2或4empty--;draw_one(new_y, new_x);//将生成的值显示}}
}int game_over()
{sleep(1);endwin();exit(0);
}

建议在linux环境下编译,编译前请安装ncurses库,安装命令:sudo apt-get install libncurses5-dev。然后gcc编译执行