• 个人简介

    #include <vector> #include <ctime> #include <cstdlib>

    using namespace std;

    const int BOARD_SIZE = 10; const int NUM_MINES = 15;

    vector<vector<char>> board(BOARD_SIZE, vector<char>(BOARD_SIZE, ' ')); vector<vector<bool>> revealed(BOARD_SIZE, vector<bool>(BOARD_SIZE, false)); vector<pair<int, int>> mines;

    void InitializeBoard() { // 初始化扫雷板 for (int i = 0; i < NUM_MINES; i++) { int x, y; do { x = rand() % BOARD_SIZE; y = rand() % BOARD_SIZE; } while (board[y][x] == ''); board[y][x] = ''; mines.push_back({x, y}); }

    // 计算邻近的雷数
    for (int y = 0; y < BOARD_SIZE; y++) {
        for (int x = 0; x < BOARD_SIZE; x++) {
            if (board[y][x] == ' ') {
                int count = 0;
                for (int dx = -1; dx <= 1; dx++) {
                    for (int dy = -1; dy <= 1; dy++) {
                        int nx = x + dx;
                        int ny = y + dy;
                        if (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE && board[ny][nx] == '*') {
                            count++;
                        }
                    }
                }
                if (count > 0) {
                    board[y][x] = count + '0';
                }
            }
        }
    }
    

    }

    void DisplayBoard(bool showMines) { system("cls"); cout << " "; for (int x = 0; x < BOARD_SIZE; x++) { cout << " " << x; } cout << "\n"; for (int y = 0; y < BOARD_SIZE; y++) { cout << y << " "; for (int x = 0; x < BOARD_SIZE; x++) { if (showMines && board[y][x] == '') { cout << " "; } else if (revealed[y][x]) { cout << board[y][x] << " "; } else { cout << ". "; } } cout << "\n"; } }

    bool IsGameWon() { int totalRevealed = 0; for (int y = 0; y < BOARD_SIZE; y++) { for (int x = 0; x < BOARD_SIZE; x++) { if (revealed[y][x]) { totalRevealed++; } } } return totalRevealed == BOARD_SIZE * BOARD_SIZE - NUM_MINES; }

    void RevealCell(int x, int y) { if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE || revealed[y][x]) { return; }

    revealed[y][x] = true;
    if (board[y][x] == ' ') {
        for (int dx = -1; dx <= 1; dx++) {
            for (int dy = -1; dy <= 1; dy++) {
                int nx = x + dx;
                int ny = y + dy;
                RevealCell(nx, ny);
            }
        }
    }
    

    }

    int main() { srand(time(0)); InitializeBoard(); DisplayBoard(false);

    while (true) {
        int x, y;
        cout << "输入坐标 (x y): ";
        cin >> x >> y;
    
        if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE || revealed[y][x]) {
            cout << "无效的坐标,请重新输入。\n";
            continue;
        }
    
        if (board[y][x] == '*') {
            DisplayBoard(true);
            cout << "游戏结束,你踩到雷了!" << endl;
            break;
        }
    
        RevealCell(x, y);
        DisplayBoard(false);
    
        if (IsGameWon()) {
            cout << "恭喜,你赢了!" << endl;
            break;
        }
    }
    
    return 0;
    

    } ———lvzhuojin

  • 通过的题目

  • 最近活动

  • 最近编写的题解

题目标签

1级1段
8
1级3段
4
运算符和表达式
3
输入输出
2
T1
2
1级2段
1
其他
1
数学
1