Remember a simple minesweeper game

Game overview: Each time you enter a coordinate, you must determine whether the current coordinate is a mine. If not, it will display the current coordinate and the number of mines in the 8 surrounding 8 non-mine coordinate points.
Divided into three parts of code:
game.h

#ifndef _GAME_H_
#define _GAME_H_

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#pragma warning(disable:4996)
#define ROW 10
#define COL 10
#define EASY_COUNT 30

void SetMine(char mine[][COL + 2], int row, int col);
void Display(char board[][COL + 2], int row, int col);
int GetMine(char mine[][COL + 2], int row, int col, int x, int y);
void Reveal(char mine[][COL + 2], char show[][COL + 2], int x, int  y);
void CleanMine(char mine[][COL + 2], char show[][COL + 2], int row, int col, int num);
void game();
#endif

game.c

#include"game.h"

 static int GetRandom(int start, int end)
{
     return rand() % (end - start + 1) + start;

}
void SetMine(char mine[][COL + 2], int row, int col)
{
    int num = EASY_COUNT;
    while (num)
    {
        int x = GetRandom(1, row);
        int y = GetRandom(1, col);
        if (mine[x][y] == '0')
        {
            mine[x][y] = '1';
            num--;
        }
    }
    Display(mine, ROW, COL);

}
void Display(char board[][COL + 2], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0; i <= row; i++)
    {
        printf (" %d  ", i);//打印列号
    }
    printf("\n--------------------------------------------\n");
    for (i = 1; i <= row; i++)
    {
        printf(" %d |", i);//打印行号
        for (j = 1; j <= col; j++)
        {
            printf(" %c |", board[i][j]);
        }
        printf("\n-------------------------------------------\n");
    }
}
int  GetMine(char mine[][COL + 2], int row, int col,int x,int y)
{
    return mine[x][y] = (mine[x - 1][y - 1] - '0')
        + (mine[x - 1][y] - '0')
        + (mine[x - 1][y + 1] - '0')
        + (mine[x][y - 1] - '0')
        + (mine[x][y + 1] - '0')
        + (mine[x + 1][y - 1] - '0')
        + (mine[x + 1][y] - '0')
        + (mine[x + 1][y + 1] - '0');

}
void Expend(char mine[][COL+2], char show[][COL+2], int x, int y, int *num)//扩展式排雷(递归)
{
    int i = 0;
    int j = 0;
    if (show[x][y] == '*')//如果该位置字符为'*',则该位置是未排过的,进行排雷
    {
        (*num)++;//排雷次数加一
        int count = GetMine(mine,ROW+2,COL+2, x, y);//统计该位置周围的雷数
        if (count != 0)//如果该位置周围的雷数不为0
        {
            show[x][y] = count + '0';//显示雷数,则该位置的字符不为'*',可避免下次排雷重复排到该位置
        }
        else//如果该位置的雷数为0,则向它周围八个位置扩展排雷
        {
            show[x][y] = '0';//该位置的字符显示为‘0’
            for (i = -1; i <= 1; i++)
            {
                for (j = -1; j <= 1; j++)
                {
                    if (x + i >= 1 && x + i <= ROW && y + j >= 1 && y + j <= COL)
                    {
                        if (i != 0 || j != 0)//避免重复排到自己
                            Expend(mine, show, x + i, y + j, num);
                    }
                }
            }
        }
    }
}

void CleanMine(char mine[][COL+2], char show[][COL+2], int row, int col, int num)
{
    int win = 0;
    int x = 0;
    int y = 0;
    int count = 0;
    while (win<(ROW*COL - EASY_COUNT))//当排雷的次数不少于无雷格数(雷盘格数 - 雷数)时,停止排雷
    {
        system("CLS");
        Display(show, ROW, COL);
        printf("请输入坐标:");
        scanf("%d%d", &x, &y);
        count++;
        if ((x >= 1) && (x <= ROW) && (y >= 1) && (y <= COL))//检验坐标是否合法
        {
            if (mine[x][y] == '1')
            {
                Display(mine, ROW, COL);//排雷失败后打印一下雷的分布
                printf("你被炸死了!\n");
                return;

            }
            else//如果所选位置没有雷,进行扩展式排雷
            {
                Expend(mine, show, x, y, &win);
            }
            Display(show, ROW, COL);//打印排雷后的雷盘
        }
        else
        {
            printf("输入错误坐标:\n");
        }
    }

}
void game()
{
    char mine[ROW+2][COL+2] = { 0 };//放雷的信息
    char show[ROW+2][COL+2] = { 0 };//显示给玩家的信息
    memset(mine, '0', (ROW + 2)*(COL + 2));//初始化雷盘为'0'
    memset(show, '*', (ROW + 2)*(COL + 2));//初始化显示盘为‘*’
    SetMine(mine, ROW , COL );//布雷
    CleanMine(mine, show, ROW, COL, EASY_COUNT);//扫雷

}

test.c

#include"game.h"

void menu()
{
    printf("*****************************\n");
    printf("****1 play       0 exit *****\n");
    printf("******************************\n");
}
void test()
{
    int select = 0;
    srand((unsigned int)time(NULL));
    do
    {
        menu();
        printf("请选择:");
        scanf("%d", &select);
        switch (select)
        {
        case 1:
            game();
            break;
        case 0:
            exit(1);
        default:
            printf("选择错误,请重新选择");
            break;
        }
    } while (select);
}
int main()
{
    test();
    system("pause");
    return 0;
}

You can also worship the works of the Great God^0^
]( https://blog.csdn.net/c_duoduo/article/details/43889963 )

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324645551&siteId=291194637