编写一个C语言2048小游戏程序,带欢迎界面
时间: 2024-03-08 21:49:09 浏览: 24
好的,以下是一个简单的 C 语言 2048 小游戏程序,包括欢迎界面。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 4
#define WIN_SCORE 2048
#define MAX_SCORE 1000000
void print_board(int board[SIZE][SIZE]) {
printf("\033[2J"); // 清屏
printf("\033[1;1H"); // 光标移动到左上角
printf("2048 Game\n\n");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%6d", board[i][j]);
}
printf("\n\n");
}
}
void add_random_number(int board[SIZE][SIZE]) {
int empty_count = 0;
int empty_pos[SIZE * SIZE][2];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == 0) {
empty_pos[empty_count][0] = i;
empty_pos[empty_count][1] = j;
empty_count++;
}
}
}
if (empty_count == 0) {
return;
}
int index = rand() % empty_count;
int i = empty_pos[index][0];
int j = empty_pos[index][1];
board[i][j] = (rand() % 2 + 1) * 2;
}
int move_left(int board[SIZE][SIZE]) {
int score = 0;
for (int i = 0; i < SIZE; i++) {
int last_merged = -1;
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == 0) {
continue;
}
int k;
for (k = j + 1; k < SIZE && board[i][k] == 0; k++);
if (k == SIZE) {
k--;
}
if (k != j + 1 && board[i][j] == board[i][k] && last_merged != k) {
board[i][j] *= 2;
board[i][k] = 0;
score += board[i][j];
last_merged = j;
} else {
int temp = board[i][j];
board[i][j] = 0;
board[i][k - 1] = temp;
}
}
}
return score;
}
int move_right(int board[SIZE][SIZE]) {
int score = 0;
for (int i = 0; i < SIZE; i++) {
int last_merged = SIZE;
for (int j = SIZE - 1; j >= 0; j--) {
if (board[i][j] == 0) {
continue;
}
int k;
for (k = j - 1; k >= 0 && board[i][k] == 0; k--);
if (k == -1) {
k++;
}
if (k != j - 1 && board[i][j] == board[i][k] && last_merged != k) {
board[i][j] *= 2;
board[i][k] = 0;
score += board[i][j];
last_merged = j;
} else {
int temp = board[i][j];
board[i][j] = 0;
board[i][k + 1] = temp;
}
}
}
return score;
}
int move_up(int board[SIZE][SIZE]) {
int score = 0;
for (int j = 0; j < SIZE; j++) {
int last_merged = -1;
for (int i = 0; i < SIZE; i++) {
if (board[i][j] == 0) {
continue;
}
int k;
for (k = i + 1; k < SIZE && board[k][j] == 0; k++);
if (k == SIZE) {
k--;
}
if (k != i + 1 && board[i][j] == board[k][j] && last_merged != k) {
board[i][j] *= 2;
board[k][j] = 0;
score += board[i][j];
last_merged = i;
} else {
int temp = board[i][j];
board[i][j] = 0;
board[k - 1][j] = temp;
}
}
}
return score;
}
int move_down(int board[SIZE][SIZE]) {
int score = 0;
for (int j = 0; j < SIZE; j++) {
int last_merged = SIZE;
for (int i = SIZE - 1; i >= 0; i--) {
if (board[i][j] == 0) {
continue;
}
int k;
for (k = i - 1; k >= 0 && board[k][j] == 0; k--);
if (k == -1) {
k++;
}
if (k != i - 1 && board[i][j] == board[k][j] && last_merged != k) {
board[i][j] *= 2;
board[k][j] = 0;
score += board[i][j];
last_merged = i;
} else {
int temp = board[i][j];
board[i][j] = 0;
board[k + 1][j] = temp;
}
}
}
return score;
}
int get_score(int board[SIZE][SIZE]) {
int score = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
score += board[i][j];
}
}
return score;
}
int check_gameover(int board[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == 0) {
return 0;
}
if (i < SIZE - 1 && board[i][j] == board[i + 1][j]) {
return 0;
}
if (j < SIZE - 1 && board[i][j] == board[i][j + 1]) {
return 0;
}
}
}
return 1;
}
int main() {
srand(time(NULL));
int board[SIZE][SIZE] = { 0 };
int score = 0;
int max_score = 0;
int is_win = 0;
// 欢迎界面
printf("Welcome to 2048 Game!\n");
printf("Press any key to start.\n");
getchar();
while (1) {
add_random_number(board);
print_board(board);
if (check_gameover(board)) {
printf("Game Over!\n");
printf("Your score: %d\n", score);
printf("Max score: %d\n", max_score);
break;
}
int direction;
printf("Score: %d\n", score);
printf("Max score: %d\n", max_score);
printf("Input direction (1: left, 2: right, 3: up, 4: down): ");
scanf("%d", &direction);
int move_score = 0;
switch (direction) {
case 1:
move_score = move_left(board);
break;
case 2:
move_score = move_right(board);
break;
case 3:
move_score = move_up(board);
break;
case 4:
move_score = move_down(board);
break;
default:
printf("Invalid direction!\n");
continue;
}
score += move_score;
if (score > max_score) {
max_score = score;
}
if (!is_win && get_score(board) >= WIN_SCORE) {
printf("Congratulations! You win!\n");
is_win = 1;
}
}
return 0;
}
```
这个程序使用了 ANSI 转义序列来控制控制台输出,因此在 Windows 系统上可能无法正常工作。如果你使用的是 Windows 系统,可以将 `print_board` 函数中的 `"\033[2J"` 和 `"\033[1;1H"` 替换为 `system("cls")`。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)