扫雷程序完美版!!!!!!可以通过点击鼠标来翻开格子或者做上标记 !!!!!!!

今天中午学会了怎么在控制台程序中使用鼠标。。

 

/**********************扫雷*********************/
/*******************2012-11-20******************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <queue>
#include <conio.h>
#include <windows.h>
using namespace std;

HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE),hin=GetStdHandle(STD_INPUT_HANDLE);
COORD coord,pos;


void hide()
{
    CONSOLE_CURSOR_INFO cursor_info={1,0};
    SetConsoleCursorInfo(hout, &cursor_info);
}

void clearr()
{
    COORD home={0,0};
    DWORD dummy;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(hout,&csbi);
    FillConsoleOutputCharacter(hout,' ',csbi.dwSize.X*csbi.dwSize.Y,home,&dummy);
    SetConsoleCursorPosition(hout,home);
}

struct node
{
    int x,y;
};

queue<node> q;
int map[1010][1010];
int already[1010][1010];
int tempx,tempy,m,n,k,x0,y0,sum;
node temp,temp2;
int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};

bool judge()
{
    if (tempx<0 || tempx>m-1 || tempy<0 || tempy>n-1) return false;
    if (map[tempx][tempy]==-1) return false;
    if (already[tempx][tempy]==0) return false;
    return true;
}

void push()
{
    temp2.x=tempx;
    temp2.y=tempy;
    q.push(temp2);
    already[tempx][tempy]=0;
}

bool bfs()
{
    while (!q.empty())
        q.pop();
    temp.x=x0;temp.y=y0;
    if (already[x0][y0]==0) return true;
    if (map[x0][y0]==-1) return false;
    q.push(temp);
    already[x0][y0]=0;
    while (!q.empty())
    {
        sum++;
        temp=q.front();
        if (map[temp.x][temp.y]==0)
            for (int i=0;i<=7;i++)
            {
                tempx=temp.x+dir[i][0];
                tempy=temp.y+dir[i][1];
                if (judge())
                    push();
            }
        q.pop();
    }
    return true;
}

void print()
{
    cout << "  ";
    for (int i=1;i<=n;i++) cout << i%10;
    cout << endl << "  ";
    for (int i=1;i<=n;i++) cout << "-";
    cout << endl;
    for (int i=0;i<=m-1;i++)
    {
        cout << (i+1)%10 << "|";
        for (int j=0;j<=n-1;j++)
        {
            switch(already[i][j])
            {
            case 0:
                if (map[i][j]==0) cout << " ";
                else
                {
                    if (map[i][j]==-1) cout << "*";
                    else cout << map[i][j];
                }
                break;
            case -1:
                cout << ".";break;
            default:
                cout << "?";
            }
        }
        cout << "|" << endl;
    }
    cout << "  ";
    for (int i=1;i<=n;i++) cout << "-";
    cout << endl;
}

double random(double start, double end)
{
    return start+(end-start)*rand()/(RAND_MAX + 1.0);
}

int judge2(int i,int j)
{
    if (i<0 || i>m-1 || j<0 || j>n-1) return 0;
    if (map[i][j]==-1) return 1;
    return 0;
}

void creat()
{
    srand(time(0));
    int a,b;
    memset(map,0,sizeof(map));
    memset(already,-1,sizeof(already));
    for (int i=1;i<=k;i++)
    {
        while (1)
        {
            a=(int)random(0,m);
            b=(int)random(0,n);
            if (map[a][b]==0)
            {
                map[a][b]=-1;
                break;
            }
        }
    }
    for (int i=0;i<=m-1;i++)
        for (int j=0;j<=n-1;j++)
            if (map[i][j]==0)
                for (int w=0;w<=7;w++)
                    map[i][j]+=judge2(i+dir[w][0],j+dir[w][1]);
}

bool del()
{
    clearr();
    if (!bfs())
    {
        cout << "I'm sorry!" << endl << endl;
        memset(already,0,sizeof(already));
        print();
        return false;
    }
    if (sum!=m*n-k)
        cout << "Good job!" << endl << endl;
    else
    {
        cout << "Congratulations!!" << endl << endl;
        memset(already,0,sizeof(already));
    }
    print();
    return true;
}

int main()
{
    cout << "--------------------扫雷--------------------" << endl;
    cout << "一开始输入三个数,分别表示行数,列数以及雷数" << endl;
    cout << "然后请点击一个格子。左键点开,右键做上标记" << endl;
    cout << "--------------------------------------------" << endl;
    cin >> m >> n >> k;
    hide();
    INPUT_RECORD mouseRec;
    DWORD res;
    int l;
    creat();
    clearr();
    cout << "Map created!" << endl << endl;
    print();
    sum=0;
    while (1)
    {
        if (sum==m*n-k) break;
        l=1;
        while (1)
        {
            ReadConsoleInput(hin,&mouseRec,1,&res);
            if (mouseRec.EventType==MOUSE_EVENT)
            {
                pos=mouseRec.Event.MouseEvent.dwMousePosition;
                x0=pos.Y-4;y0=pos.X-2;
                if (mouseRec.Event.MouseEvent.dwButtonState==FROM_LEFT_1ST_BUTTON_PRESSED)
                {
                    l=1;
                    if ((x0>=0 && x0<=m-1 && y0>=0 && y0<=n-1)) break;
                }
                if (mouseRec.Event.MouseEvent.dwButtonState==RIGHTMOST_BUTTON_PRESSED)
                {
                    l=2;
                    if ((x0>=0 && x0<=m-1 && y0>=0 && y0<=n-1)) break;
                }
            }
        }
        if ((l-1)*(l-2)!=0 || x0<0 || x0>m-1 || y0<0 || y0>n-1)
        {
            clearr();
            cout << "Input ERROR!" << endl << endl;
            print();
        }
        if (l==1)
            if (!del())
                break;
        if (l==2)
        {
            already[x0][y0]=-already[x0][y0];
            clearr();
            cout << "Try it!" << endl << endl;
            print();
        }
    }
    system("pause");
    return 0;
}


 

阅读更多

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