//2048 #include <iostream> #include <cstdlib> #include <ctime> #include <conio.h> #include <iomanip> const int SIZE = 4; const int WIN_NUM = 2048; const int EMPTY = 0; int grid[SIZE][SIZE]; int score = 0; void initGame(); void printGrid(); bool generateRandomNumber(); bool moveUp(); bool moveDown(); bool moveLeft(); bool moveRight(); bool isGameOver(); bool mergeRow(int row[]); bool compressRow(int row[]); void swapRows(int row1[], int row2[]); int main() { srand(static_cast<unsigned int>(time(NULL))); initGame(); char input; bool gameOver = false; bool win = false; std::cout << "===== 2048 游戏 =====\n"; std::cout << "操作说明:W(上) S(下) A(左) D(右) Q(退出)\n\n"; while (!gameOver) { printGrid(); if (!win) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (grid[i][j] == WIN_NUM) { win = true; std::cout << "\n?? 恭喜!你合成了2048,游戏胜利!??\n"; std::cout << "继续游玩按任意键,退出按Q\n"; break; } } if (win) break; } } input = _getch(); input = tolower(input); bool moved = false; switch (input) { case 'w': moved = moveUp(); break; case 's': moved = moveDown(); break; case 'a': moved = moveLeft(); break; case 'd': moved = moveRight(); break; case 'q': gameOver = true; break; default: std::cout << "\n无效输入!请按W/S/A/D/Q\n"; } if (moved && !gameOver) { generateRandomNumber(); if (isGameOver()) { gameOver = true; printGrid(); std::cout << "\n?? 游戏结束!你的最终分数:" << score << "\n"; } } } std::cout << "\n感谢游玩!\n"; return 0; } void initGame() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { grid[i][j] = EMPTY; } } score = 0; generateRandomNumber(); generateRandomNumber(); } void printGrid() { system("cls"); std::cout << "===== 2048 游戏 ===== 分数:" << score << "\n"; std::cout << "操作:W(上) S(下) A(左) D(右) Q(退出)\n\n"; for (int i = 0; i < SIZE; i++) { std::cout << "+------+------+------+------+\n"; for (int j = 0; j < SIZE; j++) { std::cout << "|"; if (grid[i][j] == EMPTY) { std::cout << std::setw(6) << " "; } else { std::cout << std::setw(6) << grid[i][j]; } } std::cout << "|\n"; } std::cout << "+------+------+------+------+\n"; } bool generateRandomNumber() { int emptyCells[SIZE*SIZE][2]; int count = 0; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (grid[i][j] == EMPTY) { emptyCells[count][0] = i; emptyCells[count][1] = j; count++; } } } if (count == 0) return false; int idx = rand() % count; int row = emptyCells[idx][0]; int col = emptyCells[idx][1]; grid[row][col] = (rand() % 10 == 0) ? 4 : 2; return true; } bool moveUp() { bool moved = false; for (int col = 0; col < SIZE; col++) { int temp[SIZE]; for (int row = 0; row < SIZE; row++) { temp[row] = grid[row][col]; } bool compressed = compressRow(temp); bool merged = mergeRow(temp); compressRow(temp); for (int row = 0; row < SIZE; row++) { if (grid[row][col] != temp[row]) { moved = true; grid[row][col] = temp[row]; } } } return moved; } bool moveDown() { bool moved = false; for (int col = 0; col < SIZE; col++) { int temp[SIZE]; for (int row = 0; row < SIZE; row++) { temp[row] = grid[SIZE-1 - row][col]; } bool compressed = compressRow(temp); bool merged = mergeRow(temp); compressRow(temp); for (int row = 0; row < SIZE; row++) { int original = grid[SIZE-1 - row][col]; grid[SIZE-1 - row][col] = temp[row]; if (original != temp[row]) { moved = true; } } } return moved; } bool moveLeft() { bool moved = false; for (int row = 0; row < SIZE; row++) { int temp[SIZE]; for (int col = 0; col < SIZE; col++) { temp[col] = grid[row][col]; } bool compressed = compressRow(temp); bool merged = mergeRow(temp); compressRow(temp); for (int col = 0; col < SIZE; col++) { if (grid[row][col] != temp[col]) { moved = true; grid[row][col] = temp[col]; } } } return moved; } bool moveRight() { bool moved = false; for (int row = 0; row < SIZE; row++) { int temp[SIZE]; for (int col = 0; col < SIZE; col++) { temp[col] = grid[row][SIZE-1 - col]; } bool compressed = compressRow(temp); bool merged = mergeRow(temp); compressRow(temp); for (int col = 0; col < SIZE; col++) { int original = grid[row][SIZE-1 - col]; grid[row][SIZE-1 - col] = temp[col]; if (original != temp[col]) { moved = true; } } } return moved; } bool compressRow(int row[]) { bool compressed = false; int temp[SIZE] = {EMPTY}; int idx = 0; for (int i = 0; i < SIZE; i++) { if (row[i] != EMPTY) { temp[idx++] = row[i]; if (i != idx-1) { compressed = true; } } } for (int i = 0; i < SIZE; i++) { row[i] = temp[i]; } return compressed; } bool mergeRow(int row[]) { bool merged = false; for (int i = 0; i < SIZE-1; i++) { if (row[i] != EMPTY && row[i] == row[i+1]) { row[i] *= 2; row[i+1] = EMPTY; score += row[i]; merged = true; i++; } } return merged; } bool isGameOver() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (grid[i][j] == EMPTY) { return false; } } } for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE-1; j++) { if (grid[i][j] == grid[i][j+1]) { return false; } } } for (int j = 0; j < SIZE; j++) { for (int i = 0; i < SIZE-1; i++) { if (grid[i][j] == grid[i+1][j]) { return false; } } } return true; } void swapRows(int row1[], int row2[]) { for (int i = 0; i < SIZE; i++) { int temp = row1[i]; row1[i] = row2[i]; row2[i] = temp; } }
Note.ms
/tful