扫雷2.01重置版~鼠标操作

2018-6-18-鼠标操作已完成,代码在最下面

想着用鼠标操控来着,然而没弄明白鼠标事件,就先这样吧,有空了在弄

4个模式,简单,一般,困难,自定义= =

我玩一局简单的吧

<*-------------------------------------------------*>
<*---------------[Mine Clearance]------------------*>
<*-------------------------------------------------*>
<*------------------扫雷2.01版---------------------*>
<*-------------------------------------------------*>
<*-------------------[Simple]----------------------*>
<*-------------------[Common]----------------------*>
<*-------------------[Incubi]----------------------*>
<*-------------------[Custom]----------------------*>
<*---------------------------Made-by--revolIA------*>
<*-----------------------------------2018--06--16--*>
输入1---4
1

 

     1  2  3  4  5  6  7  8  9  0
  1  .  .  .  .  .  .  .  .  .  .
  2  .  .  .  .  .  .  .  .  .  .
  3  .  .  .  .  .  .  .  .  .  .
  4  .  .  .  .  .  .  .  .  .  .
  5  .  .  .  .  .  .  .  .  .  .
  6  .  .  .  .  .  .  .  .  .  .
  7  .  .  .  .  .  .  .  .  .  .
  8  .  .  .  .  .  .  .  .  .  .
  9  .  .  .  .  .  .  .  .  .  .
  0  .  .  .  .  .  .  .  .  .  .
     1  2  3  4  5  6  7  8  9  0
  1
  2  1  1
  3  .  1
  4  .  1              1  1  1
  5  .  .  1           1  .  1
  6  .  .  1           1  .  .  1
  7  1  1  .  1  1        1  .  .
  8        1  .  1        1  1  1
  9        1  1  1
  0
     1  2  3  4  5  6  7  8  9  0
  1
  2  1  1
  3  *  1
  4  .  1              1  1  1
  5  .  .  1           1  *  1
  6  .  *  1           1  .  .  1
  7  1  1  .  1  1        1  *  .
  8        1  *  1        1  1  1
  9        1  1  1
  0
Game over!
Continue?1/0

死掉了= =,嘛大概就是这样子
 

 

#include <bits/stdc++.h>
#define ms(a) memset(a,0,sizeof(a))
using namespace std;

char a[233][233];
int b[233][233];
int boom,n,m;
struct point
{
    int x,y;
}P;

void Selection();
void showMenu();
void format();
void Bfs(int,int);
int jude();
void Show();
void play();

int main()
{
    showMenu();
    while(1)
    {
        ms(a),ms(b);
        Selection();
        format();
        play();
        printf("Continue?1/0\n");
        int tx;
        scanf("%d",&tx);
        if(!tx)
            break;
        system("cls");
    }
    return 0;
}

void Selection()
{
    system("cls");
    printf("<*-------------------------------------------------*>\n");
    printf("<*---------------[Mine Clearance]------------------*>\n");
    printf("<*-------------------------------------------------*>\n");
    printf("<*------------------扫雷2.01版---------------------*>\n");
    printf("<*-------------------------------------------------*>\n");
    printf("<*-------------------[Simple]----------------------*>\n");
    printf("<*-------------------[Common]----------------------*>\n");
    printf("<*-------------------[Incubi]----------------------*>\n");
    printf("<*-------------------[Custom]----------------------*>\n");
    printf("<*---------------------------Made-by--revolIA------*>\n");
    printf("<*-----------------------------------2018--06--16--*>\n");
    printf("输入1---4\n");
    int s;
    scanf("%d",&s);
    if(s==1)
        n=m=10,boom=5;
    else if(s<=3)
        n=m=20,boom=50*(s-1);
    else
        printf("长?宽?雷?\n"),scanf("%d%d%d",&m,&n,&boom);
}
void showMenu()
{
    printf("<*-------------------------------------------------*>\n");
    printf("<*---------------[Mine Clearance]------------------*>\n");
    printf("<*-------------------------------------------------*>\n");
    printf("<*-------------------扫雷2.01版--------------------*>\n");
    printf("<*-------------------------------------------------*>\n");
    printf("<*-------------------[Start]-----------------------*>\n");
    printf("<*-------------------------------------------------*>\n");
    printf("<*------------[鼠标点击Start开始游戏]--------------*>\n");
    printf("<*-------------------------------------------------*>\n");
    printf("<*---------------------------Made-by--revolIA------*>\n");
    printf("<*-----------------------------------2018--06--16--*>\n");
    int s,e;
    s=e=time(0);
    while(e-s<2)e=time(0);
}
void format()
{
    srand(time(0));

    for(int i=0;i<boom;i++)
    {
        int tx=rand()%n+1;
        int ty=rand()%m+1;
        if(a[tx][ty]!='*')
            a[tx][ty]='*';
        else
            i--;
    }

    int dir[8][2]={
    1,1,   1,0,   1,-1,
    0,1,          0,-1,
    -1,1,  -1,0, -1,-1
    };
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {

            for(int k=0;k<8;k++)
            {
                int tx=i+dir[k][0];
                int ty=j+dir[k][1];
                if(tx>0 && ty>0 && tx<=n && ty<=m)
                {
                    if(a[tx][ty]=='*')
                        b[i][j]++;
                }
            }
        }
    }
}
void Bfs(int x,int y)
{
    int dir[4][2]={
    1,0, 0,1, -1,0, 0,-1
    };
    queue<point> Q;
    point t1,t2;
    t1.x=x,t1.y=y;
    Q.push(t1);
    while(!Q.empty())
    {
        t2=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            t1.x=t2.x+dir[i][0];
            t1.y=t2.y+dir[i][1];
            if(t1.x>0 && t1.y>0 && t1.x<=n && t1.y<=m)
            {
                if(a[t1.x][t1.y]!='*'&&a[t1.x][t1.y]!='@')
                {
                    a[t1.x][t1.y]='@';
                    if(!b[t1.x][t1.y])
                        Q.push(t1);
                }
            }
        }
    }
}
int jude()
{
    int cnt=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(a[i][j]=='@')
                cnt++;
    if(cnt==n*m-boom)
        return 1;
    return 0;
}
void Show(int flag)
{
    system("cls");
    printf("   ");
    for(int j=1;j<=m;j++)
        printf("%3d",j%10);
    printf("\n");
    for(int i=1;i<=n;i++)
    {
        printf("%3d",i%10);
        for(int j=1;j<=m;j++)
        {
            if(a[i][j]=='@')
            {
                printf("  %c",!b[i][j] ? ' ':'0'+b[i][j]);
            }
            else
            {
                if(flag && a[i][j]=='*')
                    printf("  *");
                else
                    printf("  .");
            }
        }
        printf("\n");
    }
}
void play()
{
    Show(0);
    int x,y;
    scanf("%d%d",&y,&x);
    if(a[x][y]=='*')
    {
        Show(1);
        printf("Game over!\n");
        return;
    }
    if(a[x][y]!='@')
        a[x][y]='@',Bfs(x,y);

    if(jude())
        Show(1),printf("You Win!\n");
    else
        play();
}
#include <bits/stdc++.h>
#include <windows.h>
#define ms(a) memset(a,0,sizeof(a))
using namespace std;

char a[233][233];
int b[233][233];
int boom,n,m,F;
struct point
{
    int x,y;
}P;

void Selection();
void showMenu();
void format();
void Bfs(int,int);
int jude();
void Show(int);
void play(int,int);
int main()
{
    Selection();
    ms(a),ms(b);
    format();
    Show(0);
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);

    CONSOLE_SCREEN_BUFFER_INFO bInfo;
    INPUT_RECORD    mouseRec;
    DWORD           res;
    COORD           crPos, crHome = {0, 0};

    while (1)
    {
        ReadConsoleInput(hIn, &mouseRec, 1, &res);

        if (mouseRec.EventType == MOUSE_EVENT)
        {
            crPos = mouseRec.Event.MouseEvent.dwMousePosition;
            GetConsoleScreenBufferInfo(hOut, &bInfo);
            SetConsoleCursorPosition(hOut, crHome);
            printf("[Cursor Position] X: %2lu  Y: %2lu", (crPos.X+1)/3, crPos.Y);
            SetConsoleCursorPosition(hOut, bInfo.dwCursorPosition);
            if (mouseRec.Event.MouseEvent.dwButtonState==FROM_LEFT_1ST_BUTTON_PRESSED)
            {
                if((crPos.X+1)%3==0&&(crPos.X+1)/3>0&&(crPos.X+1)/3<=m && crPos.Y>0 && crPos.Y<=n)
                {
                    play(crPos.Y,(crPos.X+1)/3);
                    COORD cos = { 0, 0 };
                    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cos);
                    Show(F);
                    if(F==1)
                    {
                        printf("Game over!\n");
                        break;
                    }
                    else if(F==2)
                    {
                        printf("You Win!\n");
                        break;
                    }
                }
            }
        }
    }
    CloseHandle(hOut);  // 关闭标准输出设备句柄
    CloseHandle(hIn);   // 关闭标准输入设备句柄
    return 0;
}

void Selection()
{
    printf("<*-------------------------------------------------*>\n");
    printf("<*---------------[Mine Clearance]------------------*>\n");
    printf("<*-------------------------------------------------*>\n");
    printf("<*------------------扫雷2.01版---------------------*>\n");
    printf("<*-------------------------------------------------*>\n");
    printf("<*-------------------[Simple]----------------------*>\n");
    printf("<*-------------------[Common]----------------------*>\n");
    printf("<*-------------------[Incubi]----------------------*>\n");
    printf("<*-------------------[Custom]----------------------*>\n");
    printf("<*---------------------------Made-by--revolIA------*>\n");
    printf("<*-----------------------------------2018--06--16--*>\n");
    printf("输入1---4\n");
    int s;
    scanf("%d",&s);
    if(s==1)
        n=m=10,boom=5;
    else if(s<=3)
        n=m=20,boom=50*(s-1);
    else
        printf("长?宽?雷?\n"),scanf("%d%d%d",&m,&n,&boom);
}
void format()
{
    srand(time(0));

    for(int i=0;i<boom;i++)
    {
        int tx=rand()%n+1;
        int ty=rand()%m+1;
        if(a[tx][ty]!='*')
            a[tx][ty]='*';
        else
            i--;
    }

    int dir[8][2]={
    1,1,   1,0,   1,-1,
    0,1,          0,-1,
    -1,1,  -1,0, -1,-1
    };
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {

            for(int k=0;k<8;k++)
            {
                int tx=i+dir[k][0];
                int ty=j+dir[k][1];
                if(tx>0 && ty>0 && tx<=n && ty<=m)
                {
                    if(a[tx][ty]=='*')
                        b[i][j]++;
                }
            }
        }
    }
}
void Bfs(int x,int y)
{
    int dir[4][2]={
    1,0, 0,1, -1,0, 0,-1
    };
    queue<point> Q;
    point t1,t2;
    t1.x=x,t1.y=y;
    Q.push(t1);
    while(!Q.empty())
    {
        t2=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            t1.x=t2.x+dir[i][0];
            t1.y=t2.y+dir[i][1];
            if(t1.x>0 && t1.y>0 && t1.x<=n && t1.y<=m)
            {
                if(a[t1.x][t1.y]!='*'&&a[t1.x][t1.y]!='@')
                {
                    a[t1.x][t1.y]='@';
                    if(!b[t1.x][t1.y])
                        Q.push(t1);
                }
            }
        }
    }
}
int jude()
{
    int cnt=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(a[i][j]=='@')
                cnt++;
    if(cnt==n*m-boom)
        return 1;
    return 0;
}
void Show(int flag)
{

    COORD cos = { 0, 0 };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cos);
    for(int i=0;i<999;i++)printf("   ");
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cos);
    for(int j=1;j<=m;j++)
        printf("   ");
    printf("\n");
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(a[i][j]=='@')
            {
                printf("  %c",!b[i][j] ? ' ':'0'+b[i][j]);
            }
            else
            {
                if(flag && a[i][j]=='*')
                    printf("  *");
                else
                    printf("  .");
            }
        }
        printf("\n");
    }
}
void play(int x,int y)
{
    if(a[x][y]=='*')
    {
        F=1;
        return;
    }
    if(a[x][y]!='@')
        a[x][y]='@',Bfs(x,y);
    if(jude())
        F=2;
}

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用ege库实现扫雷图形化界面的鼠标操作代码: ```c++ #include <graphics.h> #include <conio.h> #include <time.h> #include <stdlib.h> #include <stdio.h> #include <Windows.h> #define COLS 30 #define ROWS 16 #define MINES 99 #define BLOCK_SIZE 25 #define WINDOW_WIDTH COLS*BLOCK_SIZE #define WINDOW_HEIGHT ROWS*BLOCK_SIZE int x, y; struct Block { int x, y; int value; bool is_mine; bool is_opened; bool is_flagged; }; Block blocks[ROWS][COLS]; int count_mines(int row, int col) { int count = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { int r = row + i; int c = col + j; if (r >= 0 && r < ROWS && c >= 0 && c < COLS && blocks[r][c].is_mine) { count++; } } } return count; } void draw_block(int row, int col) { setfillcolor(LIGHTGRAY); if (blocks[row][col].is_opened || blocks[row][col].is_flagged) { setfillcolor(WHITE); } fillrectangle(col*BLOCK_SIZE, row*BLOCK_SIZE, (col + 1)*BLOCK_SIZE, (row + 1)*BLOCK_SIZE); if (blocks[row][col].is_opened) { if (blocks[row][col].is_mine) { setfillcolor(RED); solidcircle(col*BLOCK_SIZE + BLOCK_SIZE / 2, row*BLOCK_SIZE + BLOCK_SIZE / 2, BLOCK_SIZE / 2 - 2); } else { settextcolor(BLUE); char str[2] = "\0"; str[0] = blocks[row][col].value + '0'; outtextxy(col*BLOCK_SIZE + BLOCK_SIZE / 2 - 5, row*BLOCK_SIZE + BLOCK_SIZE / 2 - 5, str); } } else if (blocks[row][col].is_flagged) { settextcolor(RED); char str[2] = "\0"; str[0] = 'F'; outtextxy(col*BLOCK_SIZE + BLOCK_SIZE / 2 - 5, row*BLOCK_SIZE + BLOCK_SIZE / 2 - 5, str); } } void draw_board() { cleardevice(); for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { draw_block(row, col); } } } void init_blocks() { for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { blocks[row][col].x = col; blocks[row][col].y = row; blocks[row][col].value = 0; blocks[row][col].is_mine = false; blocks[row][col].is_opened = false; blocks[row][col].is_flagged = false; } } int count = 0; while (count < MINES) { int row = rand() % ROWS; int col = rand() % COLS; if (!blocks[row][col].is_mine) { blocks[row][col].is_mine = true; count++; } } for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { if (!blocks[row][col].is_mine) { blocks[row][col].value = count_mines(row, col); } } } } void open_block(int row, int col) { if (row < 0 || row >= ROWS || col < 0 || col >= COLS || blocks[row][col].is_opened || blocks[row][col].is_flagged) { return; } blocks[row][col].is_opened = true; if (blocks[row][col].is_mine) { return; } if (blocks[row][col].value > 0) { return; } open_block(row - 1, col - 1); open_block(row - 1, col); open_block(row - 1, col + 1); open_block(row, col - 1); open_block(row, col + 1); open_block(row + 1, col - 1); open_block(row + 1, col); open_block(row + 1, col + 1); } void game_over() { settextcolor(RED); char str[20] = "Game Over!"; outtextxy(WINDOW_WIDTH / 2 - 50, WINDOW_HEIGHT / 2 - 5, str); } void game_win() { settextcolor(GREEN); char str[20] = "You Win!"; outtextxy(WINDOW_WIDTH / 2 - 40, WINDOW_HEIGHT / 2 - 5, str); } void game() { bool gameover = false; bool gamewin = false; while (!gameover && !gamewin) { draw_board(); if (kbhit()) { char c = getch(); if (c == ' ') { if (blocks[y][x].is_opened) { open_block(y, x); } else if (blocks[y][x].is_flagged) { blocks[y][x].is_flagged = false; } else { blocks[y][x].is_flagged = true; } } if (c == 'q') { gameover = true; } } if (GetAsyncKeyState(VK_LBUTTON)) { int mx, my; mousepos(&mx, &my); int col = mx / BLOCK_SIZE; int row = my / BLOCK_SIZE; if (row >= 0 && row < ROWS && col >= 0 && col < COLS) { if (!blocks[row][col].is_flagged) { if (blocks[row][col].is_mine) { gameover = true; } else { open_block(row, col); } } } } if (GetAsyncKeyState(VK_RBUTTON)) { int mx, my; mousepos(&mx, &my); int col = mx / BLOCK_SIZE; int row = my / BLOCK_SIZE; if (row >= 0 && row < ROWS && col >= 0 && col < COLS) { if (!blocks[row][col].is_opened) { blocks[row][col].is_flagged = !blocks[row][col].is_flagged; } } } int count_opened = 0; int count_flagged = 0; for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { if (blocks[row][col].is_opened) { count_opened++; } if (blocks[row][col].is_flagged) { count_flagged++; } } } if (count_opened == ROWS*COLS - MINES) { gamewin = true; } Sleep(50); } draw_board(); if (gameover) { game_over(); } else { game_win(); } } int main() { srand(time(NULL)); initgraph(WINDOW_WIDTH, WINDOW_HEIGHT); init_blocks(); draw_board(); game(); getch(); closegraph(); return 0; } ``` 该程序实现了扫雷的基本功能,包括鼠标左键点击方块、鼠标右键标记方块、空白方块自动展开、游戏胜利和游戏失败等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值