2048c++
时间: 2023-06-29 07:05:00 浏览: 58
以下是一个简单的 2048 游戏的 C++ 实现,使用了二维数组来存储游戏面板:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int BOARD_SIZE = 4;
int board[BOARD_SIZE][BOARD_SIZE];
// 初始化游戏面板
void init_board() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = 0;
}
}
}
// 在空白位置随机生成一个新数
void generate_new_number() {
srand(time(NULL));
int i, j;
do {
i = rand() % BOARD_SIZE;
j = rand() % BOARD_SIZE;
} while (board[i][j] != 0);
board[i][j] = (rand() % 2 + 1) * 2;
}
// 检查游戏是否结束
bool is_game_over() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 0) {
return false;
}
if (i < BOARD_SIZE - 1 && board[i][j] == board[i + 1][j]) {
return false;
}
if (j < BOARD_SIZE - 1 && board[i][j] == board[i][j + 1]) {
return false;
}
}
}
return true;
}
// 合并相邻的相同数
void merge_numbers() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE - 1; j++) {
if (board[i][j] == board[i][j + 1]) {
board[i][j] *= 2;
board[i][j + 1] = 0;
}
}
}
}
// 把所有数往左移动
void move_left() {
for (int i = 0; i < BOARD_SIZE; i++) {
int last_non_zero = -1;
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] != 0) {
if (last_non_zero != -1 && board[i][last_non_zero] == board[i][j]) {
board[i][last_non_zero] *= 2;
board[i][j] = 0;
last_non_zero = -1;
} else {
if (j != last_non_zero + 1) {
board[i][last_non_zero + 1] = board[i][j];
board[i][j] = 0;
}
last_non_zero++;
}
}
}
}
}
// 打印游戏面板
void print_board() {
cout << "---------------------" << endl;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
cout << board[i][j] << "\t";
}
cout << endl;
}
cout << "---------------------" << endl;
}
int main() {
init_board();
generate_new_number();
generate_new_number();
print_board();
while (!is_game_over()) {
char c;
cin >> c;
switch (c) {
case 'w':
// 上移
break;
case 's':
// 下移
break;
case 'a':
// 左移
move_left();
generate_new_number();
break;
case 'd':
// 右移
break;
}
print_board();
}
cout << "Game over." << endl;
return 0;
}
```
这是一个非常简单的实现,只包含了左移操作和生成新数的功能,你可以根据需要添加其他功能。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.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)