扫雷游戏(没人玩

#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int>> puzzle_type;
typedef vector<vector<int>> mask_type;
int randint(int x, int y)
{
    static auto seed = random_device()();
    static mt19937 rng(seed);
    uniform_int_distribution<> uni(x, y);
    return uni(rng);
}
int CountMines(puzzle_type pz, int x, int y)
{
    int res = 0;
    for (int i = max(y - 1, 0); i <= min(y + 1, (int)pz.size() - 1); i++)
    {
        for (int j = max(x - 1, 0); j <= min(x + 1, (int)pz.size() - 1); j++)
        {
            if (pz[i][j] == -1)
                ++res;
        }
    }
    return res;
}
/// @brief Print the Puzzle!
/// @param puzz,massk
/// @return Nothing really!
void printPz(puzzle_type puzz, mask_type massk, bool IDie = false, bool IWin = false)
{
    cout << "  ";
    for (int i = 0; i < puzz.size(); i++)
        cout << setw(3) << i;
    cout << endl;
    for (int i = 0; i < puzz.size(); i++)
    {
        cout << setw(2) << i;
        for (int j = 0; j < puzz.size(); j++)
        {
            cout << setw(3);
            if (massk[i][j] == -1 || massk[i][j] == -2) // mark
            {
                cout.clear();
                cout << "  \e[38;2;255;20;20m";
                cout << '$' << "\e[0m";
            }
            else if (massk[i][j] == 1)
            {
                if (puzz[i][j] == -1)
                {
                    cout.clear();
                    if (IWin)
                        cout << "  \e[38;2;20;255;20m";
                    else
                        cout << "  \e[38;2;255;20;20m";
                    cout << '*' << "\e[0m";
                }
                else if (puzz[i][j] == 0)
                    cout << '.';
                else
                    cout << puzz[i][j];
            }
            else
                cout << '#';
        }
        cout << endl;
    }
}
void turn(puzzle_type pz, mask_type &msk, int x, int y)
{
    if (x < 0 || y < 0 || y > pz.size() - 1)
        return;
    if (x > pz[y].size() - 1)
        return;
    if (msk[y][x] != 0 || pz[y][x] != 0)
        return;
    msk[y][x] = 1;
    turn(pz, msk, x + 1, y);
    turn(pz, msk, x - 1, y);
    turn(pz, msk, x, y + 1);
    turn(pz, msk, x, y - 1);
}
bool isDie(puzzle_type pz, mask_type msk)
{
    for (int i = 0; i < pz.size(); i++)
    {
        for (int j = 0; j < pz.size(); j++)
        {
            if (msk[i][j] == 1 && pz[i][j] == -1)
                return true;
        }
    }
    return false;
}
bool isWin(puzzle_type pz, mask_type msk, int minecount)
{
    int count = 0;
    for (int i = 0; i < pz.size(); i++)
    {
        for (int j = 0; j < pz.size(); j++)
        {
            if (msk[i][j] == 1)
                count++;
        }
    }
    if (count == (pz.size() * pz.size() - minecount))
        return true;
    else
    {
        int fl = 0;
        for (int i = 0; i < pz.size(); i++)
        {
            for (int j = 0; j < pz.size(); j++)
            {
                if (msk[i][j] == -1 || msk[i][j] == -2 && pz[i][j] == -1)
                    ++fl;
            }
        }
        if (fl == minecount)
            return true;
        else
            return false;
    }
}
string rules = "Input <x> <y> to controll the game,Example: 0 0. And Use <\'m\'> <x> <y> to mark a place,Example: m 0 0.\n";
int n, mineCnt;
int main()
{
    n = 9, mineCnt = 2;
    puzzle_type puzzlee(n, vector<int>(n, 0));
    mask_type mask(n, vector<int>(n, 0));
    vector<pair<int, int>> Pos_Mines(mineCnt);
    // Mine Gen
    for (int i = 0; i < mineCnt; i++)
    {
        bool fl = 1;
        for (; fl;)
        {
            auto x = randint(0, n - 1), y = randint(0, n - 1);
            if (puzzlee[y][x] != -1)
            {
                puzzlee[y][x] = -1;
                Pos_Mines[i] = {x, y};
                fl = 0;
            }
        }
    }
    // Count Mines
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (puzzlee[i][j] != -1)
            {
                puzzlee[i][j] = CountMines(puzzlee, j, i);
            }
        }
    }
    // Gaming
    int inx, iny;
    char oper;
    cout << rules;
    cout << "size:" << n << "*" << n << ",mines:" << mineCnt << endl;
    for (;;)
    {
        // Print Puzzle
        printPz(puzzlee, mask);
        // If lose
        if (isDie(puzzlee, mask))
        {
            for (int i = 0; i < mineCnt; i++)
                mask[Pos_Mines[i].second][Pos_Mines[i].first] = 1;
            cout << "\e[38;2;200;100;200m" << "-x " << "\e[0m" << endl;
            printPz(puzzlee, mask, true, false);
            cout << "\e[38;2;255;20;20m"
                 << "X.X\n"
                 << "\e[38;2;100;220;100mYou Died,Game Over. 菜就多练,输不起就别玩。以前是以前,现在是现在。"
                 << "\e[0m\n";
            break;
        }
        //  If win
        if (isWin(puzzlee, mask, mineCnt))
        {
            for (int i = 0; i < mineCnt; i++)
                mask[Pos_Mines[i].second][Pos_Mines[i].first] = 1;
            cout << "\e[38;2;150;200;250m" << "-x " << "\e[0m" << endl;
            printPz(puzzlee, mask, false, true);
            cout << "\e[38;2;255;150;50m"
                 << "^-^\n"
                 << "You WIN!"
                 << "\e[0m\n";
            break;
        }
        cout << "\e[38;2;200;100;200m" << "-> " << "\e[0m";
        oper = ' ';
        cin >> oper;
        if (isdigit(oper))
        {
            inx = oper - '0';
            cin >> iny;
        }
        else
        {
            cin >> inx >> iny;
            if (oper != 'm')
                continue;
        }
        if (inx < 0 || iny < 0 || iny > n - 1 || inx > n - 1)
        {
            continue;
        }
        if (oper == 'm')
        {
            if (mask[iny][inx] == 0)
                mask[iny][inx] = -2;
            else if (mask[iny][inx] == 1)
                mask[iny][inx] = -1;
            else if (mask[iny][inx] == -1)
                mask[iny][inx] = 1;
            else if (mask[iny][inx] == -2)
                mask[iny][inx] = 0;
        }
        else
        {
            if (puzzlee[iny][inx] == 0)
                turn(puzzlee, mask, inx, iny);
            if (mask[iny][inx] == -1)
                mask[iny][inx] = 1;
            else if (mask[iny][inx] == -2)
                mask[iny][inx] = 0;
            else
                mask[iny][inx] = 1;
        }
    }
    return 0;
}

有人要玩扫雷吗?

4 个赞
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <unistd.h>

int a = 0;
char floor_see[12][12] = {};
char floor_under[12][12] = {};
int x = 0, y = 0;
int score = 0;
int can_know = 3;

void print_floor()
{
    for (int i = 1; i <= a; ++i) {
        if (i == 10)
            printf("(%d): ", i);
        else
            printf("(%d ): ", i);
        for (int j = 1; j <= a; ++j)
            printf("%c ", floor_see[i][j]);
        printf("\n");
    }
}

void how_mach_gold(int x, int y)
{
    if (floor_under[x + 1][y] == '@')
        ++floor_under[x][y];
    if (floor_under[x + 1][y + 1] == '@')
        ++floor_under[x][y];
    if (floor_under[x + 1][y - 1] == '@')
        ++floor_under[x][y];
    if (floor_under[x][y + 1] == '@')
        ++floor_under[x][y];
    if (floor_under[x - 1][y] == '@')
        ++floor_under[x][y];
    if (floor_under[x - 1][y + 1] == '@')
        ++floor_under[x][y];
    if (floor_under[x - 1][y - 1] == '@')
        ++floor_under[x][y];
    if (floor_under[x][y - 1] == '@')
        ++floor_under[x][y];
}

char turn(int num)
{
    if (num == 0)
        return '0';
    if (num == 1)
        return '*';
    if (num == 2)
        return '@';
    if (num == 3)
        return '+';
}

bool not_all_have()
{
    for (int i = 1; i <= a; ++i) {
        for (int j = 1; j <= a; ++j) {
            if (floor_under[i][j] == '@' && floor_see[i][j] == '?')
                return true;
        }
    }

    return false;
}

bool is_num(char ch)
{
    if (ch >= '0' && ch <= '9')
        return true;
    return false;
}

int all_gold()
{
    int gold = 0;

    for (int i = 1; i <= a; ++i) {
        for (int j = 1; j <= a; ++j) {
            if (floor_under[i][j] == '@')
                ++gold;
        }
    }

    return gold;
}

void print_score()
{
    int gold = all_gold();

    printf("\n%d分,", score);
    if (score == gold)
        printf("太棒了!\n");
    else if (score >= gold - 10)
        printf("真不错!\n");
    else if (score >= gold - 15)
        printf("一般般。\n");
    else if (score >= 0)
        printf("也还行。\n");
    else
        printf("太差了。\n");
}

void print_under()
{
    printf("\n地下世界:\n");
    for (int i = 1; i <= a; ++i) {
        if (i == 10)
            printf("(%d): ", i);
        else
            printf("(%d ): ", i);
        for (int j = 1; j <= a; ++j)
            printf("%c ", floor_under[i][j]);
        printf("\n");
    }
}

int main()
{
    srand(time(nullptr));

    while (1) {
        printf("请输入雷区边长(2 <= 边长 <= 10):\n");
        scanf("%d", &a);
        if (a >= 2 && a <= 10)
            break;
        printf("边长不能为此数!\n");
    }

    for (int i = 1; i <= a; ++i) {
        for (int j = 1; j <= a; ++j)
            floor_see[i][j] = '?';
    }
    for (int i = 1; i <= a; ++i) {
        for (int j = 1; j <= a; ++j)
            floor_under[i][j] = turn(rand() % 4 + 0);
    }
    for (int i = 1; i <= a; ++i) {
        for (int j = 1; j <= a; ++j) {
            if (floor_under[i][j] == '0') {
                how_mach_gold(i, j);
                floor_see[i][j] = floor_under[i][j];
            }
        }
    }

    while (not_all_have()) {
        bool is_continue = false;
        print_floor();
        while (1) {
            printf("请输入你想翻开的地面编号,\n"
                   "输入(0 0)可以自己标记地雷(#),输入(11 11)可以标记无用数字(x),输入(-1 -1)可以获得提示(剩余次数:%d):\n", can_know);
            scanf("%d%d", &x, &y);
            if (x == 0 && y == 0) {
                is_continue = true;
                printf("请输入你想标记的地点:\n");
                scanf("%d%d", &x, &y);
                if (x > a || x < 1 || y > a || y < 1) {
                    printf("请好好玩!\n");
                    continue;
                }
                if (floor_see[x][y] != '?' && floor_see[x][y] != '#') {
                    printf("已翻开地点不可被标记!\n");
                    is_continue = false;
                    continue;
                }
                if (floor_see[x][y] == '?')
                    floor_see[x][y] = '#';
                else
                    floor_see[x][y] = '?';
                break;
            } else if (x == 11 && y == 11) {
                is_continue = true;
                printf("请输入你想标记的数字:\n");
                scanf("%d%d", &x, &y);
                if (x > a || x < 1 || y > a || y < 1) {
                    printf("请好好玩!\n");
                    continue;
                }
                if (!is_num(floor_see[x][y]) && floor_see[x][y] != 'x') {
                    printf("这个地点不可被标记!\n");
                    is_continue = false;
                    continue;
                }
                if (is_num(floor_see[x][y]))
                    floor_see[x][y] = 'x';
                else
                    floor_see[x][y] = floor_under[x][y];
                break;
            } else if (x == -1 && y == -1) {
                if (can_know <= 0) {
                    printf("你获取提示的机会用完了!\n");
                    continue;
                }
                --can_know;
                is_continue = true;
                printf("请输入你想知道的地点:\n");
                scanf("%d%d", &x, &y);
                if (x > a || x < 1 || y > a || y < 1) {
                    printf("请好好玩!\n");
                    continue;
                }
                if (floor_see[x][y] != '?' && floor_see[x][y] != '#') {
                    printf("这是已翻开地点!\n");
                    is_continue = false;
                    continue;
                }
                printf("%c\n", floor_under[x][y]);
            }
            if (x > a || x < 1 || y > a || y < 1) {
                printf("请好好玩!\n");
                continue;
            }
            if (floor_see[x][y] == '?' || floor_see[x][y] == '#')
                break;
            printf("它已经被翻开过了!\n");
        }
        if (is_continue)
            continue;
        floor_see[x][y] = floor_under[x][y];
        if (floor_see[x][y] == '@') {
            printf("太好了!是金币!\n");
            ++score;
        } else if (floor_see[x][y] == '*') {
            printf("不好!是地雷!\n");
            --score;
        } else if (floor_see[x][y] == '+') {
            printf("不错,是提示器!\n");
            ++can_know;
        }
        sleep(1);
    }

    print_score();
    print_under();

    return 0;
}
3 个赞

这不是报错了吗???

论坛不允许回老帖,更不允许发无关学术内容