AI-c++小游戏-扫雷

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

using namespace std;

// 扫雷格子的状态
enum class CellState {
    Hidden, // 未翻开
    Revealed, // 已翻开
    Flagged // 已标记为地雷
};

// 扫雷格子
struct Cell {
    bool hasMine; // 是否是地雷
    int neighborMines; // 相邻格子的地雷数量
    CellState state; // 格子的状态

    Cell() : hasMine(false), neighborMines(0), state(CellState::Hidden) {}
};

class Minesweeper {
private:
    int rows; // 行数
    int cols; // 列数
    int numMines; // 地雷数量
    vector<vector<Cell>> board; // 扫雷板

public:
    Minesweeper(int r, int c, int n) : rows(r), cols(c), numMines(n) {
        // 初始化扫雷板
        board.resize(rows, vector<Cell>(cols));

        // 随机布雷
        srand(time(0));
        int count = 0;
        while (count < numMines) {
            int r = rand() % rows;
            int c = rand() % cols;
            if (!board[r][c].hasMine) {
                board[r][c].hasMine = true;
                count++;
            }
        }

        // 计算每个格子的相邻地雷数量
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                if (!board[r][c].hasMine) {
                    int count = 0;
                    for (int dr = -1; dr <= 1; dr++) {
                        for (int dc = -1; dc <= 1; dc++) {
                            int nr = r + dr;
                            int nc = c + dc;
                            if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && board[nr][nc].hasMine) {
                                count++;
                            }
                        }
                    }
                    board[r][c].neighborMines = count;
                }
            }
        }
    }

    void play() {
        while (true) {
            printBoard();

            int row, col;
            cout << "请输入要翻开的格子的位置(行 列):";
            cin >> row >> col;

            // 检查输入合法性
            if (row < 0 || row >= rows || col < 0 || col >= cols) {
                cout << "输入位置不合法!" << endl;
                continue;
            }

            Cell& cell = board[row][col];
            if (cell.state == CellState::Revealed) {
                cout << "该格子已被翻开!" << endl;
                continue;
            } else if (cell.state == CellState::Flagged) {
                cout << "该格子已被标记为地雷!" << endl;
                continue;
            }

            if (cell.hasMine) {
                cout << "你踩到地雷了!游戏结束!" << endl;
                break;
            } else {
                revealCell(row, col);
                if (isVictory()) {
                    cout << "恭喜你,扫雷成功!" << endl;
                    break;
                }
            }
        }
    }

private:
    void printBoard() {
        cout << " ";
        for (int c = 0; c < cols; c++) {
            cout << " " << c;
        }
        cout << endl;

        for (int r = 0; r < rows; r++) {
            cout << r;
            for (int c = 0; c < cols; c++) {
                const Cell& cell = board[r][c];
                if (cell.state == CellState::Hidden) {
                    cout << " ?";
                } else if (cell.state == CellState::Revealed) {
                    if (cell.hasMine) {
                        cout << " *";
                    } else if (cell.neighborMines > 0) {
                        cout << " " << cell.neighborMines;
                    } else {
                        cout << "  ";
                    }
                } else {
                    cout << " F";
                }
            }
            cout << endl;
        }
    }

    void revealCell(int row, int col) {
        Cell& cell = board[row][col];
        cell.state = CellState::Revealed;

        if (cell.neighborMines == 0) {
            // 递归翻开相邻的空格子
            for (int dr = -1; dr <= 1; dr++) {
                for (int dc = -1; dc <= 1; dc++) {
                    int nr = row + dr;
                    int nc = col + dc;
                    if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && board[nr][nc].state == CellState::Hidden) {
                        revealCell(nr, nc);
                    }
                }
            }
        }
    }

    bool isVictory() {
        int count = 0;
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                if (board[r][c].state == CellState::Hidden) {
                    count++;
                }
            }
        }
        return count == numMines;
    }
};

int main() {
    int rows, cols, numMines;
    cout << "请输入扫雷板的行数:";
    cin >> rows;
    cout << "请输入扫雷板的列数:";
    cin >> cols;
    cout << "请输入地雷的数量:";
    cin >> numMines;

    Minesweeper game(rows, cols, numMines);
    game.play();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值