跳到正文

个人记录

扫雷小游戏

jijidawang约 18 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 18 分钟
获赞
2

版本

  1. v1当前

原创!

漏洞很多,请见谅…

C++
/*
    Name: Jijidawang
    Copyright: JDW
    Author: Jijidawang
    Date: 13/01/20 13:21
    Description: Sometings about C++.
*/
#include<ctime>
#include<conio.h>
#include<cstdlib>
#include<windows.h>
#include<cstdio>
#include<iostream>
using namespace std;
//==================================Vars========================================
bool isEnd=false;                                                /*是否结束游戏*/

//Cell:未开启方块 ;
//nnum:空地 ;
//openboom:被点开的雷 ;
//flag:插旗 ;
//point:目前方块 ;
typedef const char* CS;      /*CS=const string.*/
CS Cell="▇",nnum="  ",openboom="- ",flag="+ ",point="* ";

const int N=12,M=12,boom=10; /* N:长+2;  M:宽+2;   boom:雷数. */
int x,y;                     /* 目前坐标 */
int NowBoom;                 /* 未排的雷数 */
bool isdfs[N+1][M+1];        /* DFS标记 */
//==========================Structs/Classes===================================
struct cell                             //方块结构
{
    //isopened:是否打开
    //isboom:是否是雷
    //isflag:是否被插旗
    //isnum:是否不是空地
    bool isopened,isboom,isflag,isnum;
    int num; /* 旁边雷数 */
}map[N+1][M+1];                         //地图,在主地图外面加一圈,便于统计雷

int k=(N-2)*(M-2); /*目前未打开方块数*/

enum direction{W,A,S,D,E,T};
//方向枚举: W(0):上; A(1):左; S(2):下; D(3):右,E(4):选择,T(5):插旗
//=============================functions======================================
void init() //初始化地图为空
{
    x=y=1;
    memset(isdfs,false,sizeof(isdfs));
    NowBoom=boom;
    for (int i=0;i<N;i++)
        for (int j=0;j<M;j++)
        {
            map[i][j].isopened=false;
            map[i][j].isboom=false;
            map[i][j].isflag=false;
            map[i][j].isnum=true;
            map[i][j].num=0;
            if (i==0||j==0||i==N-1||j==M-1) map[i][j].isnum=false;
        }
}
void print(bool);
void DFS(int,int);
void start() //开始游戏
{
    srand(int(time(NULL)));
    for (int i=0;i<boom;i++)  //布置boom个雷
    {
        int tx=rand()%(N-2)+1,ty=rand()%(M-2)+1; //随机出一个坐标
        while (map[tx][ty].isboom)               //去重
            tx=rand()%(N-2)+1,ty=rand()%(M-2)+1;
        map[tx][ty].isboom=true;     //标记为雷
    }
    for (int i=1;i<N-1;i++)            //将整个地图初始化
    {
        for (int j=1;j<M-1;j++)
        {
            if (!map[i][j].isboom)
            //统计附近雷数
                map[i][j].num=
                    map[i][j+1].isboom+map[i+1][j].isboom+map[i][j-1].isboom+
                    map[i-1][j].isboom+map[i-1][j+1].isboom+map[i+1][j-1].isboom+
                    map[i+1][j+1].isboom+map[i-1][j-1].isboom;
            //i-1,j+1 | i,j+1 | i+1,j+1
            // i-1,j  |  i,j  |  i+1,j      <-- (i,j)的八个方向
            //i-1,j-1 | i,j-1 | i+1,j-1
            if (!map[i][j].num)   //如果num==0,那么这个方块就是空地。
                map[i][j].isnum=false;
        }
    }
    int tx=rand()%(N-2)+1,ty=rand()%(M-2)+1;        //随机初始一个点.
    while (map[tx][ty].isboom||map[tx][ty].isnum)   //初始地图
        tx=rand()%(N-2)+1,ty=rand()%(M-2)+1;
    map[tx][ty].isopened=true;
    DFS(tx,ty);
}
void print(bool die)             //打印地图
{
    if (die)
    {
        cout<<"  ";
        for (int i=0;i<N-2;i++) cout<<"- ";
        cout<<'\n';
        for (int i=1;i<N-1;i++)
        {
            cout<<'|';
            for (int j=1;j<M-1;j++)
            {
                if (map[i][j].isboom)     //雷
                    cout<<openboom;
                else if (map[i][j].isnum) //不是空地
                    cout<<map[i][j].num<<' ';
                else                      //空地
                    cout<<nnum;
            }
            cout<<'|'<<'\n';
        }
        cout<<"  ";
        for (int i=0;i<N-2;i++) cout<<"- ";
        cout<<'\n';
        return ;
    }
    cout<<"剩余雷数:"<<NowBoom<<'\n';
    cout<<"  ";
    for (int i=0;i<N-2;i++) cout<<"- ";
    cout<<'\n';
    for (int i=1;i<N-1;i++)
    {
        cout<<'|';
        for (int j=1;j<M-1;j++)
        {
            if (i==x&&y==j)          //指针指向
                cout<<point;
            else if (map[i][j].isflag)     //插旗了
                cout<<flag;
            else if (!map[i][j].isopened) //未打开
                cout<<Cell;
            else                          //打开了
            {
                if (map[i][j].isnum) //不是空地
                    cout<<map[i][j].num<<' ';
                else                      //空地
                    cout<<nnum;
            }
        }
        cout<<'|'<<'\n';
    }
    cout<<"  ";
    for (int i=0;i<N-2;i++) cout<<"- ";
    cout<<'\n';
}
direction Get()          //从键盘读取一个方向
{
    char ch1,ch2;
    ch1=getch();         //读取第一个字符
    if (ch1<'A'||ch1>'Z'||ch1<'a'||ch1>'z')        //是方向键
    {
        ch2=getch();
        switch (ch2)
        {
            case 72:return W; //是上(↑)
            case 80:return S; //是下(↓)
            case 75:return A; //是左(←)
            case 77:return D; //是右(→)
            case 10:          //是选择('\n'或'\r')
                case 13:
                    return E;
            case 9:           //是插旗(Tab)
                return T;
        }
    }
    else                 //是字母
        switch (ch1)
        {
            case 'W':          //是上(W)
                case 'w':
                    return W;
            case 'A':          //是左(A)
                case 'a':
                    return A;
            case 'S':          //是下(S)
                case 's':
                    return S;
            case 'D':          //是右(D)
                case 'd':
                    return D;
            case 'E':          //是选择(E)
                case 'e':
                    return E;
            case 'T':          //是插旗(T)
                case 't':
                    return T;
        }
}
void DFS(int x,int y)         //空地拓展
{
    if (map[x][y].isnum) return ;
    isdfs[x][y]=true;
    //只拓展旁边4个区块 至少探索一个数字
    if (y<M-2&&!isdfs[x][y+1]&&!map[x][y+1].isboom)
    {
        map[x][y+1].isopened=true;
        DFS(x,y+1);
    }
    if (x<M-2&&!isdfs[x+1][y]&&!map[x+1][y].isboom)
    {
        map[x+1][y].isopened=true;
        DFS(x+1,y);
    }
    if (x>1&&!isdfs[x-1][y]&&!map[x-1][y].isboom)
    {
        map[x-1][y].isopened=true;
        DFS(x-1,y);
    }
    if (y>1&&!isdfs[x][y-1]&&!map[x][y-1].isboom)
    {
        map[x][y-1].isopened=true;
        DFS(x,y-1);
    }
}
void menu()              //打印游戏指南
{
    puts("\t\t游戏指南");
    puts("1.用W,A,S,D(或↑↓←→)移动,按E(或Enter)打开方块,按T(或Tab)插旗");
    puts("2.死亡后可再来一局");
    puts("3.不要输入其它字符");
    getch();
    system("cls");       //清屏
}
bool isplay=0;
bool Play()              //开始游戏!!!
{
    init();              //预处理
    start();
    if (!isplay) menu(); //玩过就不给指南了
    isplay=1;
    while (NowBoom&&(N-2)*(M-2)-k!=boom)
    {
        Game:              //游戏阶段
            print(0);            //打印
            direction tmp=Get(); //获取
            switch (tmp)         //操作
            {
                case W:
                {
                    if (x>1) --x;
                    break;
                }
                case A:
                {
                    if (y>1) --y;
                    break;
                }
                case S:
                {
                    if (x<N-2) ++x;
                    break;
                }
                case D:
                {
                    if (y<N-2) ++y;
                    break;
                }
                case E:
                {
                    --k;
                    if (!map[x][y].isboom)
                    {
                        map[x][y].isopened=true;
                        if (!map[x][y].isnum) DFS(x,y);
                    }
                    else return 0;   //死亡
                    break;
                }
                case T:              //插旗
                {
                    map[x][y].isflag=!map[x][y].isflag;
                    if (map[x][y].isflag) --NowBoom;
                    else ++NowBoom;
                    if (NowBoom<0)
                    {
                        NowBoom=0;   //雷数必须>=0.
                        map[x][y].isflag=false;
                    }
                    break;
                }
            }
            system("cls");
    }
    bool flag=true;
    for (int i=1;i<N-1&&flag;i++)    //插完旗子了
    {
        for (int j=1;j<N-1&&flag;j++)//判断是否正确
        {
            if (map[i][j].isboom)
                if (map[i][j].isflag||!map[i][j].isopened);
                else flag=false;
            else
                if (map[i][j].isflag)
                    flag=false;
        }
    }
    if (flag)                        //正确
    {
        cout<<"你赢了!!";
        return 1;
    }
    else                             //错误
    {
        system("cls");
        goto Game;
    }
}
int main()
{
    for (;;)
    {
        clock_t start=clock();
        bool x=Play();
        cout<<"用时:"<<(clock()-start)/1000<<"s\n";
        Sleep(5000);
        system("cls");
        if (!x)
        {
            cout<<"\a\a\a\a\a";
            print(1);
            Sleep(10000);
        }
        cout<<"再来一局?输入(Y)es或(N)o.";
        char p=getch();
        system("cls");
        if (p=='N') break;
    }
    cout<<"游戏结束!";
    return 0;
}
原文