图形化扫雷自制版

· · 休闲·娱乐

图形化扫雷自制版

注意!!!你需要先安装easyX图形化头文件,注意修改#include路径!!! 下载

#include <easyx/graphics.h>
#include <bits/stdc++.h>
#include <conio.h>
#pragma GCC optimize(3)

using namespace std;
//#include "DevelopmentPrivate/ui.hpp"
//#include "ui_basic_t.hpp"
typedef pair<int,int> posT;
typedef pair<posT,posT> pos2T;
#define NLLPOS make_pair(0,0)
#define NLLPOS2 make_pair(NLLPOS,NLLPOS)
#define X1(pos2) pos2.first.first
#define Y1(pos2) pos2.first.second
#define X2(pos2) pos2.second.first
#define Y2(pos2) pos2.second.second
typedef pair<int,int> sizeT;
#define NLLSIZE make_pair(0,0)
#define NORM_FONT_SIZE make_pair(10,25)
//
using namespace std;
struct textT{
    bool defined;
    int color=WHITE,backgroundcolor=BLACK;
    posT pos;
    string content="YOUR TEXT HERE",font="Consolas";
    sizeT size=NORM_FONT_SIZE;
    void clear(){
        pos={0,0},size=NORM_FONT_SIZE;
        content="",font="Consolas";
        color=BLACK;
        defined=false;
    }
    void define(posT _pos,sizeT _siz,string _txt,int _col=WHITE,int _bkcol=BLACK,string _font="Consolas"){
        pos=_pos,size=_siz,content=_txt,color=_col,backgroundcolor=_bkcol,font=_font,defined=true;
    }
    void show(){
        if(!defined)return;
        setfont(size.second,size.first,font.c_str());
        settextcolor(color);
        setbkcolor(backgroundcolor);
        outtextxy(pos.first,pos.second,content.c_str());
    }
};

struct bottonT{
    bool defined;
    pos2T pos2;
    int backgroundcolor=YELLOW;
    textT text;
    void aligncenter(){
        int ftX=text.size.first,ftY=text.size.second;
        size_t sz=text.content.size();
        text.pos.first=X1(pos2)+(X2(pos2)-X1(pos2)-ftX*sz)/2,text.pos.second=Y1(pos2)+(Y2(pos2)-Y1(pos2)-ftY)/2;
    }
    void define(pos2T _pos2,int _bkcol,bool _deftxt=0,textT _txt={0}){
        pos2=_pos2,backgroundcolor=_bkcol,text=_txt;
        text.defined=_deftxt;
        aligncenter();
        defined=1;
    }
    void show(){
        if(!defined)return;
        setfillcolor(backgroundcolor);
        fillrectangle(X1(pos2),Y1(pos2),X2(pos2),Y2(pos2));
        aligncenter();
        text.show();
    }
};

//
int a[155][155],maxX,maxY;
bool vis[155][155],used[155][155];
vector<bottonT> btn[155];
void genemap(int _cnt){
    srand(time(0));
    //cnt=maxX*maxY;
    for(int i=0;i<_cnt;i++){
        int x,y;
        while(1){
            x=rand()%maxX,y=rand()%maxY;
            if(a[x][y]!=0)continue;
            else break;
        }
        a[x][y]=-1;
    }
    auto num=[&](int x,int y){
        if(a[x][y]==-1)return -1;
        int cnt=0;
        int dir[8][2]={1,0,0,1,-1,0,0,-1,1,1,-1,-1,-1,1,1,-1};
        for(int i=0;i<8;i++){
            int nx=x+dir[i][0],ny=y+dir[i][1];
            if(nx>=maxX||ny>=maxY||nx<0||ny<0)continue;
            if(a[nx][ny]==-1)cnt++;
        }
        return cnt;
    };
    for(int i=0;i<maxX;i++)
        for(int j=0;j<maxY;j++){
            a[i][j]=num(i,j);
            bottonT t_btn;
            t_btn.define({{i*50,j*50},{i*50+50,j*50+50}},(i+j)%2?YELLOW:RED);
            btn[i].push_back(t_btn);
        }
}
void update(){
    auto lvcolor=[&](int x){
        return x==0?WHITE:x==1?BLUE:x==2?GREEN:x==3?YELLOW:x==4?LIGHTGRAY:x==5?RED:x>6?BLACK:0;
    };
    for(int i=0;i<maxX;i++){
        for(int j=0;j<maxY;j++){
            if(!used[i][j]&&vis[i][j]){
                btn[i][j].backgroundcolor=WHITE;
                btn[i][j].text.content=to_string(a[i][j]);
                btn[i][j].text.size={30,30};
                btn[i][j].text.backgroundcolor=WHITE;
                btn[i][j].text.color=lvcolor(a[i][j]);
                btn[i][j].text.defined=1;
            }
        }
    }
}

bool tag[155][155];

void print(){
    for(int i=0;i<maxX;i++){
        for(int j=0;j<maxY;j++){
            btn[i][j].show();
            if(tag[i][j]){
                setfillcolor(BLUE);
                const int p[]={i*50+25,j*50+10,i*50,j*50+40,i*50+40,j*50+40};
                fillpoly(3,p);
            }
        }
    }
}

void gameover(){
    textT txt1,txt2;
    txt1.define({500,500},NORM_FONT_SIZE,"GAME OVER!",BLACK,RED);
    txt2.define({500,700},NORM_FONT_SIZE,"PRESS ANY KEY TO EXIT...",BLACK,RED);
    txt1.show(),txt2.show();
    getch();
    closegraph();
    exit(0);
}

void gamewin(){
    textT txt1,txt2;
    txt1.define({300,500},NORM_FONT_SIZE,"CONGRATULATIONS! YOU WON!",BLACK,RED);
    txt2.define({500,700},NORM_FONT_SIZE,"PRESS ANY KEY TO EXIT...",BLACK,RED);
    txt1.show(),txt2.show();
    getch();
    closegraph();
    exit(0);
}

void dfs(int x,int y){
    if(vis[x][y])return;
    vis[x][y]=1;
    if(a[x][y]!=0)return;
    int dir[4][2]={1,0,0,1,-1,0,0,-1};
    for(int i=0;i<4;i++){
        int nx=x+dir[i][0],ny=y+dir[i][1];
        if(nx>=maxX||ny>=maxY||nx<0||ny<0)continue;
        dfs(nx,ny);
    }
}
void solve(int x,int y){
    if(a[x][y]==-1)gameover();
    else{
        memset(vis,0,sizeof vis);
        dfs(x,y);
        update();
    }
}

bool win(){
    int cnt=0;
    for(int i=0;i<maxX;i++){
        for(int j=0;j<maxY;j++){
            if(tag[i][j]&&a[i][j]==-1)cnt++;
        }
    }
    return cnt>=10;
}

int main(){
    maxX=10,maxY=10;
    initgraph(1080,800);
    genemap(10);
    while(1){
        print();
        if(MouseHit()){
            MOUSEMSG msg=GetMouseMsg();
            if(msg.mkLButton){
                int x=msg.x,y=msg.y;
                for(int i=0;i<maxX;i++){
                    int ty=-1;
                    for(int j=0;j<maxY;j++){
                        pos2T p=btn[i][j].pos2;
                        int x1_=X1(p),y1_=Y1(p),x2_=X2(p),y2_=Y2(p);
                        if(x1_<=x&&x<=x2_&&y1_<=y&&y<=y2_){
                            ty=j;
                            break;
                        }
                    }
                    if(ty!=-1){
                        solve(i,ty);
                        break;
                    }
                }
            }else if(msg.mkRButton){
                int x=msg.x,y=msg.y;
                for(int i=0;i<maxX;i++){
                    int ty=-1;
                    for(int j=0;j<maxY;j++){
                        pos2T p=btn[i][j].pos2;
                        int x1_=X1(p),y1_=Y1(p),x2_=X2(p),y2_=Y2(p);
                        if(x1_<=x&&x<=x2_&&y1_<=y&&y<=y2_){
                            ty=j;
                            break;
                        }
                    }
                    if(ty!=-1){
                        tag[i][ty]=!tag[i][ty];
                        break;
                    }
                }
            }
            if(win())gamewin();
        }
    }
    printf("END");
    getch();
    return 0;
}