-
个人简介
看其他人主页都这么卷了,我也就算了吧。 复制一个2048在此。
#include <iostream> #include <vector> #include <cstdlib> #include <ctime> #include <iomanip> #include <thread> #include <chrono> #include <algorithm> using namespace std; enum class Direction { LEFT, RIGHT, UP, DOWN }; class Game { private: vector<vector<int>> grid; int rows; int cols; int score; void transpose() { vector<vector<int>> transposed(cols, vector<int>(rows)); for (int i = 0; i < rows; ++i) for (int j = 0; j < cols; ++j) transposed[j][i] = grid[i][j]; grid = transposed; swap(rows, cols); } public: Game(int r, int c) : rows(r), cols(c), score(0) { grid.resize(rows, vector<int>(cols, 0)); } void addNewTile() { vector<pair<int, int>> emptyCells; for (int i = 0; i < rows; ++i) for (int j = 0; j < cols; ++j) if (grid[i][j] == 0) emptyCells.emplace_back(i, j); if (!emptyCells.empty()) { pair<int,int> a; a = emptyCells[rand() % emptyCells.size()]; grid[a.first][a.second] = (rand() % 10) < 9 ? 2 : 4; } } void printGrid() { system("cls"); for (auto& row : grid) { for (int val : row) cout << setw(4) << val << " "; cout << endl; } } bool moveLeft() { bool moved = false; for (auto& row : grid) { vector<int> newRow; for (int j = 0; j < cols;) { if (row[j] == 0) { j++; continue; } int current = row[j], next = j+1; while (next < cols && row[next] == 0) next++; if (next < cols && row[next] == current) { newRow.push_back(current*2); score += current*2; j = next+1; moved = true; } else { newRow.push_back(current); j = next; } } while (newRow.size() < cols) newRow.push_back(0); if (row != newRow) { row = newRow; moved = true; } } return moved; } bool moveRight() { reverseGrid(); bool moved = moveLeft(); reverseGrid(); return moved; } bool moveUp() { transpose(); bool moved = moveLeft(); transpose(); return moved; } bool moveDown() { transpose(); bool moved = moveRight(); transpose(); return moved; } void reverseGrid() { for (auto& row : grid) reverse(row.begin(), row.end()); } bool isGameOver() { for (int i = 0; i < rows; ++i) for (int j = 0; j < cols; ++j) { if (grid[i][j] == 0) return false; if (j < cols-1 && grid[i][j] == grid[i][j+1]) return false; if (i < rows-1 && grid[i][j] == grid[i+1][j]) return false; } return true; } int getScore() const { return score; } vector<vector<int>> getGrid() const { return grid; } int getRows() const { return rows; } int getCols() const { return cols; } }; class AI { static int evaluate(const Game& game) { int score = 0; auto grid = game.getGrid(); int rows = game.getRows(), cols = game.getCols(); // 空格子和合并可能性 int empty = 0, merges = 0, maxVal = 0; for (int i = 0; i < rows; ++i) for (int j = 0; j < cols; ++j) { if (grid[i][j] == 0) empty++; else { if (j < cols-1 && grid[i][j] == grid[i][j+1]) merges++; if (i < rows-1 && grid[i][j] == grid[i+1][j]) merges++; maxVal = max(maxVal, grid[i][j]); } } // 最大数位置 bool maxInCorner = false; for (int i = 0; i < rows; ++i) for (int j = 0; j < cols; ++j) if (grid[i][j] == maxVal) maxInCorner = (i == 0 || i == rows-1) && (j == 0 || j == cols-1); return empty*10 + merges*100 + (maxInCorner ? 500 : 0); } public: static Direction chooseMove(const Game& game) { pair<Direction, int> best = {Direction::LEFT, -1}; for (int dir = 0; dir < 4; dir++) { Game temp = game; bool moved = [&]{ switch(Direction(dir)) { case Direction::LEFT: return temp.moveLeft(); case Direction::RIGHT: return temp.moveRight(); case Direction::UP: return temp.moveUp(); case Direction::DOWN: return temp.moveDown(); } return false; }(); if (moved && evaluate(temp) > best.second) best = {Direction(dir), evaluate(temp)}; } return best.first; } }; int main() { int c; srand(time(0)); int rows, cols; cout << "Enter rows and cols: "; cin >> rows >> cols; Game game(rows, cols); game.addNewTile(); game.addNewTile(); bool aiMode; cout << "Manual (0) / AI (1): "; cin >> aiMode; while (!game.isGameOver()) { c=0; game.printGrid(); cout << "Score: " << game.getScore() << endl; if (aiMode) { Direction dir = AI::chooseMove(game); bool moved = [&]{ switch(dir) { case Direction::LEFT: return game.moveLeft(); case Direction::RIGHT: return game.moveRight(); case Direction::UP: return game.moveUp(); case Direction::DOWN: return game.moveDown(); } return false; }(); if (moved) game.addNewTile(); if (moved) game.addNewTile(); if (moved) game.addNewTile(); if (moved) game.addNewTile(); this_thread::sleep_for(chrono::milliseconds(500)); } else { string in; cout << "WASD to move: "; cin >> in; for(char input:in){ bool moved = [&]{ switch(toupper(input)) { case 'A': return game.moveLeft(); case 'D': return game.moveRight(); case 'W': return game.moveUp(); case 'S': return game.moveDown(); default: return false; } }(); if (moved) game.addNewTile(); } } } game.printGrid(); cout << "Game Over! Final Score: " << game.getScore() << endl; return 0; }
做的时候用的英文,也懒得翻译了,就摆在这了,要的自取。
绝对不是因为基本可以AC E才摆在这里的 -
通过的题目
-
最近活动
-
最近编写的题解
This person is lazy and didn't write any solutions.