连连看游戏~~~可以直接用鼠标点击两个格子来消去。。。

/**************** 连连看 *****************/
/************** 2012-11-20 ****************/

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#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 data
{
    int x,y,dir,turn;
    bool operator < (const data &c) const
    {
        return c.turn<turn;
    }
};

priority_queue<data> q;
data temp,temp2;
int tempx,tempy,tempdir,tempturn,m,n,sum,x1,x2,yy1,y2;
int map[1010][1010];
int direct[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
bool already[1010][1010];

bool judge()
{
    if (map[tempx][tempy]!=0) return false;
    if (already[tempx][tempy]) return false;
    return true;
}

void push()
{
    temp2.x=tempx;
    temp2.y=tempy;
    temp2.dir=tempdir;
    temp2.turn=tempturn;
    q.push(temp2);
    already[tempx][tempy]=true;
}

bool bfs()
{
    if (x1<0 || x2<0 || yy1<0 || y2<0 || x1>m+1 || yy1>n+1 || x2>m+1 || y2>n+1) return false;
    if (map[x1][yy1]==0 || map[x2][y2]==0) return false;
    if (map[x1][yy1]!=map[x2][y2]) return false;
    temp.x=x1;temp.y=yy1;temp.dir=9999;temp.turn=-1;
    while (!q.empty()) q.pop();
    memset(already,false,sizeof(already));
    q.push(temp);
    already[x1][yy1]=true;
    while (!q.empty())
    {
        temp=q.top();
        for (int i=0;i<=3;i++)
        {
            tempx=temp.x+direct[i][0];tempy=temp.y+direct[i][1];
            tempdir=i;
            if (tempx<0 || tempy<0 || tempx>m+1 || tempy>n+1) continue;
            if (temp.dir==9999 || (temp.dir+i)%2==1) tempturn=temp.turn+1;
            else tempturn=temp.turn;
            if (tempturn>=3) continue;
            if (tempx==x2 && tempy==y2) return true;
            if (!judge()) continue;
            push();
        }
        q.pop();
    }
    return false;
}

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

void creat()
{
    int t=0,a,i,j;
    srand(time(0));
    memset(map,0,sizeof(map));
    while (t!=sum)
    {
        a=(int)random(0,26)+1;
        for (int l=1;l<=2;l++)
        {
            while (1)
            {
                i=(int)random(0,m)+1;
                j=(int)random(0,n)+1;
                if (map[i][j]==0)
                {
                    map[i][j]=a;
                    break;
                }
            }
        }
        t+=2;
    }
}

void print()
{
    char c;
    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=1;i<=m;i++)
    {
        cout << i%10 << "|";
        for (int j=1;j<=n;j++)
        {
            if (map[i][j]!=0)
            {
                c=map[i][j]-1+'a';
                cout << c;
            }
            else cout << " ";
        }
        cout << "|" << endl;
    }
    cout << "  ";
    for (int i=1;i<=n;i++) cout << "-";
    cout << endl;
}

void del()
{
    clearr();
    if (bfs())
    {
        map[x1][yy1]=map[x2][y2]=0;
        sum-=2;
        if (sum!=0)
        {
            cout << "Good job!" << endl << endl;
            print();
        }
    }
    else
    {
        cout << "ERROR!" << endl << endl;
        print();
    }
}

int main()
{
    cout << "-----------------------连连看-------------------------" << endl;
    cout << "请输入两个正整数,表示地图的大小。程序将随机生成地图. " << endl;
    cout << "然后请用鼠标点击两个格子,代表消去这两个." << endl;
    cout << "------------------------------------------------------" << endl;
    cin >> m >> n;
    cout << endl;
    sum=m*n;
    if (sum%2!=0)
    {
        cout << "ERROR" << endl;return 0;
    }
    hide();
    INPUT_RECORD mouseRec;
    DWORD res;
    clearr();
    cout << "Map created!" << endl << endl;
    creat();
    print();
    while (1)
    {
        if (sum==0)
        {
            cout << "Congratulations!" << endl;
            break;
        }
        //cin >> x1 >> yy1 >> x2 >> y2;
        while (1)
        {
            ReadConsoleInput(hin,&mouseRec,1,&res);
            if (mouseRec.EventType==MOUSE_EVENT)
            {
                pos=mouseRec.Event.MouseEvent.dwMousePosition;
                x1=pos.Y-3;yy1=pos.X-1;
                if (mouseRec.Event.MouseEvent.dwButtonState==FROM_LEFT_1ST_BUTTON_PRESSED)
                {
                    if ((x1>=1 && x1<=m && yy1>=1 && yy1<=n && map[x1][yy1]!=0)) break;
                }
            }
        }
        cout << x1 << " " << yy1 << endl;
        while (1)
        {
            ReadConsoleInput(hin,&mouseRec,1,&res);
            if (mouseRec.EventType==MOUSE_EVENT)
            {
                pos=mouseRec.Event.MouseEvent.dwMousePosition;
                x2=pos.Y-3;y2=pos.X-1;
                if (mouseRec.Event.MouseEvent.dwButtonState==FROM_LEFT_1ST_BUTTON_PRESSED)
                {
                    if ((x2>=1 && x2<=m && y2>=1 && y2<=n && (x1!=x2 || yy1!=y2) && map[x2][y2]!=0)) break;
                }
            }
        }
        cout << endl;
        del();
    }
    system("pause");
    return 0;
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值