利用C语言在挑战2048游戏中添加排行版代码
时间: 2024-03-07 14:49:06 浏览: 21
以下是一个简单的C语言代码,实现了在2048游戏中添加排行榜功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BOARD_SIZE 4
#define MAX_SCORES 10
// 游戏棋盘
int board[BOARD_SIZE][BOARD_SIZE] = {0};
// 分数和最高分
int score = 0;
int high_score = 0;
// 排行榜
struct score {
char name[20];
int score;
} high_scores[MAX_SCORES];
// 在空位置随机生成数字
void generate() {
int empty_cells[BOARD_SIZE * BOARD_SIZE][2];
int count = 0;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 0) {
empty_cells[count][0] = i;
empty_cells[count][1] = j;
count++;
}
}
}
if (count > 0) {
int index = rand() % count;
int i = empty_cells[index][0];
int j = empty_cells[index][1];
board[i][j] = (rand() % 2 + 1) * 2;
}
}
// 移动棋子
void move(char direction) {
int moved = 0;
if (direction == 'w' || direction == 'W') { // 上
for (int j = 0; j < BOARD_SIZE; j++) {
for (int i = 1; i < BOARD_SIZE; i++) {
if (board[i][j] != 0) {
int k = i;
while (k > 0 && board[k-1][j] == 0) {
k--;
moved = 1;
}
if (k != i) {
board[k][j] = board[i][j];
board[i][j] = 0;
}
if (k > 0 && board[k-1][j] == board[k][j]) {
board[k-1][j] *= 2;
score += board[k-1][j];
board[k][j] = 0;
moved = 1;
}
}
}
}
} else if (direction == 's' || direction == 'S') { // 下
for (int j = 0; j < BOARD_SIZE; j++) {
for (int i = BOARD_SIZE - 2; i >= 0; i--) {
if (board[i][j] != 0) {
int k = i;
while (k < BOARD_SIZE - 1 && board[k+1][j] == 0) {
k++;
moved = 1;
}
if (k != i) {
board[k][j] = board[i][j];
board[i][j] = 0;
}
if (k < BOARD_SIZE - 1 && board[k+1][j] == board[k][j]) {
board[k+1][j] *= 2;
score += board[k+1][j];
board[k][j] = 0;
moved = 1;
}
}
}
}
} else if (direction == 'a' || direction == 'A') { // 左
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 1; j < BOARD_SIZE; j++) {
if (board[i][j] != 0) {
int k = j;
while (k > 0 && board[i][k-1] == 0) {
k--;
moved = 1;
}
if (k != j) {
board[i][k] = board[i][j];
board[i][j] = 0;
}
if (k > 0 && board[i][k-1] == board[i][k]) {
board[i][k-1] *= 2;
score += board[i][k-1];
board[i][k] = 0;
moved = 1;
}
}
}
}
} else if (direction == 'd' || direction == 'D') { // 右
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = BOARD_SIZE - 2; j >= 0; j--) {
if (board[i][j] != 0) {
int k = j;
while (k < BOARD_SIZE - 1 && board[i][k+1] == 0) {
k++;
moved = 1;
}
if (k != j) {
board[i][k] = board[i][j];
board[i][j] = 0;
}
if (k < BOARD_SIZE - 1 && board[i][k+1] == board[i][k]) {
board[i][k+1] *= 2;
score += board[i][k+1];
board[i][k] = 0;
moved = 1;
}
}
}
}
}
if (moved) {
generate();
}
}
// 打印游戏棋盘
void print_board() {
printf("Score: %d\n", score);
printf("High score: %d\n", high_score);
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
printf("%5d", board[i][j]);
}
printf("\n");
}
}
// 保存排行榜到文件
void save_scores() {
FILE *fp = fopen("scores.txt", "w");
for (int i = 0; i < MAX_SCORES; i++) {
fprintf(fp, "%s %d\n", high_scores[i].name, high_scores[i].score);
}
fclose(fp);
}
// 读取排行榜文件
void load_scores() {
FILE *fp = fopen("scores.txt", "r");
if (fp == NULL) {
return;
}
for (int i = 0; i < MAX_SCORES; i++) {
fscanf(fp, "%s %d", high_scores[i].name, &high_scores[i].score);
}
fclose(fp);
}
// 更新排行榜
void update_scores() {
int index = MAX_SCORES;
for (int i = 0; i < MAX_SCORES; i++) {
if (score > high_scores[i].score) {
index = i;
break;
}
}
if (index < MAX_SCORES) {
for (int i = MAX_SCORES - 1; i > index; i--) {
high_scores[i] = high_scores[i - 1];
}
printf("Congratulations! You made it onto the leaderboard!\n");
printf("Please enter your name: ");
scanf("%s", high_scores[index].name);
high_scores[index].score = score;
save_scores();
}
}
// 打印排行榜
void print_scores() {
printf("Rank\tName\t\tScore\n");
for (int i = 0; i < MAX_SCORES; i++) {
printf("%d\t%s\t\t%d\n", i + 1, high_scores[i].name, high_scores[i].score);
}
}
int main() {
srand(time(NULL));
load_scores();
generate();
generate();
while (1) {
system("clear");
print_board();
char direction;
printf("Enter direction (WASD): ");
scanf("%c", &direction);
while (getchar() != '\n');
move(direction);
if (score > high_score) {
high_score = score;
}
if (score >= 2048) {
printf("Congratulations! You won!\n");
update_scores();
print_scores();
break;
}
int gameover = 1;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 0 ||
(i > 0 && board[i][j] == board[i-1][j]) ||
(j > 0 && board[i][j] == board[i][j-1]) ||
(i < BOARD_SIZE - 1 && board[i][j] == board[i+1][j]) ||
(j < BOARD_SIZE - 1 && board[i][j] == board[i][j+1])) {
gameover = 0;
break;
}
}
if (!gameover) {
break;
}
}
if (gameover) {
printf("Game over!\n");
update_scores();
print_scores();
break;
}
}
return 0;
}
```
以上代码实现了在2048游戏中添加排行榜功能,可以让玩家比较分数,增强游戏的趣味性和挑战性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)