#Beginner must see#Hand in hand to teach you minesweeping tutorial#B Station Pengge is the most detailed in history

foreword

Those who know how to write may not really understand. This article combines the writing techniques of Brother Peng at station B, advances the knowledge points step by step, and teaches you to write code step by step. It is very suitable for beginners to learn, and it is recommended to watch it repeatedly.

At the same time, the author, I am also a beginner of C language station B, welcome to learn and communicate together, criticize and correct.

Family members, come here, use your little hands to get rich, give me a little attention, like me, your support is the driving force for my creation, I will continue to publish high-quality articles for everyone to learn together.

class preparation


First prepare to create a custom header file for declaring functions; two source files, one for writing specific function implementations, and one for writing program procedures. I use the VS2022 compiler, you can use other compilers, but there is no guarantee that there will be other problems, welcome to Q me.

go let's get started! !


Let's start by writing a flowchart that tells your computer what to do?

As a game designer, you have to let your computer understand what it is going to do, that is, how the game works. The flow chart is attached below.

Part 1: Implementation of the menu

a46d2fd143514496905ab14ad35d141a.png

 Part II: Implementation of the game

1172eda61596455aa6e85f094f228c3c.png

Learning to see and draw flowcharts is the best way to learn and develop programming thinking. When we have a flow chart, we can start to write the code by hand and realize the operation of the code. Let's talk about the specific details.

The specific details of the menu implementation


first part:

Create the main() function, print your menu, and make your menu work.

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
#include<stdio.h>
void meun()
{
    printf("********************************\n");
    printf("**********    扫雷    **********\n");
    printf("******1.游戏开始 0.游戏结束******\n");
    printf("********************************\n");
}

void   game()
{
  //第二部分实现
}
int main()
{
    int arr;
    //打印菜单
    meun();
    do {
        printf("请输入—>\n");
        scanf("%d", &arr);
        switch (arr)
        {
        case 1:
            game();
            break;
        case 0:
            printf("你的游戏已经退出\n");
            break;
        default:
            printf("你的输入有错,请重新输入->\n");
            break;
        }
    } while (arr);

}

Some people may not use the menu key of 1 or 0, but use the string key like yes/no, so that the branch of the switch case cannot be used. At this time, the if, else if branch should be used, and when using it You can't use "==" to judge, but use trcmp(arr,"yes")==0 to achieve.

Remember our previous preparations, the header file "game.h" referenced here is defined by itself, and it is used for function declarations.

Take a look at the effect:

e382dccad38e49eca3359d8297394493.png


 Part 2: The implementation details of the game

Create a hidden chessboard array and an explicit chessboard array, and initialize them, first go to the code ->

Then all the second part functions that need to be created are in the void game() function.

   void main()
{
    //布置雷的数组
    char mine[ROWS][COLS] = {0};
    //排查雷的数组
    char show[ROWS][COLS] = {0};
    //数组初始化
    InitBoard(mine,ROWS,COLS,'0');
    InitBoard(show,ROWS,COLS,'*');
}
    

declared in the header file

#include<stdio.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
//数组初始化声明
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);

Function specific implementation (/array initialization)

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

We want to create a 9*9 chessboard, but we create an 11*11 array, why?

22838f4046f8461a99dd5ae8ff28079a.png

 The above picture explains, as shown in the picture, first of all, the place we need to operate is the big red square area, and the printed area is also the big red square area. Here, mines will be mined, demined, and mines will be displayed. However, what is the area outside the big red square area for? Have you seen the small red square area in the upper left corner? If you want to check the number of mines around No. 1, you need to add up the number of mines around No. 1 and display it on No. 1, like No. 1 It is very complicated to calculate the number of mines at the points close to the red line of the big red square area without the outer grid. With the outer grid, the special area close to the big red square area can be like the big red square area when checking for mines. It is simpler to use the same investigation method for all points in the test.

print explicit checkerboard

create function

 //打印显性棋盘
    Dispiayboard(show, ROW, COL);

print explicit checkerboard statement

//打印显性棋盘声明
void  Dispiayboard(char mine[ROWS][COLS], int row, int col);

function implementation

//打印显性棋盘
void  Dispiayboard(char board[ROWS][COLS], int row, int col)
{
    int i, j;
    for (i = 0; i <= col; i++)
    {
        printf("%d ", i);
    }
    printf("\n");
    printf("---------------------\n");
    for (i = 1; i <= row; i++)
    {
        printf("%d", i);
        printf("|");
        for (j = 1; j <= col; j++)
        {
            printf("%c ", board[i][j]);
        }
        printf("\n");
    }
    printf("---------------------\n");
}

What is printed is the 9*9 area, that is, the big red square area, and if is solved very well, and its condition ignores the area outside the big red square area. What is printed is the explicit chessboard.

Take a look at the effect:

572973885f5a4e14bf44f53341311576.png


mine

create function

 //埋雷
    set_mine(mine, ROW, COL);
    //调试时看看雷的布置结果,后面要删
    Dispiayboard(mine, ROW, COL);

A print function is called here to check the status of mines

Mining statement

//埋雷声明
void set_mine(char mine[ROWS][COLS], int row, int col);

function implementation

void set_mine(char mine[ROWS][COLS], int row, int col)
{
    int x, y;
    int count = 11;
    srand((unsigned int)time(NULL));
    while (count)
    {
        x = rand() % row + 1;
        y = rand() % col + 1;
        if ('0' == mine[x][y])
        {
            mine[x][y] = '1';

        }
        count--;
    }
}

Here, 1 is used instead of mines, count is the number of mines arranged, and rand is a library function that generates random 1-1024 numbers. Here, % row +1 or % col +1 is used to control its random value at 1 Between -9, at the same time, in order to ensure that the random value is different every time the program is started, the area of ​​​​the mines arranged is different, it will be used together with the srand function, as described above. Because the library function needs to refer to the header file, the header file here is #include<time.h> and #include <cstdlib>

Take a look at the effect:

71ccbdff85a040f0b4c7d9df68b209e0.png


demining

First block the printing function used for debugging, otherwise it is just to see the answer.

create function

 //排雷
    find_mine(mine, show, ROW, COL);

Here is a parameter with four values, which is the operation of developing the chessboard.

mine clearance statement

//排雷
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

function implementation

int  Get_mine_count(char mine[ROWS][COLS], int x, int y)
{
    return  mine[x - 1][y - 1] +
        mine[x - 1][y] +
        mine[x - 1][y + 1] +
        mine[x][y + 1] +
        mine[x][y - 1] +
        mine[x + 1][y - 1] +
        mine[x + 1][y + 1] +
        mine[x + 1][y] - 8 * '0';
}
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    int x, y;
    while (1)
    {
        printf("请输入坐标->\n");
        scanf("%d%d", &x, &y);
        if (x >= 1 && x <= 9 && y <= 9 && y >= 0)
        {
            if (mine[x][y] == '1')
            {
                printf("你被炸死了\n");
                Dispiayboard(mine, ROW, COL);
                break;
            }
            else
            {
                int count = Get_mine_count(mine, x, y);
                show[x][y] = count + '0';
                Dispiayboard(show, ROW, COL);
            }
        }
        else {
            printf("请输入正确的坐标->\n");
        }
    }

}

The principle of the Get_mine_count function is to calculate the mines around the demining point, because the value of the mine is set to 1, and it will be fine to add up. (Here is the benefit of the 11*11 array)

In the find_mine function, the two ifs have two functions, one is to judge whether the value you input is correct, and the other is to judge whether the point is 1, that is, whether it is mine. Then regardless of winning or losing, the hidden chessboard will be printed at the end, and the answer will be announced. The role of Dispiayboard.

Take a look at the effect:


 

I beg my family members to give me three consecutive links. I will continue to publish some articles suitable for beginners, so that everyone can avoid detours and avoid the pits I stepped on. Please. 

Guess you like

Origin blog.csdn.net/2301_77479336/article/details/130009757