扫雷小游戏源码

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

using namespace std;

const int WIDTH = 10;
const int HEIGHT = 10;
const int MINE_COUNT = 10;

class MineSweeper {
private:
    vector<vector<int>> board;
    vector<vector<bool>> revealed;

    // 初始化棋盘
    void initBoard() {
        board.resize(HEIGHT, vector<int>(WIDTH, 0));
        revealed.resize(HEIGHT, vector<bool>(WIDTH, false));

        // 随机放置地雷
        srand(static_cast<unsigned int>(time(nullptr)));
        int count = 0;
        while (count < MINE_COUNT) {
            int x = rand() % WIDTH;
            int y = rand() % HEIGHT;
            if (board[y][x]!= -1) {
                board[y][x] = -1;
                count++;
            }
        }

        // 计算每个格子周围的地雷数量
        for (int y = 0; y < HEIGHT; y++) {
            for (int x = 0; x < WIDTH; x++) {
                if (board[y][x]!= -1) {
                    int mineCount = 0;
                    for (int dy = -1; dy <= 1; dy++) {
                        for (int dx = -1; dx <= 1; dx++) {
                            int nx = x + dx;
                            int ny = y + dy;
                            if (nx >= 0 && nx < WIDTH && ny >= 0 && ny < HEIGHT && board[ny][nx] == -1) {
                                mineCount++;
                            }
                        }
                    }
                    board[y][x] = mineCount;
                }
            }
        }
    }

    // 打印棋盘
    void printBoard() {
        for (int y = 0; y < HEIGHT; y++) {
            for (int x = 0; x < WIDTH; x++) {
                if (revealed[y][x]) {
                    if (board[y][x] == -1) {
                        cout << "* ";
                    } else {
                        cout << board[y][x] << " ";
                    }
                } else {
                    cout << "# ";
                }
            }
            cout << endl;
        }
    }

    // 递归揭示空白区域
    void revealEmpty(int x, int y) {
        if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT || revealed[y][x]) {
            return;
        }

        revealed[y][x] = true;
        if (board[y][x] == 0) {
            for (int dy = -1; dy <= 1; dy++) {
                for (int dx = -1; dx <= 1; dx++) {
                    revealEmpty(x + dx, y + dy);
                }
            }
        }
    }

public:
    MineSweeper() {
        initBoard();
    }

    void play() {
        int x, y;
        while (true) {
            printBoard();

            cout << "PLEASE INPUT (x y): ";
            cin >> x >> y;

            if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) {
                cout << "ERROR!" << endl;
                continue;
            }

            if (board[y][x] == -1) {
                cout << "BOOM,YOU LOSE!" << endl;
                break;
            }

            revealed[y][x] = true;
            if (board[y][x] == 0) {
                revealEmpty(x, y);
            }

            bool allRevealed = true;
            for (int y = 0; y < HEIGHT; y++) {
                for (int x = 0; x < WIDTH; x++) {
                    if (board[y][x]!= -1 &&!revealed[y][x]) {
                        allRevealed = false;
                        break;
                    }
                }
            }
            if (allRevealed) {
                cout << "YOU WIN!" << endl;
                break;
            }
        }
    }
};

int main() {
    MineSweeper game;
    game.play();

    return 0;
}

以下是简单的 Java 扫雷小游戏源代码: ``` import java.util.Random; import java.util.Scanner; public class Minesweeper { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Random random = new Random(); int rows, columns, bombs; System.out.print("Enter number of rows: "); rows = scanner.nextInt(); System.out.print("Enter number of columns: "); columns = scanner.nextInt(); System.out.print("Enter number of bombs: "); bombs = scanner.nextInt(); boolean[][] board = new boolean[rows][columns]; int[][] counts = new int[rows][columns]; // randomly place bombs for (int i = 0; i < bombs; i++) { int row, column; do { row = random.nextInt(rows); column = random.nextInt(columns); } while (board[row][column]); board[row][column] = true; for (int dr = -1; dr <= 1; dr++) { for (int dc = -1; dc <= 1; dc++) { if (dr != 0 || dc != 0) { int r = row + dr; int c = column + dc; if (r >= 0 && r < rows && c >= 0 && c < columns) { counts[r][c]++; } } } } } boolean[][] revealed = new boolean[rows][columns]; int remaining = rows * columns - bombs; while (remaining > 0) { // print the board for (int c = 0; c < columns; c++) { System.out.print(" " + c); } System.out.println(); for (int r = 0; r < rows; r++) { System.out.print(r); for (int c = 0; c < columns; c++) { if (revealed[r][c]) { if (board[r][c]) { System.out.print("* "); } else { System.out.print(counts[r][c] + " "); } } else { System.out.print(". "); } } System.out.println(); } // ask for the next move int row, column; do { System.out.print("Enter row and column: "); row = scanner.nextInt(); column = scanner.nextInt(); } while (row < 0 || row >= rows || column < 0 || column >= columns || revealed[row][column]); revealed[row][column] = true; remaining--; // handle the move if (board[row][column]) { System.out.println("Boom! Game over."); break; } else if (counts[row][column] == 0) { // reveal all neighbors for (int dr = -1; dr <= 1; dr++) { for (int dc = -1; dc <= 1; dc++) { if (dr != 0 || dc != 0) { int r = row + dr; int c = column + dc; if (r >= 0 && r < rows && c >= 0 && c < columns && !revealed[r][c]) { revealed[r][c] = true; remaining--; } } } } } } if (remaining == 0) { System.out.println("Congratulations! You win!"); } scanner.close(); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值