系统相关
首页 > 系统相关> > C++鼠标点击板扫雷(限windows)

C++鼠标点击板扫雷(限windows)

作者:互联网

这是一个c++鼠标点击版本的贪吃蛇小游戏(限windows)

这是本人于2021 9 3花费1个小时的成果

请勿转载,请勿商用,请于复制后24h内删除

稍微有点小问题,就是当时写的时候是先生成雷区再点第一次

这样可能第一次就是雷,但是本人比较懒qwq,所以没有改

其实改一改挺简单的,这里说一下大致方案

在标注有默认八颗雷可以自己改之前先点击一次

在mp里标记出来,然后再运行下面的循环(rep(i,1,num))

在进行一次DFS(往旁边进行地图扩长)

最后print_map就行了,还是很简单的呢! 

 

//本程序属个人劳动成果,如有雷同,纯属巧合(全是手打的,思路也是自己的)
#include<bits/stdc++.h>
#include<windows.h>
#include <conio.h>
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0X8000) ? 1:0)
using namespace std;
HANDLE hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_FONT_INFO consoleCurrentFont;
const int isshow=-1;
int mp[100][100],num,canshow[100][100],now;
int mp_vis[100][100];
int isbj[100][100];
bool tsbj[100][100];
inline void gotoxy(int x,int y) {
    HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X=x;
    pos.Y=y;
    SetConsoleCursorPosition(handle,pos);
    return;
}
void color(int a) {//颜色函数
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void print_num(int n) {
    color(56);
    if(n==1)cout<<"①";
    if(n==2)cout<<"②";
    if(n==3)cout<<"③";
    if(n==4)cout<<"④";
    if(n==5)cout<<"⑤";
    if(n==6)cout<<"⑥";
    if(n==7)cout<<"⑦";
    if(n==8)cout<<"⑧";
    color(7);
}
void print_map() {
    cout<<"by lize yyd 2021.9.3 8:56~9:53\n";
    cout << "--------------------扫雷--------------------" << endl;
    cout << "鼠标点击          左键翻开          右键标记" << endl;
    cout << "--------------------------------------------" << endl;
    cout<<"            ";
    rep(i,1,9)cout<<i<<' ';
    cout<<endl;
    rep(i,1,9) {
        cout<<"          ";
        cout<<i<<' ';
        rep(j,1,9) {
//            cout<<mp[i][j]<<' ';
            if(tsbj[i][j]) {
                color(56);
                cout<<"■";
            } else if(isbj[i][j]) {
                color(25);
                if(isbj[i][j]==1) {
                    cout<<"!";
                } else {
                    cout<<"?";
                }
            } else {
                if(mp[i][j]==9) {
                    color(25);
                    cout<<"■";
                } else {
                    if(mp[i][j]==isshow) {
                        color(56);
                        cout<<"■";
                    } else {
                        if(canshow[i][j]) {
                            print_num(mp[i][j]);
                        } else {
                            color(25);
                            cout<<"■";
                        }
                    }
                }
            }

            color(7);
        }
        cout<<endl;
    }
    cout<<"              剩余雷数:"<<num-now<<endl;
}
void print_gameover(int x,int y) {//游戏结束
    system ("cls");
    cout<<"by lize yyd 2021.9.3 8:56~9:53\n";
    cout << "--------------------扫雷--------------------" << endl;
    cout << "鼠标点击          左键翻开          右键标记" << endl;
    cout << "--------------------------------------------" << endl;
    cout<<"            ";
    rep(i,1,9)cout<<i<<' ';
    cout<<endl;
    rep(i,1,9) {
        cout<<"          ";
        cout<<i<<' ';
        rep(j,1,9) {
//            cout<<mp[i][j]<<' ';
            if(i==x&&j==y)color(64),cout<<"﹡";
            else if(mp[i][j]==9) {
                color(12);
                if(isbj[i][j]) {
                    cout<<"×";
                } else cout<<"﹡";
            } else {
                if(isbj[i][j]) {
                    color(25);
                    if(isbj[i][j]==1) {
                        cout<<"!";
                    } else {
                        cout<<"?";
                    }
                } else {
                    if(mp[i][j]==isshow) {
                        color(56);
                        cout<<"■";
                    } else {
                        if(canshow[i][j]) {
                            print_num(mp[i][j]);
                        } else {
                            color(25);
                            cout<<"■";
                        }
                    }
                }
            }

            color(7);
        }
        cout<<endl;
    }
    Sleep(1000);
    system("cls");
    color(4);
    printf(
        "   _____          __  __ ______     ______      ________ _____  \n"
        "  / ____|   /\\   |  \\/  |  ____|   / __ \\ \\    / /  ____|  __ \\\n"
        " | |  __   /  \\  | \\  / | |__     | |  | \\ \\  / /| |__  | |__) |\n"
        " | | |_ | / /\\ \\ | |\\/| |  __|    | |  | |\\ \\/ / |  __| |  _  / \n"
        " | |__| |/ ____ \\| |  | | |____   | |__| | \\  /  | |____| | \\ \\ \n"
        "  \\_____/_/    \\_\\_|  |_|______|   \\____/   \\/   |______|_|  \\_\\\n"
    );
    color(7);
    exit(0);
}
void print_win() {//你赢了
    system ("cls");
    color(10);
    printf(
        " __     ______  _    _   __          _______ _   _\n"
        " \\ \\   / / __ \\| |  | |  \\ \\        / /_   _| \\ | |\n"
        "  \\ \\_/ / |  | | |  | |   \\ \\  /\\  / /  | | |  \\| |\n"
        "   \\   /| |  | | |  | |    \\ \\/  \\/ /   | | | . ` |\n"
        "    | | | |__| | |__| |     \\  /\\  /   _| |_| |\\  |\n"
        "    |_|  \\____/ \\____/       \\/  \\/   |_____|_| \\_|\n"
    );
    color(7);
    exit(0);
}
const int fx[9]= {0,0,0,1,-1,-1,-1,1,1},fy[9]= {0,1,-1,0,0,1,-1,1,-1};
void dfs(int x0,int y0) {
    if(isbj[x0][y0])return;
    if(x0>9||y0>9||x0<1||y0<1)return;
    if(mp[x0][y0]==9)return;
    canshow[x0][y0]=1;
    if(mp[x0][y0]!=0)return;
    mp[x0][y0]=isshow;
    rep(i,1,8) {
        dfs(x0+fx[i],y0+fy[i]);
    }
}
bool check() {
    int sum=0;
    rep(i,1,9) {
        rep(j,1,9) {
            if(canshow[i][j]) {
                sum++;
            }
        }
    }
    return sum==9*9-num;
}
int mouseclick() {
    while(1) {

    }
}
int main() {
    HWND h=GetForegroundWindow();
    HANDLE hStdin=GetStdHandle(STD_INPUT_HANDLE);
    DWORD mode;
    GetConsoleMode(hStdin,&mode);
    mode&=~ENABLE_QUICK_EDIT_MODE;
    SetConsoleMode(hStdin,mode);
    num=8;//默认8颗雷,可自己更改
    srand(time(NULL));
    rep(i,1,num) {
        int x,y;
        do {
            x=rand()%9+1,y=rand()%9+1;
        } while(mp_vis[x][y]==1);
        mp_vis[x][y]=1;
    }
    rep(i,1,9) {
        rep(j,1,9) {
            rep(k,-1,1) {
                rep(l,-1,1) {
                    mp[i][j]+=mp_vis[i+k][j+l];
                }
            }
            if(mp_vis[i][j])mp[i][j]=9;
        }
    }
    while(1) {
        memset(tsbj,0,sizeof(tsbj));
        gotoxy(0,0);
        print_map();
        cout<<endl<<endl;

//        作弊选项

//        rep(i,1,9){
//            rep(j,1,9){
//                cout<<mp[i][j]<<' ';
//                if(mp[i][j]>=0)cout<<' ';
//            }
//            cout<<endl;
//        }
        cout<<endl;
        int y,x,opt=0;
        while(1) {
            if(KEY_DOWN(VK_LBUTTON)||KEY_DOWN(VK_RBUTTON))break;
        }
        if(KEY_DOWN(VK_LBUTTON)) {
            POINT p;
            GetCursorPos(&p);
            ScreenToClient(h,&p);
            GetCurrentConsoleFont(hOutput,FALSE,&consoleCurrentFont);
            x=p.x/=consoleCurrentFont.dwFontSize.X;
            y=p.y/=consoleCurrentFont.dwFontSize.Y;
            x/=2;
            y-=4;
            Sleep(100);
            int tmp=x;
            x=y;
            y=tmp-5;
            opt=1;
        } else if(KEY_DOWN(VK_RBUTTON)) {
            POINT p;
            GetCursorPos(&p);
            ScreenToClient(h,&p);
            GetCurrentConsoleFont(hOutput,FALSE,&consoleCurrentFont);
            x=p.x/=consoleCurrentFont.dwFontSize.X;
            y=p.y/=consoleCurrentFont.dwFontSize.Y;
            x/=2;
            y-=4;
            Sleep(100);
            int tmp=x;
            x=y;
            y=tmp-5;
            opt=2;
        }
        if(opt==1) {
            if(canshow[x][y]==1&&mp[x][y]!=9&&mp[x][y]!=0) {
                int cntt=0;
                rep(k,-1,1) {
                    rep(l,-1,1) {
                        if(x+k==0||x+k==10||y+l==0||y+l==10)continue;
                        if(k==0&&l==0)continue;
                        if(isbj[x+k][y+l]==1)cntt++;
                        else if(!canshow[x+k][y+l])tsbj[x+k][y+l]=true;
                    }
                }
                if(cntt==mp[x][y]) {
                    rep(k,-1,1) {
                        rep(l,-1,1) {
                            if(k==0&&l==0)continue;
                            if(x+k==0||x+k==10||y+l==0||y+l==10)continue;
                            if(mp_vis[x+k][y+l]&&!isbj[x+k][y+l]) {
                                print_gameover(x+k,y+l);
                            }
                        }
                    }
                    rep(k,-1,1) {
                        rep(l,-1,1) {
                            if(k==0&&l==0)continue;
                            if(x+k==0||x+k==10||y+l==0||y+l==10)continue;
                            dfs(x+k,y+l);
                        }
                    }
                } else {
                    gotoxy(0,0);
                    print_map();
                }
            } else {
                if(canshow[x][y])continue;
                if(mp_vis[x][y]&&isbj[x][y]==0)print_gameover(x,y);
                dfs(x,y);
            }

        } else if(opt==2&&canshow[x][y]==0) {
            if(x<10&&y<10&&x>0&&y>0) {
                if(!isbj[x][y]) {
                    now++;
                    isbj[x][y]=1;
                } else {
                    if(isbj[x][y]==1) {
                        now--;
                        isbj[x][y]=2;
                    } else {
                        isbj[x][y]=0;
                    }
                    Sleep(100);
                }
            }

        }
        if(check())print_win();
        Sleep(35);
    }
    return 0;
}

 

标签:isbj,HANDLE,鼠标,windows,pos,C++,int,100,GetStdHandle
来源: https://www.cnblogs.com/YyD081012/p/16325569.html