扫雷

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shadow19990927/article/details/78804319
#include <iostream>
#include <time.h>
#include <conio.h> 
#include <windows.h>
using std::cin;
using std::cout;
using std::endl;

int map[18][18];
int maps[18][18];                               // 0 hide 1 show 2 sign 
int target=0,mine=0;
int X,Y;

void Pos(long x,long y)
{
    HANDLE direct=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos={x,y};
    SetConsoleCursorPosition(direct,pos);
}

void Color(long clr)
{
    HANDLE direct=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(direct,clr);
}

void Show(long judge)
{
    HANDLE direct=GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(direct,&cci);
    cci.bVisible=judge;
    SetConsoleCursorInfo(direct,&cci);  
} 

/*void Check()
{
    Pos(0,20);
    cout<<"                                  ";
    Pos(0,20);
    cout<<X<<"  "<<Y<<"  "<<map[Y][X]<<"   "<<maps[Y][X];
    Pos(X*2,Y);            
}*/ 

void GG()
{
    Pos(X*2,Y);
    Color(0x35);
    cout<<"X ";
    Pos(10,19);
    cout<<"Good Game";
    while(1);
}

void Open(int x,int y)
{
    maps[y][x]=1;
    Pos(x*2,y);
    Color(0x05);
    cout<<map[y][x]<<" ";

    if(map[y][x]==0)
    {
        if(!maps[y+1][x])
            Open(x,y+1);
        if(!maps[y-1][x])
            Open(x,y-1);
        if(!maps[y][x+1])
            Open(x+1,y);
        if(!maps[y][x-1])
            Open(x-1,y);
        if(!maps[y+1][x+1])
            Open(x+1,y+1);
        if(!maps[y+1][x-1])
            Open(x-1,y+1);
        if(!maps[y-1][x+1])
            Open(x+1,y-1);
        if(!maps[y-1][x-1])
            Open(x-1,y-1);
    }
}

void Prt(int Bool)
{
    if(Bool)
        Color(0x35);
    else
        Color(0x05);
    if(maps[Y][X]==1)
        cout<<map[Y][X]<<" ";
    if(maps[Y][X]==2)
        cout<<"Y ";
    if(maps[Y][X]==0)
        cout<<"  ";
}

void Right()
{
    switch(maps[Y][X])
    {
        case 0:maps[Y][X]=2;target++;if(map[Y][X]>8)mine++;break;
        case 2:maps[Y][X]=0;target--;if(map[Y][X]>8)mine--;break;
    }
    Pos(X*2,Y);
    Prt(1);
}

void Left()
{
    if(maps[Y][X]==0)
    {
        if(map[Y][X]>8)
            GG();
        else
            Open(X,Y);
    }
    Pos(X*2,Y);
    Prt(1);
} 

void Move(char key)
{   
    Pos(X*2,Y);
    Prt(0);
    switch(key)
    {
        case 'w':if(Y>1)Y--;break;
        case 's':if(Y<16)Y++;break;
        case 'a':if(X>1)X--;break;
        case 'd':if(X<16)X++;break;
    }
    Pos(X*2,Y);
    Prt(1);
    //Check();
}

int main()
{
    memset(map,0,sizeof(map));
    memset(maps,0,sizeof(maps));

    int x0,y0;
    for(int i=1;i<=40;i++)
    {
        srand(time(0));
        do{
            x0=rand()%16+1;
            y0=rand()%16+1;
          }while(map[x0][y0]>=9);
        map[x0][y0]=9;
        map[x0+1][y0]++;        
        map[x0-1][y0]++;    
        map[x0][y0+1]++;    
        map[x0][y0-1]++;    
        map[x0+1][y0+1]++;  
        map[x0+1][y0-1]++;  
        map[x0-1][y0+1]++;  
        map[x0-1][y0-1]++;      
    }

    Show(0);
    Color(0x10); 
    for(int y=0;y<=17;y++)
    {
        maps[y][0]=maps[y][17]=map[y][0]=map[y][17]=1; 
        Pos(0,y);
        cout<<"  ";
        Pos(34,y);
        cout<<"  ";
    }
    for(int x=1;x<=16;x++)
    {
        maps[0][x]=maps[17][x]=map[0][x]=map[17][x]=1;
        Pos(x*2,0);
        cout<<"  ";
        Pos(x*2,17);
        cout<<"  ";
    }

    Color(0x00); 
    X=8;Y=8;
    Pos(X*2,Y);
    Prt(1);
    //Check();

    char key;
    while(!kbhit())
    {
        key=getch();
        switch(key)
        {
            case 'W':case 'w':Move('w');break;
            case 'S':case 's':Move('s');break;
            case 'A':case 'a':Move('a');break;
            case 'D':case 'd':Move('d');break;
            case 'J':case 'j':Left();break;
            case 'K':case 'k':Right();break;
        }
        if((target==40)&&(mine==40))
        {
            Pos(10,19);
            cout<<"You Win";
            while(1);
        }   
    }
}
阅读更多
换一批

没有更多推荐了,返回首页