#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <limits>
#include <iomanip>
using namespace std;
const int BOARD_SIZE = 4; // 游戏板的大小
int score = 0; // 游戏得分
// 定义游戏板
vector<vector<int>> board(BOARD_SIZE, vector<int>(BOARD_SIZE, 0));
// 初始化游戏板,添加两个随机数字
void initBoard() {
srand(time(NULL)); // 仅在这里初始化随机数种子
for (int i = 0; i < 2; ++i) {
vector<pair<int, int>> emptyCells;
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
if (board[i][j] == 0) {
emptyCells.push_back(make_pair(i, j));
}
}
}
if (!emptyCells.empty()) {
int index = rand() % emptyCells.size();
pair<int, int> pos = emptyCells[index];
board[pos.first][pos.second] = (rand() % 10 == 0) ? 4 : 2;
}
}
}
// 打印棋盘状态
void printBoard() {
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
cout << board[i][j] << "\t";
}
cout << endl;
}
cout << "Score: " << score << endl;
}
// 棋盘向左移动
bool moveLeft() {
bool moved = false;
for (int i = 0; i < BOARD_SIZE; ++i) {
vector<int> tempRow;
int mergeIndex = 0;
// 移动非零元素至左侧
for (int j = 0; j < BOARD_SIZE; ++j) {
if (board[i][j] != 0) {
tempRow.push_back(board[i][j]);
}
}
// 合并相同的元素
for (size_t j = 0; j < tempRow.size(); ++j) {
if (mergeIndex < j && tempRow[mergeIndex] == tempRow[j]) {
tempRow[mergeIndex] *= 2;
score += tempRow[mergeIndex];
tempRow.erase(tempRow.begin() + j);
--j;
++mergeIndex;
moved = true;
} else if (tempRow[mergeIndex] != tempRow[j]) {
++mergeIndex;
}
}
// 将合并后的结果放回原位置
for (int j = 0; j < BOARD_SIZE; ++j) {
if (j < tempRow.size()) {
board[i][j] = tempRow[j];
} else {
board[i][j] = 0;
}
}
}
return moved;
}
// 棋盘向右移动
bool moveRight() {
bool moved = false;
for (int i = 0; i < BOARD_SIZE; ++i) {
vector<int> tempRow;
int mergeIndex = 0;
// 移动非零元素至右侧
for (int j = BOARD_SIZE - 1; j >= 0; --j) {
if (board[i][j] != 0) {
tempRow.insert(tempRow.begin(), board[i][j]);
}
}
// 合并相同的元素
for (size_t j = 0; j < tempRow.size(); ++j) {
if (mergeIndex < j && tempRow[mergeIndex] == tempRow[j]) {
tempRow[mergeIndex] *= 2;
score += tempRow[mergeIndex];
tempRow.erase(tempRow.begin() + j);
--j;
++mergeIndex;
moved = true;
} else if (tempRow[mergeIndex] != tempRow[j]) {
++mergeIndex;
}
}
// 将合并后的结果放回原位置
for (int j = BOARD_SIZE - 1; j >= 0; --j) {
if (j < BOARD_SIZE - tempRow.size()) {
board[i][j] = 0;
} else {
board[i][j] = tempRow[BOARD_SIZE - 1 - j];
}
}
}
return moved;
}
// 棋盘向上移动
bool moveUp() {
bool moved = false;
for (int j = 0; j < BOARD_SIZE; ++j) {
vector<int> tempCol;
int mergeIndex = 0;
// 移动非零元素至上侧
for (int i = 0; i < BOARD_SIZE; ++i) {
if (board[i][j] != 0) {
tempCol.push_back(board[i][j]);
}
}
// 合并相同的元素
for (size_t i = 0; i < tempCol.size(); ++i) {
if (mergeIndex < i && tempCol[mergeIndex] == tempCol[i]) {
tempCol[mergeIndex] *= 2;
score += tempCol[mergeIndex];
tempCol.erase(tempCol.begin() + i);
--i;
++mergeIndex;
moved = true;
} else if (tempCol[mergeIndex] != tempCol[i]) {
++mergeIndex;
}
}
// 将合并后的结果放回原位置
for (int i = 0; i < BOARD_SIZE; ++i) {
if (i < tempCol.size()) {
board[i][j] = tempCol[i];
} else {
board[i][j] = 0;
}
}
}
return moved;
}
// 棋盘向下移动
bool moveDown() {
bool moved = false;
for (int j = 0; j < BOARD_SIZE; ++j) {
vector<int> tempCol;
int mergeIndex = 0;
// 移动非零元素至下侧
for (int i = BOARD_SIZE - 1; i >= 0; --i) {
if (board[i][j] != 0) {
tempCol.insert(tempCol.begin(), board[i][j]);
}
}
// 合并相同的元素
for (size_t i = 0; i < tempCol.size(); ++i) {
if (mergeIndex < i && tempCol[mergeIndex] == tempCol[i]) {
tempCol[mergeIndex] *= 2;
score += tempCol[mergeIndex];
tempCol.erase(tempCol.begin() + i);
--i;
++mergeIndex;
moved = true;
} else if (tempCol[mergeIndex] != tempCol[i]) {
++mergeIndex;
}
}
// 将合并后的结果放回原位置
for (int i = BOARD_SIZE - 1; i >= 0; --i) {
if (i < BOARD_SIZE - tempCol.size()) {
board[i][j] = 0;
} else {
board[i][j] = tempCol[BOARD_SIZE - 1 - i];
}
}
}
return moved;
}
// 检查游戏是否结束
bool isGameOver() {
// 检查是否有空位
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
if (board[i][j] == 0) {
return false;
}
}
}
// 检查是否有相邻的相同数字
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] || (j < BOARD_SIZE - 1 && board[j][i] == board[j + 1][i])) {
return false;
}
}
}
return true;
}
// 主函数
int main() {
initBoard();
bool gameEnded = false;
while (!gameEnded) {
printBoard();
cout << "Enter your move (w/a/s/d): ";
char move;
cin >> move;
switch (move) {
case 'a':
moveLeft();
break;
case 'd':
moveRight();
break;
case 'w':
moveUp();
break;
case 's':
moveDown();
break;
default:
cout << "Invalid input. Try again." << endl;
continue;
}
// 确保在每次有效移动后添加一个新的随机数字
vector<pair<int, int>> emptyCells;
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
if (board[i][j] == 0) {
emptyCells.push_back(make_pair(i, j));
}
}
}
if (!emptyCells.empty()) {
int index = rand() % emptyCells.size();
pair<int, int> pos = emptyCells[index];
board[pos.first][pos.second] = (rand() % 10 == 0) ? 4 : 2;
}
gameEnded = isGameOver();
}
printBoard();
cout << "Game Over! Your score is: " << score << endl;
return 0;
}
过了样例就算队(test4)
最新推荐文章于 2024-08-02 16:46:49 发布