下面是一个简单的C++扫雷游戏代码示例:

#include <iostream>
#include <vector>
#include <random>

const int SIZE = 10; // 游戏板大小
const int NUM_MINES = 10; // 雷的数量

// 定义一个结构体表示一个方块
struct Square {
    bool isMine; // 是否是雷
    bool isRevealed; // 是否被翻开
    int numSurroundingMines; // 周围的雷的数量
};

// 定义一个二维数组表示游戏板
std::vector<std::vector<Square>> board(SIZE, std::vector<Square>(SIZE));

// 初始化游戏板
void initializeBoard() {
    // 初始化所有方块为非雷且未翻开
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            board[i][j].isMine = false;
            board[i][j].isRevealed = false;
            board[i][j].numSurroundingMines = 0;
        }
    }

    // 随机放置雷
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, SIZE - 1);
    for (int i = 0; i < NUM_MINES; i++) {
        int x = dis(gen);
        int y = dis(gen);
        if (!board[x][y].isMine) {
            board[x][y].isMine = true;
        } else {
            i--; // 重新放置雷
        }
    }

    // 计算每个方块周围的雷的数量
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            if (board[i][j].isMine) {
                continue;
            }
            // 遍历方块周围的8个方块
            for (int dx = -1; dx <= 1; dx++) {
                for (int dy = -1; dy <= 1; dy++) {
                    int nx = i + dx;
                    int ny = j + dy;
                    if (nx >= 0 && nx < SIZE && ny >= 0 && ny < SIZE && board[nx][ny].isMine) {
                        board[i][j].numSurroundingMines++;
                    }
                }
            }
        }
    }
}

// 翻开方块
void revealSquare(int x, int y) {
    // 如果方块已经被翻开或者是雷,则不做任何操作
    if (board[x][y].isRevealed || board[x][y].isMine) {
        return;
    }

    // 翻开方块
    board[x][y].isRevealed = true;

    // 如果翻开的方块周围没有雷,则递归翻开周围的方块
    if (board[x][y].numSurroundingMines == 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 < SIZE && ny >= 0 && ny < SIZE) {
                    revealSquare(nx, ny);
                }
            }
        }
    }
}

// 显示游戏板
void printBoard() {
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            if (board[i][j].isRevealed) {
                if (board[i][j].isMine) {
                    std::cout << "* ";
                } else {
                    std::cout << board[i][j].numSurroundingMines << " ";
                }
            } else {
                std::cout << "- ";
            }
        }
        std::cout << std::endl;
    }
}

int main() {
    initializeBoard();
    printBoard();

    // 玩家输入坐标翻开方块
    int x, y;
    while (true) {
        std::cout << "Enter the coordinates (x, y) to reveal a square: ";
        std::cin >> x >> y;
        if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) {
            std::cout << "Invalid coordinates. Please try again." << std::endl;
            continue;
        }
        revealSquare(x, y);
        printBoard();
        if (board[x][y].isMine) {
            std::cout << "Game over! You hit a mine." << std::endl;
            break;
        }
    }

    return 0;
}

这个代码实现了一个简单的扫雷游戏,游戏板的大小为10x10,雷的数量为10。玩家可以通过输入坐标来翻开方块,如果翻开的方块是雷,则游戏结束。每个方块周围显示的数字表示周围的雷的数量

c++扫雷代码

原文地址: https://www.cveoy.top/t/topic/hFaR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录