linux中C语言2048
时间: 2024-01-06 16:51:05 浏览: 76
以下是一个简单的 C 语言 2048 游戏的实现,运行在 Linux 系统中:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <termios.h>
#include <unistd.h>
#define ROWS 4
#define COLS 4
// 取消终端回显并设置非标准模式
void init_termios(struct termios *oldt) {
struct termios newt;
tcgetattr(STDIN_FILENO, oldt);
newt = *oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
}
// 恢复终端设置
void reset_termios(struct termios *oldt) {
tcsetattr(STDIN_FILENO, TCSANOW, oldt);
}
// 获取用户输入的方向键
int get_direction() {
int c;
c = getchar();
if (c != '\033') {
return c;
}
c = getchar();
if (c != '[') {
return c;
}
c = getchar();
switch (c) {
case 'A':
return 'w'; // 上
case 'B':
return 's'; // 下
case 'C':
return 'd'; // 右
case 'D':
return 'a'; // 左
default:
return c;
}
}
// 打印棋盘
void print_board(int board[ROWS][COLS]) {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("%d\t", board[i][j]);
}
printf("\n");
}
}
// 生成一个随机数
void generate_random(int board[ROWS][COLS]) {
int i, j;
while (1) {
i = rand() % ROWS;
j = rand() % COLS;
if (board[i][j] == 0) {
board[i][j] = rand() % 2 ? 2 : 4;
break;
}
}
}
// 检查棋盘是否还有空位
int check_spaces(int board[ROWS][COLS]) {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (board[i][j] == 0) {
return 1;
}
}
}
return 0;
}
// 检查是否可以移动
int check_move(int board[ROWS][COLS], int direction) {
int i, j;
switch (direction) {
case 'w':
for (i = 0; i < ROWS - 1; i++) {
for (j = 0; j < COLS; j++) {
if (board[i][j] != 0 && board[i+1][j] == 0) {
return 1;
}
if (board[i][j] != 0 && board[i][j] == board[i+1][j]) {
return 1;
}
}
}
break;
case 's':
for (i = ROWS - 1; i > 0; i--) {
for (j = 0; j < COLS; j++) {
if (board[i][j] != 0 && board[i-1][j] == 0) {
return 1;
}
if (board[i][j] != 0 && board[i][j] == board[i-1][j]) {
return 1;
}
}
}
break;
case 'a':
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS - 1; j++) {
if (board[i][j] != 0 && board[i][j+1] == 0) {
return 1;
}
if (board[i][j] != 0 && board[i][j] == board[i][j+1]) {
return 1;
}
}
}
break;
case 'd':
for (i = 0; i < ROWS; i++) {
for (j = COLS - 1; j > 0; j--) {
if (board[i][j] != 0 && board[i][j-1] == 0) {
return 1;
}
if (board[i][j] != 0 && board[i][j] == board[i][j-1]) {
return 1;
}
}
}
break;
}
return 0;
}
// 移动棋子
void move(int board[ROWS][COLS], int direction) {
int i, j, k;
switch (direction) {
case 'w':
for (j = 0; j < COLS; j++) {
for (i = 0; i < ROWS - 1; i++) {
if (board[i][j] == 0) {
continue;
}
for (k = i + 1; k < ROWS; k++) {
if (board[k][j] == 0) {
continue;
}
if (board[i][j] == board[k][j]) {
board[i][j] *= 2;
board[k][j] = 0;
}
break;
}
}
}
for (j = 0; j < COLS; j++) {
for (i = 0; i < ROWS - 1; i++) {
if (board[i][j] == 0) {
for (k = i + 1; k < ROWS; k++) {
if (board[k][j] != 0) {
board[i][j] = board[k][j];
board[k][j] = 0;
break;
}
}
}
}
}
break;
case 's':
for (j = 0; j < COLS; j++) {
for (i = ROWS - 1; i > 0; i--) {
if (board[i][j] == 0) {
continue;
}
for (k = i - 1; k >= 0; k--) {
if (board[k][j] == 0) {
continue;
}
if (board[i][j] == board[k][j]) {
board[i][j] *= 2;
board[k][j] = 0;
}
break;
}
}
}
for (j = 0; j < COLS; j++) {
for (i = ROWS - 1; i > 0; i--) {
if (board[i][j] == 0) {
for (k = i - 1; k >= 0; k--) {
if (board[k][j] != 0) {
board[i][j] = board[k][j];
board[k][j] = 0;
break;
}
}
}
}
}
break;
case 'a':
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS - 1; j++) {
if (board[i][j] == 0) {
continue;
}
for (k = j + 1; k < COLS; k++) {
if (board[i][k] == 0) {
continue;
}
if (board[i][j] == board[i][k]) {
board[i][j] *= 2;
board[i][k] = 0;
}
break;
}
}
}
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS - 1; j++) {
if (board[i][j] == 0) {
for (k = j + 1; k < COLS; k++) {
if (board[i][k] != 0) {
board[i][j] = board[i][k];
board[i][k] = 0;
break;
}
}
}
}
}
break;
case 'd':
for (i = 0; i < ROWS; i++) {
for (j = COLS - 1; j > 0; j--) {
if (board[i][j] == 0) {
continue;
}
for (k = j - 1; k >= 0; k--) {
if (board[i][k] == 0) {
continue;
}
if (board[i][j] == board[i][k]) {
board[i][j] *= 2;
board[i][k] = 0;
}
break;
}
}
}
for (i = 0; i < ROWS; i++) {
for (j = COLS - 1; j > 0; j--) {
if (board[i][j] == 0) {
for (k = j - 1; k >= 0; k--) {
if (board[i][k] != 0) {
board[i][j] = board[i][k];
board[i][k] = 0;
break;
}
}
}
}
}
break;
}
}
int main() {
struct termios oldt;
int board[ROWS][COLS] = {0};
int direction;
int score = 0;
int i, j;
// 初始化终端设置
init_termios(&oldt);
// 生成两个随机数
srand((unsigned)time(NULL));
generate_random(board);
generate_random(board);
// 游戏循环
while (1) {
system("clear");
printf("Score: %d\n\n", score);
print_board(board);
if (!check_spaces(board) && !check_move(board, 'w') && !check_move(board, 's')
&& !check_move(board, 'a') && !check_move(board, 'd')) {
printf("\nGame Over!\n");
break;
}
direction = get_direction();
if (direction == 'w' || direction == 's' || direction == 'a' || direction == 'd') {
move(board, direction);
if (check_spaces(board)) {
generate_random(board);
}
}
}
// 恢复终端设置
reset_termios(&oldt);
return 0;
}
```
使用指令 `gcc filename.c -o game` 进行编译,使用指令 `./game` 运行游戏。可以使用 `w`、`s`、`a`、`d` 分别控制棋子向上、下、左、右移动。
阅读全文