Linux下的C之2048

时间:2021-11-01 11:00:57
 #include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <time.h>
#include <unistd.h>
#include <signal.h> // 4*4方格
int a[][] = {};
// 方格里空格的个数
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 ;
} void init()
{
int x, y; initscr();
cbreak();
noecho();
curs_set(); empty = ;
srand(time());
x = rand() % ;
y = rand() % ;
a[y][x] = ;
draw();
} void draw()
{
int n, m, x, y;
char c[] = {'', '', '', ''}; clear();
for(n = ; n < ; n += ) //横线
for(m = ; m < ; m++) {
move(n, m);
addch('-');
refresh();
}
for(m = ; m < ; m += ) //竖线
for(n = ; n < ; n++) {
move(n, m);
addch('|');
refresh();
}
for(y = ; y < ; y++) //数字
for(x = ; x < ; x++) {
draw_one(y, x);
}
} void draw_one(int y, int x)
{
int i, m, k, j;
char c[] = {'', '', '', ''}; i = a[y][x];
m = ;
do {
j = i % ;
c[m++] = j + '';
i = i / ;
}while(i > );
m = ;
k = (x + ) * - ;
while(c[m] != '') {
move(*y+, k);
addch(c[m++]);
k--;
}
} void play()
{
int x, y, i, new_x, new_y, tmp;
int old_empty, move;
char ch; while() {
move = ;
old_empty = empty;
//draw();
ch = getch();
switch(ch) {
case 'A':
case 'a':
//从左向右消去相同方块
for(y = ; y < ; y++)
for(x = ; x < ; ) {
if(a[y][x] == ) {
x++;
continue;
} else {
for(i = x + ; i < ; i++) {
if(a[y][i] == ) {
continue;
}
else {
if(a[y][x] == a[y][i]) {
a[y][x] += a[y][i];
a[y][i] = ;
x = i + ;
empty++;
break;
}
else {
x = i;
break;
}
}
}
x = i;
}
}
//向左移动方块
for(y = ; y < ; y++)
for(x = ; x < ; x++) {
if(a[y][x] == ) {
continue;
} else {
for(i = x; (i > ) && (a[y][i-] == ); i--) {
a[y][i-] = a[y][i];
a[y][i] = ;
move = ;
}
}
}
break;
case 'D':
case 'd':
//从右向左消去相同方块
for(y = ; y < ; y++)
for(x = ; x >= ; ) {
if(a[y][x] == ) {
x--;
continue;
} else {
for(i = x - ; i >= ; i--) {
if(a[y][i] == ) {
continue;
} else if(a[y][x] == a[y][i]) {
a[y][x] += a[y][i];
a[y][i] = ;
x = i - ;
empty++;
break;
} else {
x = i;
break;
}
}
x = i;
}
}
//向右移动方块
for(y = ; y < ; y++)
for(x = ; x >= ; x--) {
if(a[y][x] == ) {
continue;
} else {
for(i = x; (i < ) && (a[y][i+] == ); i++) {
a[y][i+] = a[y][i];
a[y][i] = ;
move = ;
}
}
}
break;
case 'W':
case 'w':
//从上向下消去相同方块
for(x = ; x < ; x++)
for(y = ; y < ; ) {
if(a[y][x] == ) {
y++;
continue;
} else {
for(i = y + ; i < ; i++) {
if(a[i][x] == ) {
continue;
} else if(a[y][x] == a[i][x]) {
a[y][x] += a[i][x];
a[i][x] = ;
y = i + ;
empty++;
break;
} else {
y = i;
break;
}
}
y = i;
}
}
//向上移动方块
for(x = ; x < ; x++)
for(y = ; y < ; y++) {
if(a[y][x] == ) {
continue;
} else {
for(i = y; (i > ) && (a[i-][x] == ); i--) {
a[i-][x] = a[i][x];
a[i][x] = ;
move = ;
}
}
}
break;
case 'S':
case 's':
//从下向上消去相同方块
for(x = ; x < ; x++)
for(y = ; y >= ; ) {
if(a[y][x] == ) {
y--;
continue;
} else {
for(i = y - ; i >= ; i--) {
if(a[i][x] == ) {
continue;
} else if(a[y][x] == a[i][x]) {
a[y][x] += a[i][x];
a[i][x] = ;
y = i -;
empty++;
break;
} else {
y = i;
break;
}
}
y = i;
}
}
//向下移动方块
for(x = ; x < ; x++)
for(y = ; y >= ; y--) {
if(a[y][x] == ) {
continue;
} else {
for(i = y; (i < ) && (a[i+][x] == ); i++) {
a[i+][x] = a[i][x];
a[i][x] = ;
move = ;
}
}
}
break;
case 'Q':
case 'q':
game_over();
break;
default:
continue;
break;
} if(empty <= )
game_over();
draw();
//生成新方块
if((empty != old_empty) || (move == )) { //修复了不移动或消除方块也生成新方块的bug
do {
new_x = rand() % ;
new_y = rand() % ;
}while(a[new_y][new_x] != ); cnt_value(&new_y, &new_x); do {
tmp = rand() % ;
}while(tmp == || tmp == );
a[new_y][new_x] = tmp + ;
empty--; draw_one(new_y, new_x);
}
}
} int cnt_one(int y, int x)
{
int value = ; if(y - > )
a[y-][x] ? : value++;
if(y + < )
a[y+][x] ? : value++;
if(x - >= )
a[y][x-] ? : value++;
if(x + < )
a[y][x+] ? : value++;
if(y - >= && x - >= )
a[y-][x-] ? : value++;
if(y - >= && x + < )
a[y-][x+] ? : value++;
if(y + < && x - >= )
a[y+][x-] ? : value++;
if(y + < && x + < )
a[y+][x+] ? : value++; return value;
} void cnt_value(int *new_y, int *new_x)
{
int max_x, max_y, x, y, value;
int max = ; max = cnt_one(*new_y, *new_x);
for(y = ; y < ; y++)
for(x = ; x < ; x++) {
if(!a[y][x]) {
value = cnt_one(y, x);
if(value > max && old_y != y && old_x != x) { //避免在同一位置反复出现新方块
*new_y = y;
*new_x = x;
old_x = x;
old_y = y;
break;
}
}
}
} int game_over()
{
sleep();
endwin();
exit();
}

vim game_2048.c,按下i进入编辑模式,然后将上面的代码复制进去,再按Esc退出编辑模式,按下:x保存并退出。

然后在终端运行:gcc game_2048.c -o 2048 -lcurses 进行编译,最后输入./2048即可运行2048。方向键是wasd。

效果截图:Linux下的C之2048