头文件

#pragma once

#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10

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

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void Display(char board[ROWS][COLS], int rows, int cols);
void SetMine(char board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int  col);

函数实现

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            board[i][j] = set;
        }
    }
}

void Display(char board[ROWS][COLS], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < row+1; i++)
    {
        for (j = 0; j < col+1; j++)
        {
            if (i != 0 && j != 0)
            {
                printf("%c ", board[i][j]);
            }
            if (i == 0)
            {
                printf("%d ", j);
            }
            if (j == 0 && i != 0)
            {
                printf("%d ", i);
            }
        }

        printf("\n");

    }
}

void SetMine(char board[ROWS][COLS], int row, int col)
{
    int count = EASY_COUNT;
    int x = 0;
    int y = 0;

    while (count)
    {
        x = rand() % 9 + 1;
        y = rand() % 9 + 1;
        if (board[x][y] == '0')
        {
            board[x][y] = '1';
        }
        else
            continue;
        count--;
    }
}

char get_mine_count(char mine[ROWS][COLS], int x, int y)
{
    return mine[x][y - 1] +
        mine[x + 1][y - 1] + 
        mine[x + 1][y] +                     
        mine[x + 1][y + 1] + 
        mine[x][y + 1] +
        mine[x - 1][y + 1] +
        mine[x - 1][y] +
        mine[x - 1][y - 1] - 8 * '0';
}

/*void Extend(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y)
{
    int count = 0;
    int i = 0;
    for (i = 1;; i++)
    {
        if (mine[x][y - i] ==
            mine[x + i][y - i] ==
            mine[x + i][y] ==
            mine[x + i][y + i] ==
            mine[x][y + i] ==
            mine[x - i][y + i] ==
            mine[x - i][y] ==
            mine[x - i][y - i] == '0')
        {
            show[x][y - i] = '0';
            count = get_mine_count(mine, x, y-i);
            show[x][y-i] = count + '0';

            show[x + i][y - i] = '0';
            count = get_mine_count(mine, x+i, y-i);
            show[x + i][y - i] = count + '0';

            show[x + i][y] = '0';
            count = get_mine_count(mine, x+i, y);
            show[x + i][y] = count + '0';

            show[x + i][y + i] = '0';
            count = get_mine_count(mine, x+i, y+i);
            show[x + i][y + i] = count + '0';

            show[x][y + i] = '0';
            count = get_mine_count(mine, x, y);
            show[x][y + i] = count + '0';

            show[x - i][y + i] = '0';
            count = get_mine_count(mine, x-i, y+i);
            show[x - i][y + i] = count + '0';

            show[x - i][y] = '0';
            count = get_mine_count(mine, x-i, y);
            show[x - i][y] = count + '0';

            show[x - i][y - i] = '0';
            count = get_mine_count(mine, x-i, y-i);
            show[x - i][y - i] = count + '0';

            Extend(mine, show, x, y);
        }
        else
        {
            break;
        }
    }
    //count = get_mine_count(mine, x, y);
    //show[x][y] = count + '0';
}*/
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int  col)
{
    int x = 0;
    int y = 0;
    int count = 0;
    int win = 0;
    while (win<row*col-EASY_COUNT)
    {
        printf("请输入想要排查的坐标:>  ");
        scanf("%d%d", &x, &y);
        if (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            if (mine[x][y] == '1')
            {
                printf("雷区蹦迪呢宝贝er,很遗憾你死了!!\n");
                Display(mine, ROW, COL);
                break;
            }
            else
            {
                //Extend(mine, show, x, y);
                count = get_mine_count(mine, x, y);
                show[x][y] = count + '0';               
                Display(show, ROW, COL);
                win++;
            }
            if (win == row * col - EASY_COUNT)
            {
                printf("恭喜你,你赢了\a\n");
                Display(mine, row, col);
            }
        }
        else
        {
            printf("非法坐标,请重新输入\a\n");
        }
    }
}
//递归 实现扫雷的展开 

主函数

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void game()
{
    //雷的信息存储
    //1.布置好的雷的信息
    char mine[ROWS][COLS] = { 0 };
    //2.排查出的雷的信息
    char show[ROWS][COLS] = { 0 };
    //数组的初始化
    InitBoard(mine, ROWS, COLS,'0');
    InitBoard(show, ROWS, COLS,'*');    
    //打印棋盘
    //Display(mine, ROW, COL);
    Display(show, ROW, COL);
    //布置雷
    SetMine(mine, ROW, COL);
    //Display(mine, ROW, COL);
    //扫雷
    FindMine(mine,show,ROW,COL);
}

void menu()
{
    printf("****************************\n");
    printf("*******    0.Exit    *******\n");
    printf("*******    1.Play    *******\n");
    printf("****************************\n");

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

        printf("请选择是否开始游戏:>\n");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            {
                printf("Game Start!!!\n");
                game();
                break;
            }   
        case 0:
            {
                printf("退出游戏\n");
                break;
            }
        default:
            {
                printf("非法输入,请重新选择>:\n");
            }

        }

    } while (input);
}

int main()
{
    test();
    return 0;
}