扫雷

· · 个人记录

注意:按下打开,长按超过0.3秒标记

注意,调格式为方阵8*16

#include<cstdio>
#include<cstdlib>
#include<windows.h>
#include<cstring>
#include<ctime>
#include<conio.h>
using namespace std;
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
HWND hConsole = FindWindow("ConsoleWindowClass", NULL);
POINT pt;
void start();
void Goto(int X,int Y){
    COORD C;
    C.X=X;
    C.Y=Y; 
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),C);
}
void HideCursor(){
    CONSOLE_CURSOR_INFO cursor_info={1,0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void colset(int a){
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
char fh[11]={'-','1','2','3','4','5','6','7','8','*','#'};
char dr,xxx[2];
int li,co,mi,jjbb[5],l[26][32],xx,yy,xiao;
bool hi[26][32]={false},ng[26][32];
int di;
int qwe[9]={126,120,121,115,127,115,122,123,115};
time_t t1,t2; 
void print(int a){
    if(a==0)    printf("□");
    if(a==1)    printf("①");
    if(a==2)    printf("②");
    if(a==3)    printf("③");
    if(a==4)    printf("④");
    if(a==5)    printf("⑤");
    if(a==6)    printf("⑥");
    if(a==7)    printf("⑦");
    if(a==8)    printf("⑧");
}
void lose(){
    colset(qwe[0]);
    mi=0;
    system("cls");
    int ttjj=0;
    for(int i=0;i<=li+1;i++){
        for(int j=0;j<=co+1;j++){
            if(i==0||i==li+1||j==0||j==co+1){
                colset(121);
                printf("■");
                continue;
            }
            SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
            if(i==xx&&j==yy)    colset(112);
            if(l[i][j]!=9){
                int su=0;
                for(int k=i-1;k<i+2;k++){
                    for(int ll=j-1;ll<j+2;ll++){
                        if((k!=i||ll!=j)&&l[k][ll]==9)  ++su;
                    }
                }
                colset(qwe[su]);
                print(su);
            }
            else{
                colset(124);
                if(i==xx&&j==yy)    colset(112);
                printf("◎");
                ++mi;
            }
            if(hi[i][j])    ++ttjj;
        }
        printf("\n");
    }
    colset(qwe[0]);
    printf("You LOSE!\nYou play %d seconds\n",di);
    printf("Press any key to continue...\n");
    if(ttjj==0) jjbb[2]=1;
    jjbb[4]=jjbb[3];
    jjbb[3]=0;
    ++jjbb[0];
    getch();
    dr=0;
    start();
}
int s;
bool sou(){
    if(mi!=0)   return false;
    for(int i=1;i<=li;i++){
        for(int j=1;j<=co;j++){
            if(ng[i][j]==true && l[i][j]!=9)    return false;
        }
    }
    return true;
}
void so(int x,int y){
    s=0;
    if(l[x][y]==9)  return;
    for(int i=x-1;i<x+2;i++){
        for(int j=y-1;j<y+2;j++){
            if(l[i][j]==9)  ++s;
        }
    }
    --xiao;
    hi[x][y]=true;
    Goto(2*y,x);
    colset(qwe[s]);
    print(s);
    if(s==0){
        for(int i=x-1;i<x+2;i++){
            for(int j=y-1;j<y+2;j++){
                if(!hi[i][j]&&i>0&&j>0&&i<=li&&j<=co)   so(i,j);
            }
        }
    }
}
void ks(){
    memset(l,0,sizeof(l));
    memset(hi,0,sizeof(hi));
    memset(ng,0,sizeof(ng));
    for(int i=1;i<=li;i++){
        for(int j=1;j<=co;j++)  l[i][j]=10;
    }
    xiao=li*co-mi;
    for(int i=0;i<mi;i++){
        do{
            xx=rand()%li+1;
            yy=rand()%co+1;
        }while(l[xx][yy]==9);
        l[xx][yy]=9;
    }
    xx=1;yy=1;
    colset(121);
    system("cls");
    for(int i=0;i<=li+1;i++){
        for(int j=0;j<=co+1;j++){
            if(i==0||i==li+1||j==0||j==co+1){
                colset(121);
               printf("■");
                continue;
            }
            if(hi[i][j]){
                int su=0;
                for(int k=i-1;k<i+2;k++){
                    for(int ll=j-1;ll<j+2;ll++){
                        if((k!=i||ll!=j)&&l[k][ll]==9)  ++su;
                    }
                }
                SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
                if(i==1&&j==1)  colset(121);
                print(su);
            }
            else{
                colset(qwe[0]);
                if(i==1&&j==1)  colset(112);
                printf("■");   
            }
            colset(qwe[0]);
        }
        printf("\n");
    }
    colset(qwe[0]);
    printf("USE MOUSE TO MOVE \nPRESS TO CHOOSE\nLONG PRESS TO TICK\nmine:%5d  time:%4d",mi,0);
    time(&t1);
    int yyy,xxx;
    bool zj;
    clock_t t3,t4;
    while(xiao!=0){
        if(sou())   break;
        Sleep(10);
        time(&t2);
        di=difftime(t2,t1);
        Goto(17,li+5);
        colset(121);
        printf("%5d",di);
        zj=false;
        while(KEY_DOWN(MOUSE_MOVED)){
            if(!zj) t3=clock();
            zj=true;
        }
        if(zj){
            t4=clock();
            double dii=double(t4-t3)/CLOCKS_PER_SEC;
            GetCursorPos(&pt);
            ScreenToClient(hConsole, &pt);
            yy=pt.x/16;
            xx=pt.y/16;
            if(dii>=0.3){
                if(ng[xx][yy]==false){
                    ng[xx][yy]=true;
                    --mi;
                }
                else{
                    ng[xx][yy]=false;
                    ++mi;
                }
                Goto(5,li+5);
                colset(qwe[0]);
                printf("%5d",mi);
            }
            else if(!hi[xx][yy]){
                if(l[xx][yy]==9){
                    lose();
                    return;
                }
                else    so(xx,yy);
            }
        }
        if(zj)  continue;
        GetCursorPos(&pt);
        ScreenToClient(hConsole, &pt);
        yyy=pt.x/16;
        xxx=pt.y/16;
        if(xx==xxx&&yy==yyy)    continue;
        if(xxx<=0||xxx>li||yyy<=0||yyy>co)  continue;
        Goto(2*yy,xx); 
        if(hi[xx][yy]){
            int tj=0;
            for(int i=xx-1;i<xx+2;i++){
                for(int j=yy-1;j<yy+2;j++){
                    if(l[i][j]==9)  ++tj;
                }
            }
            colset(qwe[tj]);
            print(tj);
        }
        else    if(ng[xx][yy]){
            colset(125);
            printf("¤");
        }
        else{
            colset(qwe[0]);
            printf("■");   
        }
        if(sou())   break;
        xx=xxx;
        yy=yyy;
        Goto(2*yy,xx);
        if(hi[xx][yy]){
            int tj=0;
            for(int i=xx-1;i<xx+2;i++){
                for(int j=yy-1;j<yy+2;j++){
                    if(l[i][j]==9)  ++tj;
                }
            }
            colset(112);
            print(tj);
        }
        else    if(ng[xx][yy]){
            colset(112);
            printf("¤");
        }
        else{
            colset(112);
            printf("■");
        }
    }
    system("cls");
    mi=0;
    for(int i=0;i<=li+1;i++){
        for(int j=0;j<=co+1;j++){
            if(i==0||i==li+1||j==0||j==co+1){
                colset(121);
                printf("■");
                colset(112);
                continue;
            }
            if(l[i][j]!=9){
                int su=0;
                for(int k=i-1;k<i+2;k++){
                    for(int ll=j-1;ll<j+2;ll++){
                        if((k!=i||ll!=j)&&l[k][ll]==9)  ++su;
                    }
                }
                colset(qwe[su]);
                if(i==xx&&j==yy)    colset(112);
                print(su);
            }
            else{
                colset(124);
                if(i==xx&&j==yy)    colset(112);
                printf("◎");
                ++mi;
            }
        }
        printf("\n");
    }
    colset(qwe[0]);
    printf("You WIN!\nYou use %d seconds\n",di);
    printf("Press any key to continue...\n");
    getch();
    ++jjbb[1];
    ++jjbb[0];
    ++jjbb[3];
    dr=0;
}
void jb(){
    colset(121);
    system("cls"); 
    printf("Trophy\n");
    if(jjbb[0]==100){
        colset(126);
        printf("\nFAN OF SWEEPING\nGolgen\nplay 100 games\n");
    }
    else if(jjbb[0]>=70){
        colset(120);
        printf("\nFAN OF SWEEPING\nSilver NEXT: Golden\nplay %d/100 games\n",jjbb[0]);
    }
    else if(jjbb[0]>=30){
        colset(116);
        printf("\nFAN OF SWEEPING\nCopper NEXT: Silver\nplay %d/70 games\n",jjbb[0]);
    }
    else{
        colset(121);
        printf("\nFAN OF SWEEPING\nNaN NEXT: Copper\nplay %d/30 games\n",jjbb[0]);
    }
    if(jjbb[1]==50){
        colset(126);
        printf("\nWIN!WIN!WIN!\nGolgen\nwin 50 games\n");
    }
    else if(jjbb[1]>=35){
        colset(120);
        printf("\nWIN!WIN!WIN!\nSilver NEXT: Golden\nwin %d/50 games\n",jjbb[1]);
    }
    else if(jjbb[1]>=15){
        colset(116);
        printf("\nWIN!WIN!WIN!\nCopper NEXT: Silver\nwin %d/35 games\n",jjbb[1]);
    }
    else{
        colset(121);
        printf("\nWIN!WIN!WIN!\nNaN NEXT: Copper\nwin %d/15 games\n",jjbb[1]);
    }
    if(jjbb[2]==1){
        colset(126);
        printf("\nFIRST BLOOD!\nGolgen\nChoose a mine at the first time\n");
    }
    else{
        colset(121);
        printf("\nFIRST BLOOD!\nNaN NEXT: Golgen\nChoose a mine at the first time\n");
    }
    int ng=jjbb[4];
    if(jjbb[3]>ng){
        jjbb[4]=jjbb[3];
        ng=jjbb[3];
    }
    if(ng==5){
        colset(126);
        printf("\nPERSVERANCE PREVAILS\nGolgen\nstraight win 5 games\n");
    }
    else if(ng>=3){
        colset(120);
        printf("\nPERSVERANCE PREVAILS\nSilver NEXT: Golden\nstraight win %d/5 games\n",ng);
    }
    else if(ng>=2){
        colset(116);
        printf("\nPERSVERANCE PREVAILS!\nCopper NEXT: Silver\nstraight win %d/3 games\n",ng);
    }
    else{
        colset(121);
        printf("\nPERSVERANCE PREVAILS\nNaN NEXT: Copper\nstraight win %d/2 games\n",ng);
    }
    colset(112);
    printf("\nPRESS ANY KEY TO CONYINUE...\n");
    dr=getch();
    dr=0;
}
void zdy(){
    colset(121);
    do{
        system("cls");
        printf("Press lines(>=9,<=24)\n");
        scanf("%d",&li);
    }while(li<9||li>24);

    do{
        system("cls");
        printf("Press columns(>=9,<=30)\n");
        scanf("%d",&co);
    }while(co<9||co>30);
    do{
        system("cls");
        printf("Press mines(>=9,<=%d)\n",(li*co)*7/10);
        scanf("%d",&mi);
    }while(mi<9||mi>(li*co)*7/10);
    do{
        colset(121);
        system("cls");
        printf("Press your personal choose key(LETTER)\nE.g.\n");
        colset(127);
        printf("■■■    ■■■\n");
        printf("■");
        colset(112);
        printf("■");
        colset(127);
        printf("■ to ■");
        colset(qwe[2]);
        printf("①");
        colset(127);
        printf("■\n■■■    ■■■\n");
        xxx[0]=getch();
        if(xxx[0]<'a')  xxx[0]=xxx[0]-'A'+'a';
    }while(xxx[0]<'a'||xxx[0]>'z');
    do{
        colset(121);
        system("cls");
        printf("Press your personal tick key(LETTER)\nE.g.\n");
        colset(127);
        printf("■■■    ■■■\n");
        printf("■");
        colset(112);
        printf("■");
        colset(127);
        printf("■ to ■");
        colset(125);
        printf("¤");
        colset(127);
        printf("■\n■■■    ■■■\n");
        xxx[1]=getch();
        if(xxx[1]<'a')  xxx[1]=xxx[1]-'A'+'a';
    }while(xxx[1]==xxx[0]||xxx[1]<'a'||xxx[1]>'z');
    dr=0;
    return;
}
void start(){
    colset(121);
    system("cls");
    printf("Welcome to mine sweeping!\n");
    printf("Press these keys...\n");
    printf("'S' to start this game\n");
    printf("'T' to look about trophy\n");
    printf("'C' to custom the map\n");
    printf("'E' for easy mode\n");
    printf("'M' for middle mode\n");
    printf("'H' for hard mode\n"); 
    printf("%d lines,%d columns,%d mines\n",li,co,mi);
    dr=getch();
    while(dr!='s'&&dr!='t'&&dr!='c'
        &&dr!='e'&&dr!='m'&&dr!='h')    dr=getch();
    if(dr=='s') ks();
    if(dr=='t') jb();
    if(dr=='c') zdy();
    if(dr=='e'){
        li=9;
        co=9;
        mi=9;
        ks();
    }
    if(dr=='m'){
        li=16;
        co=16;
        mi=40;
        ks(); 
    }
    if(dr=='h'){
        li=16;
        co=30;
        mi=99;
        ks();
    }
    return;
}
int main(){
    HideCursor();
    srand(time(NULL));
    li=9;co=9;mi=9;
    xxx[0]='q';
    xxx[1]='e';
    while(1)    start();
    return 0;
}

函数

HideCursor

Goto

_kbhit()

time

SetConsoleTextAttribute

GetCursorPos