扫雷 2.0

· · 休闲·娱乐

扫雷 2.0

转载自 LegendaryMiracle

欢迎各位提出修改建议。

#include <bits/stdc++.h>
#include <windows.h>
#define cl system("cls")
#define Made using
#define by namespace
#define LegendaryMiracle std
Made by LegendaryMiracle;
mt19937_64 mrand(random_device {}());
const string str[] = {"加载游戏中", "绘制地图中", "进入页面中", "等待系统中", "加载难度中"};
char board[32][32];
bool shown[32][32] = {}, spreaded[32][32] = {};
int n;
void Slowsay(string a, int sp) {
    int l = a.size();
    for (int i = 0; i < l; i++) {
        cout << a[i];
        Sleep(sp);
    }
}
int rng(int x) {
    int y = mrand() % x + 1;
    return y;
}
int is_mine(int x, int y) {
    if (x < 0 || y < 0 || x >= n || y >= n) {
        return 0;
    }
    return board[x][y] == '*';
}
int around(int x, int y) {
    return is_mine(x - 1, y) + is_mine(x + 1, y) + is_mine(x - 1, y - 1) + is_mine(x + 1, y - 1) + is_mine(x - 1, y + 1) + is_mine(x + 1, y + 1) + is_mine(x, y - 1) + is_mine(x, y + 1);
}
void spread(int x, int y) {
    if (!(x < 0 || y < 0 || x >= n || y >= n || spreaded[x][y] == 1 || board[x][y] == '*')) {
        shown[x][y] = 1;
        spreaded[x][y] = 1;
        if (board[x][y] == '0') {
            spread(x - 1, y);
            spread(x + 1, y);
            spread(x, y - 1);
            spread(x, y + 1);
        }
    }
}
void draw_map(int x) {
    int sp = 12;
    if (n >= 10)
        sp = 3;
    if (n >= 20)
        sp = 0.75;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (x == 1 || (x == 3 && !shown[i][j]))
                printf("?");
            if (x == 2 || (x == 3 && shown[i][j]))
                printf("%c", board[i][j]);
            Sleep(sp);
        }
        puts("");
        Sleep(sp * 2);
    }
}
void start(bool x) {
    if (x) {
        Slowsay("欢迎游玩扫雷2.0!\n", 20);
        Slowsay("原作者 : LegendaryMiracle (uid 1557225)\n", 15);
        Slowsay("2.0 版本作者 : Ambrose0321 (uid 1470808)\n\n", 15);
        Slowsay("请选择难度\n", 20);
        Slowsay("简单难度输入1\n", 20);
        Slowsay("中等难度输入2\n", 20);
        Slowsay("困难难度输入3\n", 20);
        return;
    }
    Slowsay("欢迎游玩扫雷2.0!\n", 75);
    Sleep(100);
    Slowsay("原作者 : LegendaryMiracle (uid 1557225)\n", 50);
    Sleep(50);
    Slowsay("2.0 版本作者 : Ambrose0321 (uid 1470808)\n\n", 50);
    Sleep(250);
    Slowsay("请选择难度", 75);
    Sleep(300);
    Slowsay("\n简单难度输入1", 60);
    Sleep(300);
    Slowsay("\n中等难度输入2", 60);
    Sleep(300);
    Slowsay("\n困难难度输入3\n", 60);
}
int row() {
    Slowsay("普通操作请输入1, 标记雷请输入2\n", 10);
    int t;
    cin >> t;
    while (t < 1 || t > 2) {
        Slowsay("输入不合法, 请重新输入\n", 10);
        Sleep(300);
        cl;
        draw_map(3);
        Slowsay("普通操作请输入1, 标记雷请输入2\n", 10);
        cin >> t;
    }
    return t;
}
int main() {
    for (int r = 1; ; r++) {
        start(r > 1);
        cin >> n;
        while (n < 1 || n > 3) {
            Slowsay("输入不合法,请重新输入!", 10);
            Sleep(500);
            cl;
            start(1);
            cin >> n;
        }
        cl;
        n = pow(2, n - 1) * 5;
        for (int i = 3; i; --i) {
            Slowsay(str[rng(5) - 1], rng(500));
            Slowsay("... ...", rng(150));
            cl;
        }
        int x, y, mine_cnt = (mrand() % n / 2) + n / 2;
        for (int i = 0; i < mine_cnt; i++) {
            x = mrand() % n;
            y = mrand() % n;
            while (board[x][y] == '*') {
                x = mrand() % n;
                y = mrand() % n;
            }
            board[x][y] = '*';
        }
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                if (board[i][j] != '*')
                    board[i][j] = char(around(i, j) + '0');
        Sleep(rng(1500));
        Slowsay("创建完毕!\n", 20);
        cl;
        draw_map(1);
        Slowsay("=== 游戏开始 ===\n", 30);
        int type, find_cnt = 0;
        for (; ; ) {
            if (find_cnt == mine_cnt) {
                Slowsay("你赢了!\n", 20);
                Slowsay("你找到了 ", 30);
                printf("%d/%d ", find_cnt, mine_cnt);
                Slowsay("个雷\n完整棋盘:\n", 30);
                draw_map(2);
                break;
            }
            type = row();
            Slowsay("请输入坐标 (x, y)\n", 5);
            cin >> x >> y;
            x--;
            y--;
            while (x < 0 || y < 0 || x >= n || y >= n || shown[x][y]) {
                Slowsay("输入不合法(", 10);
                if (x < 0 || y < 0 || x >= n || y >= n)
                    Slowsay("坐标不合法)", 10);
                else
                    Slowsay("此点已被展示)", 10);
                Sleep(600);
                cl;
                draw_map(3);
                Slowsay("请输入坐标 (x, y)\n", 5);
                cin >> x >> y;
                x--;
                y--;
            }
            cl;
            if (type == 1 && board[x][y] == '*' || type == 2 && board[x][y] != '*') {
                Slowsay("你输了!\n", 20);
                Slowsay("你找到了 ", 30);
                printf("%d/%d ", find_cnt, mine_cnt);
                Slowsay("个雷\n完整棋盘:\n", 30);
                draw_map(2);
                break;
            }
            if (type == 2) {
                Slowsay("标记成功!\n", 10);
                shown[x][y] = 1;
                ++find_cnt;
            } else {
                Slowsay("此格不是雷!\n", 10);
                spread(x, y);
            }
            Slowsay("目前棋盘:\n", 5);
            draw_map(3);
        }
        system("pause");
        cl;
        Slowsay("是否再玩一局?\n", 50);
        Slowsay("再玩一局请输入 1,退出请输出 2\n", 20);
        int c;
        cin >> c;
        cl;
        if (c == 2)
            break;
    }
    system("color 3b");
    Slowsay(" ______________________________________\n", 15);
    Slowsay("|                                      |\n", 15);
    Slowsay("|           感谢您游玩本游戏           |\n", 30);
    Slowsay("|           期待您的下次游玩           |\n", 30);
    Slowsay("|  如果喜欢的话就给两位作者点个关注吧  |\n", 30);
    Slowsay("|       您的支持使我们最大的动力!     |\n", 30);
    Slowsay("|______________________________________|\n", 15);
}