跳到正文

个人记录

扫雷小游戏V1.0.0.2

houmy约 13 分钟
原文
文章信息与版本

文章信息

发布
2022年6月20日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 13 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
map<int,pair<int,int> > mp={{0,{15,15}},{1,{3,0}},{2,{9,0}},{3,{2,0}},{4,{10,0}},{5,{14,0}},{6,{6,0}},{7,{12,0}},{8,{4,0}}};
bool board[101][101],revealed[101][101];
int a,b,x,y,lvl=1,m=1,rnd,error,rest,cnt[101][101],marked[101][101];
POINT p;
void setup(){
    memset(board,0,sizeof(board));
    memset(cnt,0,sizeof(cnt));
    memset(marked,0,sizeof(marked));
    memset(revealed,0,sizeof(revealed));
    rest=0;
    for(int i=1;i<=a;i++){
        for(int j=1;j<=b;j++){
            board[i][j]=rand()%5;
            board[i][j]=!board[i][j];
            revealed[i][j]=false;
            if(board[i][j]){
                cnt[i-1][j-1]++;
                cnt[i-1][j]++;
                cnt[i-1][j+1]++;
                cnt[i][j-1]++;
                cnt[i][j+1]++;
                cnt[i+1][j-1]++;
                cnt[i+1][j]++;
                cnt[i+1][j+1]++;
                rest++;
            }

        }
    }
}
void FullScreen(){
    PostMessage(GetForegroundWindow(),WM_SYSCOMMAND, SC_MAXIMIZE,0);
}
void setC(int bg,int fg){
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),bg*16+fg);
}
void display(){
    system("cls");
    cout<<"lvl "<<lvl<<",round "<<rnd+1<<endl;
    cout<<"Click left mouse button to open/mark,and HERE to change mode."<<endl<<"err/rest:";
    setC(0,12);
    cout<<error;
    setC(0,15);
    cout<<"/";
    setC(0,10);
    cout<<rest;
    setC(0,15);
    cout<<" current mode:"<<((m==1)?"open":"mark")<<endl<<"+";
    for(int i=1;i<=a;i++) cout<<"----+";
    cout<<endl;
    for(int i=1;i<=a;i++){
        cout<<"|";
        for(int j=1;j<=b;j++){
            if(revealed[i][j]&&board[i][j]){
                if(i==x&&j==y){
                    setC(15,12);
                    cout<<"^";
                    setC(15,0);
                    cout<<"( )";
                    setC(0,15);
                    cout<<"|";
                }else{
                    setC(15,0);
                    cout<<"`";
                    setC(15,0);
                    cout<<"( )";
                    setC(0,15);
                    cout<<"|";
                }
                continue;
            }
            if(marked[i][j]){
                if(x==i&&y==j) setC(3,15);
                else setC(12,0);
                cout<<" |> ";
                setC(0,15);
                cout<<"|";
                continue;
            }
            if(revealed[i][j]){
                if(x==i&&y==j) setC(15-mp[cnt[i][j]].first,15-mp[cnt[i][j]].second);
                else setC(mp[cnt[i][j]].first,mp[cnt[i][j]].second);
                printf("%-4d",cnt[i][j]);
                setC(0,15);
                cout<<"|";
            }else{
                if(x==i&&y==j) setC(0,15);
                else setC(15,0);
                cout<<"????";
                setC(0,15);
                cout<<"|";
            }
        };
        cout<<endl<<"+";
        for(int i=1;i<=a;i++) cout<<"----+";
        cout<<endl;
    }
}
void dfs(int x,int y){
    if(!board[x][y]) revealed[x][y]=true;
    else return;
    int dx[4]={1,-1,0,0},dy[4]={0,0,-1,1};
    for(int i=0;i<4;i++){
        int x2=x+dx[i],y2=y+dy[i];
        if(x2<1||y2<1||x2>a||y2>b||revealed[x2][y2]) continue;
        if(cnt[x2][y2]==0||(cnt[x][y]==0&&!board[x2][y2])){
            dfs(x2,y2);
        }
    }
}
int main(){
    HANDLE hd=GetStdHandle(STD_INPUT_HANDLE);
    DWORD mode;
    GetConsoleMode(hd,&mode);
    mode&=~ENABLE_QUICK_EDIT_MODE;
    SetConsoleMode(hd,mode);
    HWND h=GetForegroundWindow();
    FullScreen();
    CONSOLE_FONT_INFOEX cfi;
    cfi.cbSize=sizeof(cfi);
    cfi.nFont=0;
    cfi.dwFontSize.X=7;                   // Width of each character in the font
    cfi.dwFontSize.Y=14;                  // Height
    cfi.FontFamily=FF_DONTCARE;
    cfi.FontWeight=FW_NORMAL;
    wcscpy(cfi.FaceName, L"Consolas"); // Choose your font
    SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE),FALSE,&cfi);
    srand(time(0));
    a=b=5;
    while(true){
        x=y=1;
        rnd=0;
        setup();
        while(rest){
            display();
            HANDLE hdOUT=GetStdHandle(STD_OUTPUT_HANDLE);
            CONSOLE_FONT_INFO font;
            GetCurrentConsoleFont(hdOUT,FALSE,&font);
            if((GetAsyncKeyState(VK_LBUTTON) & 0x8000)&&m==1){
                GetCursorPos(&p);
                ScreenToClient(h,&p);
                y=(p.x/font.dwFontSize.X-1)/5+1;
                x=(p.y/font.dwFontSize.Y)/2-1;
                if(x==-1&&y==9){
                	m=3-m;
                	while((GetAsyncKeyState(VK_LBUTTON) & 0x8000)) Sleep(20);
                }
                if(x<1||y<1||x>a||y>b){
                	continue;
				}
                if(marked[x][y]) marked[x][y]=false;
                if(board[x][y]==1){
                    for(int i=1;i<=a;i++){
                        for(int j=1;j<=b;j++) revealed[i][j]=true;
                    }
                    display();
                    cout<<"GAME OVER!!!";
                    Sleep(5000);
                    return 0;
                }else{
                    if(cnt[x][y]==0) dfs(x,y);
                    else revealed[x][y]=true;
                }
                rnd++;
                while((GetAsyncKeyState(VK_LBUTTON) & 0x8000)) Sleep(20);
            }else if((GetAsyncKeyState(VK_LBUTTON) & 0x8000)&&m==2){
                GetCursorPos(&p);
                ScreenToClient(h,&p);
                y=(p.x/font.dwFontSize.X-1)/5+1;
                x=(p.y/font.dwFontSize.Y)/2-1;
                if(x==-1&&y==9){
                	m=3-m;
                	while((GetAsyncKeyState(VK_LBUTTON) & 0x8000)) Sleep(20);
                }
                if(x<1||y<1||x>a||y>b){
                	continue;
                }
                if(marked[x][y]) break;
                if(board[x][y]==1) rest--;
                else error++;
                marked[x][y]=true;
                rnd++;
                while((GetAsyncKeyState(VK_LBUTTON) & 0x8000)) Sleep(20);
            }else while(!((GetAsyncKeyState(VK_LBUTTON) & 0x8000)));
        }
        cout<<"SUCCESS!"<<endl;
        cout<<"Upgrade to lvl "<<lvl+1<<" with "<<rnd<<" moves."<<endl;
        Sleep(5000);
        lvl++;
        a++;
        b++;
    }
    return 0;
}
原文