c语言小游戏代码2048
时间: 2023-07-30 21:06:56 浏览: 112
以下是一个简单的 C 语言实现 2048 小游戏的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 4
#define TARGET 2048
int board[SIZE][SIZE];
void init_board() {
int i, j;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
board[i][j] = 0;
}
}
}
void print_board() {
int i, j;
printf("-----------------------------\n");
for (i = 0; i < SIZE; i++) {
printf("|");
for (j = 0; j < SIZE; j++) {
if (board[i][j] == 0) {
printf(" |");
} else {
printf("%6d|", board[i][j]);
}
}
printf("\n-----------------------------\n");
}
}
void place_random_tile() {
int i, j;
int empty_spots[SIZE * SIZE][2];
int num_empty_spots = 0;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (board[i][j] == 0) {
empty_spots[num_empty_spots][0] = i;
empty_spots[num_empty_spots][1] = j;
num_empty_spots++;
}
}
}
if (num_empty_spots == 0) {
return;
}
int index = rand() % num_empty_spots;
int value = (rand() % 10 == 0) ? 4 : 2;
board[empty_spots[index][0]][empty_spots[index][1]] = value;
}
int slide_row(int row[]) {
int i, j;
int num_changes = 0;
for (i = 0; i < SIZE - 1; i++) {
if (row[i] == 0) {
for (j = i + 1; j < SIZE; j++) {
if (row[j] != 0) {
row[i] = row[j];
row[j] = 0;
num_changes++;
break;
}
}
}
for (j = i + 1; j < SIZE; j++) {
if (row[j] == row[i]) {
row[i] *= 2;
row[j] = 0;
num_changes++;
break;
} else if (row[j] != 0) {
break;
}
}
}
return num_changes;
}
int slide_board(int direction) {
int i, j;
int num_changes = 0;
switch (direction) {
case 0: // Slide left
for (i = 0; i < SIZE; i++) {
num_changes += slide_row(board[i]);
}
break;
case 1: // Slide up
for (i = 0; i < SIZE; i++) {
int row[SIZE];
for (j = 0; j < SIZE; j++) {
row[j] = board[j][i];
}
num_changes += slide_row(row);
for (j = 0; j < SIZE; j++) {
board[j][i] = row[j];
}
}
break;
case 2: // Slide right
for (i = 0; i < SIZE; i++) {
int row[SIZE];
for (j = 0; j < SIZE; j++) {
row[j] = board[i][SIZE - 1 - j];
}
num_changes += slide_row(row);
for (j = 0; j < SIZE; j++) {
board[i][SIZE - 1 - j] = row[j];
}
}
break;
case 3: // Slide down
for (i = 0; i < SIZE; i++) {
int row[SIZE];
for (j = 0; j < SIZE; j++) {
row[j] = board[SIZE - 1 - j][i];
}
num_changes += slide_row(row);
for (j = 0; j < SIZE; j++) {
board[SIZE - 1 - j][i] = row[j];
}
}
break;
}
return num_changes;
}
int game_over() {
int i, j;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (board[i][j] == 0) {
return 0;
} else if (board[i][j] == TARGET) {
return 1;
}
}
}
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE - 1; j++) {
if (board[i][j] == board[i][j + 1]) {
return 0;
}
if (board[j][i] == board[j + 1][i]) {
return 0;
}
}
}
return 1;
}
int main() {
srand(time(NULL));
init_board();
place_random_tile();
place_random_tile();
print_board();
while (!game_over()) {
int direction;
printf("Enter direction (0 = left, 1 = up, 2 = right, 3 = down): ");
scanf("%d", &direction);
int num_changes = slide_board(direction);
if (num_changes > 0) {
place_random_tile();
}
print_board();
}
printf("Game over!\n");
return 0;
}
```
这段代码使用了二维数组来表示游戏面板,其中 `board[i][j]` 表示第 i 行第 j 列的格子上的数字。游戏的主要逻辑在 `slide_row` 和 `slide_board` 函数中,它们用于在一个行或列上执行滑动操作,并返回滑动时数字发生的变化次数。在主循环中,程序等待用户输入一个方向并执行相应的滑动操作,直到游戏结束为止。在每次滑动后,程序会随机在空格子上放置一个新的数字,并打印出当前的游戏面板。游戏结束时,程序会输出 "Game over!"。
阅读全文