2048小游戏c语言完整版
时间: 2023-07-15 11:09:13 浏览: 30
以下是一个简单的C语言实现的2048小游戏的完整版代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define SIZE 4
int board[SIZE][SIZE];
// 初始化游戏棋盘
void init_board() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = 0;
}
}
}
// 在随机位置生成一个2或4
void generate_number() {
int empty_count = 0;
int empty_cells[SIZE * SIZE][2];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == 0) {
empty_cells[empty_count][0] = i;
empty_cells[empty_count][1] = j;
empty_count++;
}
}
}
if (empty_count > 0) {
srand(time(NULL));
int random_index = rand() % empty_count;
int random_value = (rand() % 2 + 1) * 2;
int row = empty_cells[random_index][0];
int col = empty_cells[random_index][1];
board[row][col] = random_value;
}
}
// 绘制游戏棋盘
void draw_board() {
system("cls");
printf("2048 Game\n");
printf("-----------------------------\n");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == 0) {
printf("| ");
} else {
printf("|%4d", board[i][j]);
}
}
printf("|\n");
printf("-----------------------------\n");
}
}
// 检查游戏是否结束
int is_game_over() {
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;
}
// 执行向上移动操作
void move_up() {
for (int j = 0; j < SIZE; j++) {
int current_row = 0;
int current_col = j;
while (current_row < SIZE - 1) {
int next_row = current_row + 1;
if (board[next_row][current_col] == 0) {
current_row = next_row;
} else if (board[next_row][current_col] == board[current_row][current_col]) {
board[current_row][current_col] *= 2;
board[next_row][current_col] = 0;
current_row = next_row;
} else {
break;
}
}
}
}
// 执行向下移动操作
void move_down() {
for (int j = 0; j < SIZE; j++) {
int current_row = SIZE - 1;
int current_col = j;
while (current_row > 0) {
int next_row = current_row - 1;
if (board[next_row][current_col] == 0) {
current_row = next_row;
} else if (board[next_row][current_col] == board[current_row][current_col]) {
board[current_row][current_col] *= 2;
board[next_row][current_col] = 0;
current_row = next_row;
} else {
break;
}
}
}
}
// 执行向左移动操作
void move_left() {
for (int i = 0; i < SIZE; i++) {
int current_row = i;
int current_col = 0;
while (current_col < SIZE - 1) {
int next_col = current_col + 1;
if (board[current_row][next_col] == 0) {
current_col = next_col;
} else if (board[current_row][next_col] == board[current_row][current_col]) {
board[current_row][current_col] *= 2;
board[current_row][next_col] = 0;
current_col = next_col;
} else {
break;
}
}
}
}
// 执行向右移动操作
void move_right() {
for (int i = 0; i < SIZE; i++) {
int current_row = i;
int current_col = SIZE - 1;
while (current_col > 0) {
int next_col = current_col - 1;
if (board[current_row][next_col] == 0) {
current_col = next_col;
} else if (board[current_row][next_col] == board[current_row][current_col]) {
board[current_row][current_col] *= 2;
board[current_row][next_col] = 0;
current_col = next_col;
} else {
break;
}
}
}
}
int main() {
init_board();
generate_number();
generate_number();
draw_board();
while (!is_game_over()) {
char move = getch();
switch (move) {
case 'w':
move_up();
break;
case 's':
move_down();
break;
case 'a':
move_left();
break;
case 'd':
move_right();
break;
default:
continue;
}
generate_number();
draw_board();
}
printf("Game Over!\n");
return 0;
}
```
这是一个简单的控制台游戏,使用WASD键来移动方块。希望这个示例能帮到你!
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)