笔记-扫雷游戏

头文件(game.h)

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1

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

#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2
#define EASY_COUNT 10//简单模式下雷的总数


void menu();//显示菜单函数
void game();
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);//打印表格
void SetMine(char board[ROWS][COLS], int row, int col);//设置雷的位置
void FindMine(char board1[ROWS][COLS],char board2[ROWS][COLS], int row, int col);//排查雷
int GetMineCount(char board[ROWS][COLS], int x, int y);//统计某坐标周围雷的个数

游戏的函数定义源文件(game.c)

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
    printf("************************\n");
    printf("******   1.play   ******\n");
    printf("******   0.exit   ******\n");
    printf("************************\n");
}
void game()
{

    char mine[ROWS][COLS] = { 0 };
    char show[ROWS][COLS] = { 0 };
    InitBoard(mine, ROWS, COLS, '0');
    InitBoard(show, ROWS, COLS, '*');
    DisplayBoard(show, ROW, COL);
    SetMine(mine, ROW, COL);
    FindMine(mine, show, ROW, COL);
}
void InitBoard(char board[ROWS][COLS],int rows, int cols, char set)
{ 
    //memset(board,set,rows * cols * sizeof(board[0][0]));
    int i = 0, j = 0;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            board[i][j] = set;
        }
    }
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
    int i = 0;
    int j = 0;
    printf("++++++++++ 扫雷 +++++++++\n");
    printf(" ");
    for (i = 1; 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 ", board[i][j]);
        }
        printf("\n");
    }
    printf("+++++++++++++++++++++++++\n");
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
    int count = EASY_COUNT;
    while (count)
    {
        int x = rand() % row + 1;
        int y = rand() % col + 1;
        if (board[x][y] == '0')
        {
            board[x][y] = '1';
            count--;
        }
     }
}
int GetMineCount(char board[ROWS][COLS], int x, int y)
{
    int i = 0, j = 0, count = 0;
    for (i = -1; i <= 1; i++) {
        for (j = -1; j <= 1; j++)
        {
            count += (board[x + i][y + j] - '0');//计算坐标周围雷的个数。
        }
    }
    return count;
}
void FindMine(char board[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    int x = 0, y = 0, win = 0;
    while (win < row * col - EASY_COUNT)
    {
        printf("请输入想要排查的位置坐标\n");
        scanf("%d %d", &x, &y);
            if (x >= 1 && x <= row && y >= 1 && y <= col)
            {
                if (show[x][y] == '*')
                {
                    if (board[x][y] == '1')
                    {
                        printf("成功踩雷,已被炸死!\n");
                        break;
                    }
                    else
                    {
                        win++;//如果不是雷,就++。
                        int count = GetMineCount(board, x, y);
                        show[x][y] = count + '0';
                        DisplayBoard(show, row, col);
                    }
                }
                else printf("该位置已经排查过了,请重新选择其他位置\n");
            }
            else printf("位置越界,请重新输入!");
    }
    if (win == row * col - EASY_COUNT) printf("恭喜你,雷已经全部找到!!!\n");
}

游戏测试源文件(game_test.c)

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
//设置两个二维数组,一个放置自身带雷信息,一个放置周围雷的信息。
//开始打印的第二个数组,全部显示为‘*’,开始扫雷时,显示数字表示周围雷的个数。
//防止统计周围雷的个数时数组越界,我们提前把数组扩大一圈。
//没有雷的地方显示‘0’,有雷的地方显示‘1’。
int main()
{
    srand((unsigned int)time(NULL));
    int input;
    do
    {
        menu();
        printf("请选择:>");
        scanf("%d", &input);
        switch (input)
        {
        case 1: 
            game();
            break;
        case 0:
            break;
        default:
            printf("没有此选项,请重新选择:>");
            break;
        }
    } while (input);
}