跳到正文

科技·工程

小挺扫雷 测试版0.21

_xiaoting约 9 分钟
原文
文章信息与版本

文章信息

收录
2026年7月8日
更新
2026年7月8日
篇幅
约 9 分钟
获赞
1

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
int xiaoting; 
const int SIZE = 10;
const int MINES = 15;
enum GameState {
    PLAYING,
    WIN,
    LOSE
};
struct Cell {
    bool isMine;
    bool isRevealed;
    bool isFlagged;
    int nearbyMines;
};
vector<vector<Cell>> board(SIZE, vector<Cell>(SIZE));
GameState gameState = PLAYING;
int remainingCells = SIZE * SIZE - MINES;
void initializeBoard() {
    for (int i = 0; i < SIZE; ++i) {
        for (int j = 0; j < SIZE; ++j) {
            board[i][j] = {false, false, false, 0};
        }
    }
    int minesPlaced = 0;
    while (minesPlaced < MINES) {
        int x = rand() % SIZE;
        int y = rand() % SIZE;
        if (!board[x][y].isMine) {
            board[x][y].isMine = true;
            minesPlaced++;
        }
    }
    for (int i = 0; i < SIZE; ++i) {
        for (int j = 0; j < SIZE; ++j) {
            if (!board[i][j].isMine) {
                int count = 0;
                for (int dx = -1; dx <= 1; ++dx) {
                    for (int dy = -1; dy <= 1; ++dy) {
                        if (dx == 0 && dy == 0) continue;
                        int ni = i + dx, nj = j + dy;
                        if (ni >= 0 && ni < SIZE && nj >= 0 && nj < SIZE && board[ni][nj].isMine) {
                            count++;
                        }
                    }
                }
                board[i][j].nearbyMines = count;
            }
        }
    }
}
void displayBoard(bool showMines = false) {
    cout << "   ";
    for (int j = 0; j < SIZE; ++j) {
        cout << setw(2) << j << "  ";
    }
    cout << "\n  +";
    for (int j = 0; j < SIZE; ++j) {
        cout << "---+";
    }
    cout << endl;
    for (int i = 0; i < SIZE; ++i) {
        cout << setw(2) << i << "|";
        for (int j = 0; j < SIZE; ++j) {
            if (board[i][j].isRevealed) {
                if (board[i][j].isMine) {
                    cout << " * |";
                } else {
                    cout << " " << board[i][j].nearbyMines << " |";
                }
            } else if (board[i][j].isFlagged) {
                cout << " F |";
            } else if (showMines && board[i][j].isMine) {
                cout << " M |";
            } else {
                cout << "   |";
            }
        }
        cout << "\n  +";
        for (int j = 0; j < SIZE; ++j) {
            cout << "---+";
        }
        cout << endl;
    }
}
void revealCell(int x, int y) {
    if (x < 0 || x >= SIZE || y < 0 || y >= SIZE || board[x][y].isRevealed || board[x][y].isFlagged) {
        return;
    }
    board[x][y].isRevealed = true;
    remainingCells--;
    if (board[x][y].isMine) {
        gameState = LOSE;
        return;
    }
    if (board[x][y].nearbyMines == 0) {
        for (int dx = -1; dx <= 1; ++dx) {
            for (int dy = -1; dy <= 1; ++dy) {
                if (dx == 0 && dy == 0) continue;
                revealCell(x + dx, y + dy);
            }
        }
    }
    if (remainingCells == 0) {
        gameState = WIN;
    }
}
void toggleFlag(int x, int y) {
    if (x >= 0 && x < SIZE && y >= 0 && y < SIZE && !board[x][y].isRevealed) {
        board[x][y].isFlagged = !board[x][y].isFlagged;
    }
}
void playGame() {
    srand(time(0));
    initializeBoard();
    while (gameState == PLAYING) {
        displayBoard();
        cout << "\n剩余安全区域: " << remainingCells << endl;
        cout << "输入命令 (r x y 揭开单元格, f x y 标记/取消标记, q 退出): ";
        char cmd;
        cin >> cmd;
        if (cmd == 'q') {
            cout << "游戏结束!\n";
            cin>>xiaoting;
            return;
        }
        int x, y;
        cin >> x >> y;
        if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) {
            cout << "无效的坐标!\n";
            continue;
        }
        if (cmd == 'r') {
            revealCell(x, y);
        } else if (cmd == 'f') {
            toggleFlag(x, y);
        } else {
            cout << "无效的命令!\n";
        }
    }
    displayBoard(true);
    if (gameState == WIN) {
        cout << "\n恭喜你赢了!\n";
        cin>>xiaoting;
        
    } else {
        cout << "\n很遗憾,你踩到地雷了!\n";
        cin>>xiaoting;
    }
}
int main() {
    cout << "============ 小挺扫雷 ============\n";
    cout << "======= 版本:0.2.1 测试版 =======\n";
    cout << "命令说明:\n";
    cout << "r x y - 揭开单元格 (x,y)\n";
    cout << "f x y - 标记/取消标记单元格 (x,y)\n";
    cout << "q - 退出游戏\n";
    cout << "F - 标记的单元格\n";
    cout << "* - 地雷\n";
    cout << "数字 - 周围的地雷数量\n";
    cout << "==================================\n\n";
    playGame();
    return 0;
}
原文