C语言实现简易扫雷

game.h
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <Windows.h>
#include <time.h>
#include <stdlib.h>

#define ROW 9         //显示棋盘的大小
#define COL 9
#define ROWS ROW+2    //整个棋盘的大小
#define COLS COL+2

#define COUNT 10      //雷的数量

void chushihua(char arr[ROWS][COLS], int rows, int cols,char set);
void qipanxianshi(char arr[ROWS][COLS], int row, int col);
void buzhilie(char arr[ROWS][COLS], int row, int col);
void kaishipailei(char arr1[ROWS][COLS], char arr2[ROWS][COLS],int row, int col);

text.c

#include "game.h"

void saolie()
{
    char arr1[ROWS][COLS] = { 0 };
    char arr2[ROWS][COLS] = { 0 };
    //创建两个数组,一个放雷,一个显示
    chushihua(arr1, ROWS, COLS, ‘0‘);
    chushihua(arr2, ROWS, COLS, ‘*‘); //初始化两个棋盘
    qipanxianshi(arr2,ROW,COL); //打印一下棋盘
    buzhilie(arr1, ROW, COL);  //放置地雷
    qipanxianshi(arr1, ROW, COL); //测试打印一下带雷的棋盘
    kaishipailei(arr1,arr2,ROW,COL);
}

void meun()
{
    printf("================================\n");
    printf("=========== 1.开  始 ===========\n");
    printf("=========== 0.退  出 ===========\n");
    printf("================================\n");
}

void game()
{
    int input = 0;
    srand((unsigned int)time(NULL));
    do
    {       
        meun();
        printf("请选择:>");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            saolie();
            break;
        case 0:
            printf("游戏结束\n");
            break;
        default:
            printf("输入错误\n");
            break;
        }

    } while (input);
}

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

game.c


#include "game.h"

void chushihua(char arr[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++)
        {
            arr[i][j] = set;
        }
    }
}

void qipanxianshi(char arr[ROWS][COLS], int row, int col)
{
    int i = 0;
    int j = 0;
    for ( i = 0; i <= col; i++)
    {
        printf("%d ", i);
    }
    printf("\n");
    //打印一行的数字,方便玩家找坐标
    for ( i = 1; i <= row; i++)
    {
        printf("%d ", i);
        //打印每一列的数字
        for ( j = 1; j <=col; j++)
        {
            printf("%c ", arr[i][j]);
        }
        printf("\n");
    }
}

void buzhilie(char arr[ROWS][COLS], int row, int col)
{
    int count = COUNT;
    while (count)
    {
        int x = rand() % row + 1;
        int y = rand() % col + 1;
        if (arr[x][y] == ‘0‘)
        {
            arr[x][y] = ‘1‘;
            count--;
        }
    }
}

int tnl(char arr1[ROWS][COLS], int x, int y)
{
    return arr1[x - 1][y] - ‘0‘ + arr1[x][y - 1] - ‘0‘ + arr1[x - 1][y - 1] - ‘0‘ +
        arr1[x + 1][y] - ‘0‘ + arr1[x][y + 1] - ‘0‘ + arr1[x + 1][y + 1] - ‘0‘ +
        arr1[x + 1][y - 1] - ‘0‘ + arr1[x - 1][y + 1] - ‘0‘;
}

void kaishipailei(char arr1[ROWS][COLS], char arr2[ROWS][COLS], int row, int col)
{
    int num1 = 0;
    int num2 = 0;
    int win = 0;
    while (win<row*col - COUNT)   //COUNT参考game.h这是雷的数量
    {
        printf("请输入排查的坐标:>");
        scanf("%d%d", &num1, &num2);
        if (num1 >= 1 && num1 <= 9 && num2 >= 1 && num2 <= 9)
        {
            if (arr1[num1][num2] == ‘1‘)
            {
                printf("游戏结束,你被炸死了!\n");
                break;
            }
            else
            {
                int num3 = tnl(arr1,num1,num2);
                if (arr2[num1][num2]==‘*‘)
                {               
                        arr2[num1][num2] = num3 + ‘0‘;
                        win++;                                          
                }
                else
                {
                    printf("该坐标已经排过雷了\n");
                }
                system("cls");
                qipanxianshi(arr2, ROW, COL);
            }
        }
        else
        {
            printf("坐标不合法,重新输入");
        }
    }
    if (win == row*col - COUNT)
    {
        printf("恭喜你把所有雷都排完了\n");
        printf("雷阵 1为雷\n");
        qipanxianshi(arr1, ROW, COL);
    }
}