YUTU——扫雷

懒得注释,直接上代码!

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define YUTUHEIGHT 10
#define YUTUWIDHT 10
#define BOON_NUM 10  

struct square
{
    bool isOpen;    
    int groundBoon;  
};
square map[YUTUHEIGHT][YUTUWIDHT];

void csh()
{
    int *p;

    for (int i = 0;i < YUTUHEIGHT;i++)
        for (int j = 0;j < YUTUWIDHT;j++)
        {
            map[i][j].groundBoon = 0;
            map[i][j].isOpen = false;
        }

    srand(time(NULL));
    p = &map[rand() % YUTUHEIGHT][rand() % YUTUWIDHT].groundBoon;
    *p = -1;
    for(int i=0;i<BOON_NUM-1;i++)
    {
        while (*p == -1)
        {
            p = &map[rand() % YUTUHEIGHT][rand() % YUTUWIDHT].groundBoon;
        }
    *p = -1;
    }

}

void sc()
{
    printf("  ");
    for (int i = 0;i < YUTUWIDHT;i++)
        printf("%d ",i);
    printf("\n");
    for (int i = 0;i < YUTUHEIGHT;i++)
    {
        printf("%d ",i);
        for (int j = 0;j < YUTUWIDHT;j++)
        {
            if (map[i][j].isOpen)
                if (map[i][j].groundBoon==-1)
                    printf("* ");
                else if (map[i][j].groundBoon == 0)
                    printf("  ");
                else
                    printf("%d ", map[i][j].groundBoon);
            else
                printf("# ");
        }
        printf("\n");
    }
    printf("\nYUTU:“有%d个雷”\n",BOON_NUM);
}
void bigOpen(int openH, int openW)
{
    int temp;
    int ground(int openH, int openW); 

    map[openH][openW].groundBoon = 0;
    map[openH][openW].isOpen = true;

    if (openH - 1 >= 0 && openW - 1 >= 0&&map[openH - 1][openW - 1].isOpen == false)
    {
        temp = ground(openH-1, openW-1);
        map[openH - 1][openW - 1].groundBoon = temp;
        map[openH - 1][openW - 1].isOpen = true;
    }
    if (openH - 1 >= 0 && map[openH - 1][openW].isOpen == false)
    {
        temp = ground(openH-1, openW);
        map[openH - 1][openW].groundBoon = temp;
        map[openH - 1][openW].isOpen = true;
    }
    if (openH - 1 >= 0 && openW + 1 < YUTUWIDHT && map[openH - 1][openW + 1].isOpen == false)
    {
        temp = ground(openH-1, openW+1);
        map[openH - 1][openW + 1].groundBoon = temp;
        map[openH - 1][openW + 1].isOpen = true;
    }
    if (openW - 1 >= 0 && map[openH][openW - 1].isOpen == false)
    {
        temp = ground(openH, openW-1);
        map[openH][openW - 1].groundBoon = temp;
        map[openH][openW - 1].isOpen = true;
    }
    if (openW + 1 < YUTUWIDHT && map[openH][openW + 1].isOpen == false)
    {
        temp = ground(openH, openW+1);
        map[openH][openW + 1].groundBoon = temp;
        map[openH][openW + 1].isOpen = true;
    }
    if (openH + 1 < YUTUHEIGHT && openW - 1 >= 0 && map[openH + 1][openW - 1].isOpen == false)
    {
        temp = ground(openH+1, openW-1);
        map[openH + 1][openW - 1].groundBoon = temp;
        map[openH + 1][openW - 1].isOpen = true;
    }
    if (openH + 1 < YUTUHEIGHT && map[openH + 1][openW].isOpen == false)
    {
        temp = ground(openH+1, openW);
        map[openH + 1][openW].groundBoon = temp;
        map[openH + 1][openW].isOpen = true;
    }
    if (openH + 1 < YUTUHEIGHT && openW + 1 < YUTUWIDHT && map[openH + 1][openW + 1].isOpen == false)
    {
        temp = ground(openH+1, openW+1);
        map[openH + 1][openW + 1].groundBoon = temp;
        map[openH + 1][openW + 1].isOpen = true;
    }
}
int ground(int openH, int openW)
{
    int sum = 0;   

    if (openH - 1 >= 0 && openW - 1 >= 0 && map[openH - 1][openW - 1].groundBoon == -1)
        sum++;
    if (openH - 1 >= 0 && map[openH - 1][openW].groundBoon == -1)
        sum++;
    if (openH - 1 >= 0 && openW + 1 < YUTUWIDHT && map[openH - 1][openW + 1].groundBoon == -1)
        sum++;
    if ( openW - 1 >= 0 && map[openH ][openW - 1].groundBoon == -1)
        sum++;
    if (openW + 1 < YUTUWIDHT && map[openH][openW +1].groundBoon == -1)
        sum++;
    if (openH + 1 < YUTUHEIGHT && openW - 1 >= 0 && map[openH +1][openW - 1].groundBoon == -1)
        sum++;
    if (openH + 1 < YUTUHEIGHT  && map[openH+  1][openW ].groundBoon == -1)
        sum++;
    if (openH +1 < YUTUHEIGHT && openW + 1 < YUTUWIDHT && map[openH + 1][openW + 1].groundBoon == -1)
        sum++;
    if (sum == 0)   
        bigOpen(openH, openW);
    return sum;
}

bool open(int openH,int openW)
{
    if (map[openH][openW].isOpen)  
        return false;
    if (map[openH][openW].groundBoon == -1)  
        return true;

    map[openH][openW].groundBoon=ground(openH, openW);
    map[openH][openW].isOpen = true;
    return false;
}

void end()
{
    for (int i = 0;i < YUTUHEIGHT;i++)
        for (int j = 0;j < YUTUWIDHT;j++)
            if (map[i][j].groundBoon == -1)
                map[i][j].isOpen = true;
    sc();
}
bool win()
{
    for (int i = 0;i < YUTUHEIGHT;i++)
        for (int j = 0;j < YUTUWIDHT;j++)
            if (!map[i][j].isOpen && map[i][j].groundBoon != -1)
			return false;
    return true;
}
int main()
{
    bool isEnd=false,isWin=false; 
    int openH, openW;   
    int isAgain;  

    csh();
    while(!isEnd&&!isWin)
    {
        sc();
        printf("请输入要打开的格子行数:");
        scanf_s("%d",&openH);
        while (openH<0||openH>YUTUHEIGHT)
        {
            printf("错误!请输入正确的行数:");
            scanf_s("%d", &openH);
        }
        printf("请输入要打开的格子列数:");
        scanf_s("%d", &openW);
        while (openW<0 || openW>YUTUHEIGHT)
        {
            printf("错误!请输入正确的列数:");
            scanf_s("%d", &openW);
        }
        isEnd=open(openH,openW);
        isWin = win();
        system("cls");
    }
        end();
        if (isWin)
            printf("成功!\n");
        else
            printf("失败!\n");
        printf("输入0重开游戏,输入其他任意数字退出游戏:");
        scanf_s("%d",&isAgain);
        if (isAgain == 0)
        {
            system("cls");
            main();
        }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值