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

来源:互联网 发布:完美dota2 有mac版 编辑:程序博客网 时间:2024/05/22 20:21

最近用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;}

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

原创粉丝点击