C语言实现简易版扫雷游戏

最近用C语言实现了一个简易版的扫雷游戏,感觉很有意思!
效果图:

游戏界面

这里写图片描述

效果图

这里写图片描述

这里写图片描述

代码如下

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


#define ROW 9
#define COL 9
#define ROWS (ROW+2)
#define COLS (COL+2)
#define MAX 10

//初始化
void init_board(char arr[ROWS][COLS], char set, int row, int col)
{
    memset(arr, set, row*col*sizeof(arr[0][0]));

}
//打印棋盘
void display(char arr[ROWS][COLS], int row, int col)
{
    int i = 1, j = 1;
    printf("   ");
    for (i = 1; i <= row - 2; i++)
    {
        printf("%d ", i);
    }
    printf("\n");

    for (i = 1; i <= row - 2; i++)
    {
        printf("%d  ", i);

        for (j = 1; j <= col - 2; j++)
        {
            printf("%c ", arr[i][j]);
        }
        printf("\n");
    }

}

//布雷
void set_boom(char arr[ROWS][COLS])
{
    int count = MAX;

    while (count > 0)
    {
        int x = rand() % 9 + 1;//产生1~9的随机数
        int y = rand() % 9 + 1;

        if (arr[x][y] == '0')
        {
            arr[x][y] = '1';
            count--;//布雷
        }
    }

}

//扫描雷
int  get_boom(char arr[ROWS][COLS], int x, int y)
{
    return arr[x - 1][y - 1] + arr[x - 1][y] +
        arr[x - 1][y + 1] + arr[x][y - 1] +
        arr[x][y + 1] + arr[x + 1][y - 1] +
        arr[x + 1][y] + arr[x + 1][y + 1] - 8 * '0';//计算周围八个位置雷的个数

}

//对某一点进行扩展

void fun(char arr[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{

    if ((x >= 0) && (x <= 11) && (y >= 0) && (y <= 11))//递归约束条件
    {
        if (get_boom(arr, x, y) == 0)//判断雷的个数
        {
            show[x][y] = ' ';
            if (show[x - 1][y - 1] == '*')//对周围八个位置分别进行递归
            {
                fun(arr, show, x - 1, y - 1);
            }
            if (show[x - 1][y] == '*')
            {
                fun(arr, show, x - 1, y);
            }
            if (show[x - 1][y + 1] == '*')
            {
                fun(arr, show, x - 1, y + 1);
            }
            if (show[x][y - 1] == '*')
            {
                fun(arr, show, x, y - 1);
            }
            if (show[x][y + 1] == '*')
            {
                fun(arr, show, x, y + 1);
            }
            if (show[x + 1][y - 1] == '*')
            {
                fun(arr, show, x + 1, y - 1);
            }
            if (show[x + 1][y] == '*')
            {
                fun(arr, show, x + 1, y);
            }
            if (show[x + 1][y + 1] == '*')
            {
                fun(arr, show, x + 1, y + 1);
            }

        }
        else
            show[x][y] = get_boom(arr, x, y) + '0';//如果周围有雷则显示雷的个数

    }
}


void game()
{
    char arr[ROWS][COLS] = { 0 };
    char show[ROWS][COLS] = { 0 };
    int win = 0;
    int x = 0;
    int y = 0;

    init_board(arr, '0', ROWS, COLS);
    init_board(show, '*', ROWS, COLS);
    set_boom(arr);
    display(show, ROWS, COLS);
    while (win != MAX)//判断是否排完雷
    {
        printf("请输入坐标:");
        scanf("%d %d", &x, &y);
        if (((x >= 1) && (x <= ROW)) && ((y >= 1) && (y <= COL)))
        {
            if (arr[x][y] == '1')
            {
                printf("GAME OVER!\n");
                break;
            }
            else
            {
                fun(arr, show, x, y);
                for (int i = 1; i <= 9; i++)
                {
                    for (int j = 1; j <= 9; j++)
                    {
                        if (show[i][j] == '*')
                        {
                            win++;
                        }

                    }
                }

                display(show, ROWS, COLS);

            }
        }
        else
        {
            printf("输入坐标有误\n");
        }
    }
    if (win == MAX)
    {
        printf("CONGRATULATIONS!");
    }
    printf("雷阵如下:\n");
    display(arr, ROWS, COLS);
}

void menu()
{
    printf("******************************\n");
    printf("******1.piay     0.exit*******\n");
    printf("******************************");
}


void test()
{
    int input = 0;
    srand((unsigned int)time(NULL));

    do
    {
        menu();
        printf("请选择:");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 0:
            break;
        default:
            printf("选择错误,请重新选择!");
            break;
        }
    } while (input);

}

int main()
{
    test();

    system("pause");
    return 0;
}

希望可以和大家一起学习,交流!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值