C++#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <conio.h> // 用于_getch()函数
#include <iomanip> // 用于setw()
using namespace std;
const int SIZE = 4;
int score = 0;
// 初始化游戏板
vector<vector<int>> initializeBoard() {
vector<vector<int>> board(SIZE, vector<int>(SIZE, 0));
// 随机生成两个初始方块
board[rand() % SIZE][rand() % SIZE] = 2;
int r, c;
do {
r = rand() % SIZE;
c = rand() % SIZE;
} while (board[r][c] != 0);
board[r][c] = 2;
return board;
}
// 在空白位置添加新方块
void addNewTile(vector<vector<int>>& board) {
vector<pair<int, int>> emptyCells;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == 0) {
emptyCells.push_back(make_pair(i, j));
}
}
}
if (!emptyCells.empty()) {
int idx = rand() % emptyCells.size();
int value = (rand() % 10 == 0) ? 4 : 2; // 10%概率生成4
board[emptyCells[idx].first][emptyCells[idx].second] = value;
}
}
// 打印游戏板
void printBoard(const vector<vector<int>>& board) {
system("cls"); // 清屏
cout << "2048 Game - Score: " << score << "\n\n";
for (int i = 0; i < SIZE; i++) {
cout << "+------+------+------+------+\n";
for (int j = 0; j < SIZE; j++) {
cout << "| ";
if (board[i][j] > 0) {
cout << setw(4) << board[i][j] << " ";
} else {
cout << " ";
}
}
cout << "|\n";
}
cout << "+------+------+------+------+\n";
cout << "Controls: w(up) s(down) a(left) d(right) x(exit)\n";
}
// 向左移动
bool moveLeft(vector<vector<int>>& board) {
bool moved = false;
for (int i = 0; i < SIZE; i++) {
// 合并相同方块
for (int j = 0; j < SIZE - 1; j++) {
for (int k = j + 1; k < SIZE; k++) {
if (board[i][k] == 0) continue;
if (board[i][j] == 0) {
board[i][j] = board[i][k];
board[i][k] = 0;
moved = true;
j--; // 重新检查当前位置
break;
} else if (board[i][j] == board[i][k]) {
board[i][j] *= 2;
score += board[i][j];
board[i][k] = 0;
moved = true;
break;
} else {
break;
}
}
}
}
return moved;
}
// 旋转游戏板(用于复用向左移动逻辑)
void rotateBoard(vector<vector<int>>& board) {
vector<vector<int>> temp(SIZE, vector<int>(SIZE));
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
temp[j][SIZE - 1 - i] = board[i][j];
}
}
board = temp;
}
// 检查游戏是否结束
bool isGameOver(const vector<vector<int>>& board) {
// 检查是否有空格
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == 0) return false;
}
}
// 检查水平方向是否有可合并的
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE - 1; j++) {
if (board[i][j] == board[i][j + 1]) return false;
}
}
// 检查垂直方向是否有可合并的
for (int j = 0; j < SIZE; j++) {
for (int i = 0; i < SIZE - 1; i++) {
if (board[i][j] == board[i + 1][j]) return false;
}
}
return true;
}
int main() {
srand(time(0)); // 初始化随机种子
vector<vector<int>> board = initializeBoard();
bool gameOver = false;
while (!gameOver) {
printBoard(board);
// 检测键盘输入
char input = _getch();
bool moved = false;
switch (tolower(input)) {
case 'a': // 左
moved = moveLeft(board);
break;
case 'd': // 右
rotateBoard(board);
rotateBoard(board);
moved = moveLeft(board);
rotateBoard(board);
rotateBoard(board);
break;
case 'w': // 上
rotateBoard(board);
rotateBoard(board);
rotateBoard(board);
moved = moveLeft(board);
rotateBoard(board);
break;
case 's': // 下
rotateBoard(board);
moved = moveLeft(board);
rotateBoard(board);
rotateBoard(board);
rotateBoard(board);
break;
case 'x': // 退出
return 0;
default:
continue;
}
if (moved) {
addNewTile(board);
gameOver = isGameOver(board);
}
}
printBoard(board);
cout << "Game Over! Final Score: " << score << endl;
return 0;
}