扫雷

· · 个人记录

根据国际惯例,一篇文章需要一个头图

今天闲着没事干做了个扫雷(控制台版本)

第一次做游戏,做得不好望各位大佬见谅

废话少说,上代码

#include<cstdio>
#include<stdlib.h>
#include<iostream>
#include<time.h>
#include<conio.h>
#include<Windows.h>
#include<cstring>
using namespace std;
const int MAX=10005;
const int dre[9][2]= {{0,0},{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
const int dre_4[5][2]= {{0,0},{1,0},{0,1},{-1,0},{0,-1}};
const char f[3]= {'#','?','!'};

int n,m,sb,mp;
int num;
bool gend,cli,wcflag;

int vin,vx,vy;
char vch;
char col[2];

int map[MAX][MAX];
bool see[MAX][MAX];
int flag[MAX][MAX];

string s[6]=
{
    "\n                         欢迎来到  扫雷  游戏\n\n",
    "                                 本游戏由\n\n",
    "                                Kavin_0409\n",
    "                              zengzhiyu1022\n\n",
    "                                 共同制作\n\n\n",
    "                   1. 开始游戏   2. 游戏规则   3.更改颜色\n\n\n"
};
string guize="                            游 戏 介 绍\n\n\n      游戏中,玩家通过选择空白方格来揭示方格内的数字.这些数字代表了与它们相邻\n\n  的8个方格内藏有的地雷数量.玩家需要通过推理,判断出那些方格内有地雷,并揭开所\n\n  有没有地雷的方格.";

inline int read()
{
    int s=0,w=1;
    char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-')
        {
            w=-1;
        }
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        s=s*10+c-'0';
        c=getchar();
    }
    return s*w;
}
void setsize(int col,int row)
{
    char cmd[64];
    sprintf(cmd,"mode con cols=%d lines=%d",col,row);
    system(cmd);
}
void HideCursor(bool sw)
{
    CONSOLE_CURSOR_INFO cursor_info = {1, sw};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void color()
{
    setsize(20,25);
    system("cls");
    cout<<"color:\n";
    cout<<"0   Black\n";
    cout<<"1 D_Blue\n";
    cout<<"2 D_Green\n";
    cout<<"3 D_Sky\n";
    cout<<"4 D_red\n";
    cout<<"5 D_Purple\n";
    cout<<"6 D_Yellow\n";
    cout<<"7 D_White\n";
    cout<<"8   Grey\n";
    cout<<"9 L_Blue\n";
    cout<<"a L_Green\n";
    cout<<"b L_Sky\n";
    cout<<"c L_Red\n";
    cout<<"d L_Purple\n";
    cout<<"e L_Yellow\n";
    cout<<"f L_White\n";
    HideCursor(1);
    cout<<"请选择背景颜色\n";
    cin>>col[0];
    cout<<"请选择文字颜色\n";
    cin>>col[1];
    string c_u="color ";
    c_u+=col[0];
    c_u+=col[1];
    system(c_u.c_str());
    HideCursor(0);
    return;
}
void welcome()
{
cd:
    setsize(79,25);
    for(int i=0; i<6; i++)
    {
        cout<<s[i];
        if(!wcflag)Sleep(500);
    }
    wcflag=1;
qq:
    HideCursor(1);
    vin=read();
    HideCursor(0);
    while(vin!=1&&vin!=2&&vin!=3&&vin!=114514)
    {
        system("cls");
        for(int i=0; i<6; i++)
        {
            cout<<s[i];
        }
        HideCursor(1);
        vin=read();
        HideCursor(0);
    }
    while(vin!=1)
    {
        if(vin==114514)
        {
            system("cls");
            cout<<"\n                                  彩 蛋 ! ! !\n";
            Sleep(1000);
            cout<<"\n                           即将 打开 作者主页 ! ! !\n";
            Sleep(1000);
            system("start https://www.luogu.com.cn/user/916760");
            system("start https://www.luogu.com.cn/user/1110182");
            Sleep(5000);
            system("cls");
            goto cd;
        }
        else if(vin==2)
        {
            system("cls");
            cout<<guize;
            cout<<"\n\n\n  0.退出\n";
            while(1)
            {
                HideCursor(1);
                vin=read();
                HideCursor(0);
                if(vin==0)
                {
                    system("cls");
                    for(int i=0; i<6; i++)
                    {
                        cout<<s[i];
                    }
                    break;
                }
                else
                {
                    system("cls");
                    cout<<guize;
                    cout<<"0.退出\n\n\n";
                }
            }
            HideCursor(1);
            vin=read();
            HideCursor(0);
        }
        else if(vin==3)
        {
            color();
            cout<<"更改成功!";
            Sleep(1500);
            system("cls");
            goto cd;
        }
        else
        {
            goto qq;
        }
    }
    return;
}
void jiazai()
{
    setsize(79,25);
    system("cls");
    cout<<"\n                                  扫雷\n\n\n            kkksc03提醒您:\n\n";
    cout<<"            抵制不良游戏 拒绝盗版游戏 注意自我保护 谨防受骗上当。\n\n";
    cout<<"            适度游戏益脑 沉迷游戏伤身 合理安排时间 享受健康生活。\n\n\n";
    cout<<"      |============================================================  0%|\b\b";
    for(int i=1; i<=60; i++)
    {
        Sleep(50);
        for(int j=1; j<=60-i+4; j++)
        {
            cout<<'\b';
        }
        cout<<">";
        for(int j=i+1; j<=60; j++)
        {
            cout<<"=";
        }
        cout<<' ';
        if(100*i/60<10)
        {
            cout<<' ';
        }
        cout<<100*i/60;
    }
    cout<<"\n\n                               加载完成!!!\n\n\n";
    Sleep(1000);
}
void click(int x,int y)
{
    if(map[x][y]==-1)
    {
        gend=1;
        return;
    }
    if(x>n||x<1||y>m||y<1||see[x][y])
    {
        return;
    }
    num--;
    see[x][y]=1;
    if(map[x][y]>0)
    {
        return;
    }
    for(int i=1; i<=4; i++)
    {
        click(x+dre_4[i][0],y+dre_4[i][1]);
    }
    return;
}
void s_map()
{
    setsize(max(79,m+4),max(25,n+5));
    system("cls");
    cout<<"\n   ";
    for(int i=0; i<m; i++)
    {
        cout<<char(i%26+'a');
    }
    cout<<'\n';
    for(int i=1; i<=n; i++)
    {
        cout<<' '<<i%10<<'|';
        for(int j=1; j<=m; j++)
        {
            if(!map[i][j])cout<<" ";
            else if(map[i][j]==-1)cout<<"@";
            else cout<<map[i][j];
        }
        cout<<"|\n";
    }
    cout<<"  ";
    for(int i=-1; i<=m; i++)
    {
        cout<<'`';
    }
    cout<<"\n按任意键退出......";
    getch();
}
char game_end()
{
e:
    setsize(79,25);
    system("cls");
    cout<<" BBBBBBB      OOOOO        OOOOO         M     M      !!\n";
    cout<<" B      B   OO     OO    OO     OO      M M   M M     !!\n";
    cout<<" B      B  OO       OO  OO       OO    M  M   M  M    !!\n";
    cout<<" BBBBBBB   O         O  O         O   M    M M    M   !!\n";
    cout<<" B      B  OO       OO  OO       OO   M    M M    M   !!\n";
    cout<<" B      B   OO     OO    OO     OO   M      M      M    \n";
    cout<<" BBBBBBB      OOOOO        OOOOO     M             M  !!\n";
    cout<<"\n 输入h回到主页\n\n 输入m查看地图\n\n 输入r重玩本地图\n\n 输入n开启新地图\n\n 输入c设置颜色\n\n 其余退出\n\n ";
    char ch;
    HideCursor(1);
    cin>>ch;
    HideCursor(0);
    if(ch=='m')
    {
        s_map();
        goto e;
    }
    if(ch=='c')
    {
        color();
        goto e;
    }
    if(ch=='h')
    {
        ch='n';
        welcome();
        jiazai();
    }
    return ch;
}
bool w_flag=false;
char game_win()
{
w:
    setsize(79,25);
    system("cls");
    cout<<" W      W      W  IIIIII  NN      N  !!\n";
    cout<<" W     W W     W    II    N N     N  !!\n";
    cout<<"  W    W W    W     II    N  N    N  !!\n";
    cout<<"  W   W   W   W     II    N   N   N  !!\n";
    cout<<"   W  W   W  W      II    N    N  N  !!\n";
    cout<<"   W W     W W      II    N     N N    \n";
    cout<<"    W       W     IIIIII  N      NN  !!\n";
    if(mp==114514&&!w_flag)
    {
        cout<<"\n 地图114514触发彩蛋\n";
        Sleep(2000);
        cout<<"\n 你被骗了!!!\n";
        Sleep(1000);
        system("start https://vdse.bdstatic.com//192d9a98d782d9c74c96f09db9378d93.mp4");
        Sleep(1000);
    }
    w_flag=true;
    cout<<"\n 输入h回到主页\n\n 输入m查看地图\n\n 输入r重玩本地图\n\n 输入n开启新地图\n\n 输入c设置颜色\n\n 其余退出\n\n ";
    HideCursor(1);
    char ch;
    cin>>ch;
    HideCursor(0);
    if(ch=='m')
    {
        s_map();
        goto w;
    }
    if(ch=='c')
    {
        color();
        goto w;
    }
    if(ch=='h')
    {
        ch='n';
        welcome();
        jiazai();
    }
    return ch;
}
void make_map(int lx,int ly,int sx,int sy,int num_boom,int num_map)
{
    srand(num_map==-1?clock():num_map);
    for(int i=1; i<=num_boom; i++)
    {
        int x,y;
        while(1)
        {
            x=rand()%lx+1;
            y=rand()%ly+1;
            if(x==sx&&y==sy)continue;
            if(map[x][y]!=-1)break;
        }
        for(int j=1; j<=8; j++)
        {
            if(map[x+dre[j][0]][y+dre[j][1]]>=0)
            {
                map[x+dre[j][0]][y+dre[j][1]]++;
            }
        }
        map[x][y]=-1;
    }
}
void play()
{
n:
    setsize(79,25);
    cli=0;
    memset(map,0,sizeof(map));
    system("cls");
    cout<<s[0]<<s[1]<<s[2]<<s[3]<<s[4];
    cout<<"\n    请输入 行数 列数 雷数 地图(随机输入-1)\n\n    ";
    HideCursor(1);
    cin>>n>>m>>sb>>mp;
    HideCursor(0);
r:
    setsize(max(79,m+4),max(25,n+10));
    num=n*m;
    gend=0;
    memset(see,0,sizeof(see));
    memset(flag,0,sizeof(flag));
    while(1)
    {
        if(num==sb)
        {
            vch=game_win();
            if(vch=='r')
            {
                goto r;
            }
            if(vch=='n')
            {
                goto n;
            }
            break;
        }
        system("cls");
        cout<<"\n   ";
        for(int i=0; i<m; i++)
        {
            cout<<char(i%26+'a');
        }
        cout<<'\n';
        for(int i=1; i<=n; i++)
        {
            cout<<' '<<i%10<<'|';
            for(int j=1; j<=m; j++)
            {
                if(see[i][j])
                {
                    if(!map[i][j])cout<<" ";
                    else cout<<map[i][j];
                }
                else
                {
                    cout<<f[flag[i][j]];
                }
            }
            cout<<"|\n";
        }
        cout<<"  ";
        for(int i=-1; i<=m; i++)
        {
            cout<<'`';
        }
        cout<<endl;
        cout<<"0.标记    1.清扫\n";
        HideCursor(1);
        if(read())
        {
            cout<<"请输入你想清扫的方块(行 列)\n";
            vx=read();
            vy=read();
            HideCursor(0);
            if(!cli)
            {
                make_map(n,m,vx,vy,sb,mp);
                cli=1;
            }
            click(vx,vy);
            if(gend)
            {
                vch=game_end();
                if(vch=='r')
                {
                    goto r;
                }
                if(vch=='n')
                {
                    goto n;
                }
                break;
            }
        }
        else
        {
            HideCursor(1);
            cout<<"请输入你想标记的方块(行 列)\n";
            cin>>vx>>vy;
            cout<<"你想标记此方块为 0未知(?) 1地雷(!) 其余取消\n";
            cin>>vin;
            HideCursor(0);
            if(vin==0)
            {
                flag[vx][vy]=1;
            }
            else if(vin==1)
            {
                flag[vx][vy]=2;
            }
            else
            {
                flag[vx][vy]=0;
            }
        }
    }
    return;
}
int main()
{
    HideCursor(0);
    SetWindowLongPtrA(GetConsoleWindow(),GWL_STYLE,GetWindowLongPtrA(GetConsoleWindow(),GWL_STYLE)&~WS_SIZEBOX&~WS_MAXIMIZEBOX&~WS_MINIMIZEBOX);
    welcome();
    jiazai();
    play();
    return 0;
}

感谢观看

如有意见或建议请发表于评论区

转载请说明出处

本游戏由作者与zengzhiyu1022共同制作