c language-production of simple minesweeper game

1. Principle analysis

1. Analysis and design of the original game

In the Minesweeper game, the simplest one is a 9*9 chessboard. So when we write this program in C language, we must first create a 9*9 chessboard. So how do we create a 9*9 chessboard in C language? At this time we need to create a two-dimensional array with 9 rows and 9 columns.

After creating the array, we need to put elements into the array. In the minesweeper game, when there are mines and there are no mines, we use char type 0’ to represent no mines, and 1’ to represent mines. A good 2D figure should look like this.

After creating the chessboard, we have to think about how to implement mine clearance?

If we check the coordinates (2, 5), we need to visit 8 locations around it and count the number of nearby thunder and lightning. At this time, we can write code to traverse the eight surrounding positions, but when we want to check the first row, the last row, the first column, and the last column, there will be an out-of-bounds situation, which is more troublesome.

At this time, we can solve this problem through a simple method

We solved this problem by adding a frame to the periphery.

Next, in the game, we need to display and print the board for players to watch, so that we can better troubleshoot. We solve this contradiction by creating two two-dimensional arrays. One is the chessboard where the mines are stored, and the other is the chessboard displayed to the players.

2. Basic framework

1. Main function framework

After analyzing the game, let’s try to write a program outline

First of all, the tasks we need to complete in the main function are

1. Printing of menu 2. Choice of starting or exiting the game

We write out a rough framework

int main()
{
   //Print menu
   //Choose to start or exit the game
}

Then we will expand this framework

int main()
{
int input = 0;//First define an input value
do//Because this program will continue as long as the player does not choose to exit, we choose do-while loop
{
menu();//Define a menu function, encapsulate it, and use it multiple times
printf("Please enter your choice:");
scanf_s("%d", & amp;input);
        //Here we will write the content of selecting the game.
} while (input);
return 0;
}

Based on this, we write the function menu()

void menu()
{
printf("******************************\\
");
printf("***** 1.play *****\\
");
printf("***** 0.exit *****\\
");
printf("******************************\\
");
}

According to the menu content, we have to choose to play the game or exit. At this time, we have to use the switch statement to implement the branch selection structure.

int main()
{
int input = 0;
do
{
menu();
printf("Please enter your choice:");
scanf_s("%d", & amp;input);
switch(input)
{
case 1:
{
game();
break;
}
case 0:
{
printf("Exit the game\\
");
break;
}
default:
{
printf("Input error, please re-enter");
break;
}
}

} while (input);
return 0;
}

This is the approximate content of the main function.

2. Game framework

Then how to write the game() game module?

Through the analysis of the game, we found that the process is initialization, placing mines, displaying mines, and detecting mines.

So the framework is as follows

void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];

//Initialize mine table

    //Print the chessboard

    //Thunder
\t
//Demining
\t
}

3. Writing functions

1. Total game function modules

void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];

//Initialize mine table
initboard(mine, ROWS, COLS, '0');
initboard(show, ROWS, COLS, '*');
//Print the chessboard
displayborad(show, ROW, COL);
//Thunder
setmine(mine, ROW, COL);
//displayborad(mine, ROW, COL);
//Demining
findmine(mine, show, ROW, COL);
}

2.Initialization module

We declare the function in the header file game.h, and then write the specific content of the function in the file game.c.

First call the initialization function in the test.c file

There are a total of parameters passed, an array, the number of rows in the array, the number of columns in the array, and initialization content;

Let’s take a look at the content of the function

void initboard(char board[ROWS][COLS], int rows, int cols, char set)
{
for (int i = 0; i < rows; i + + )//We traverse the entire two-dimensional array through nested loops
{
for (int j = 0; j < cols; j + + )
{
board[i][j] = set;//Initialize the elements of each subscript
}
}
}

3. Print display module

First, call the display function in the test.c file

Function implementation

void displayborad(char show[ROWS][COLS], int row, int col)
{
printf("------Mine Sweeper Game---------\\
");
for (int a = 0; a <=col; a + + ) //Print the column label of the first row first
{
printf("%d ", a);
}
printf("\\
");
for (int i = 1; i <=row; i + + )//Print the chessboard
{
printf("%d ", i);
for (int j = 1; j <= col; j + + )
{
printf("%c ", show[i][j]);
}
printf("\\
");
}
}

4. Mine setting module

//Thunder
setmine(mine, ROW, COL);
void setmine(char arr[ROWS][COLS], int row, int col)
{
int num = minenum;
while(num)
{
int x = rand() % row + 1;//Randomly mine by generating random values
int y = rand() % col + 1;
if (arr[x][y] == '0')
{
arr[x][y] = '1';
num--;
}
}
\t
}

When we use the rand function, we need to use srand on the main function.

srand((unsigned int)time(NULL));

5. Demining module

//Demining
findmine(mine, show, ROW, COL);
void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x, y = 0;//First define a check coordinate
int num = 0;//define another number to calculate the number of locations that have been checked
while (num < ROW * COL - minenum)
{
printf("Please enter the coordinates for investigation:");
scanf_s("%d %d", & amp;x, & amp;y);
if (x >= 1 & amp; & y >= 1 & amp; & amp; x <= row & amp; & amp; y <= col)//Check whether the correct coordinates are entered
{
if (mine[x][y] == '1')//Execute this when it detects mine
{
printf("The game failed and you were killed");
displayborad(mine, ROW, COL);
break;
}
else//Main demining program
{
//Retrieve nine nearby digits
int num1 = getnum_of_mine(mine,x,y);
show[x][y] = num1 + '0';//Change the display content based on the number of nearby mines
displayborad(show, ROW, COL);//Display the troubleshooting information
num + + ;

}
}
else
{
printf("Illegal coordinates, please re-enter\\
");
}
}
if (num == row * col - minenum)
{
printf("Congratulations, you won! Demining completed!\\
");
displayborad(mine, ROW, COL);
}
\t
}

The module for retrieving the surrounding 8 bits is also called here, and the simple code is implemented as follows:

int getnum_of_mine(char mine[ROWS][COLS],int x,int y)
{
return (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] + mine[x][y + 1] + mine[x - 1][y + 1]-8*'0');
}

Four. Game implementation

Game full code

game.h
#pragma once
#include<stdlib.h>
#include<time.h>
#define minenum 10
#define ROW 9
#define COL 9

#define ROWS ROW + 2
#define COLS COL + 2
//initialization
void initboard(char board[ROWS][COLS], int rows, int cols, char set);
//Print the chessboard
void displayborad(char show[ROWS][COLS], int row, int col);
//Thunder
void setmine(char arr[ROWS][COLS], int row, int col);
//Demining
void findmine(char mine[ROWS][COLS], char show[ROWS][COLS],int row, int col);
game.c
#include"game.h"

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


void displayborad(char show[ROWS][COLS], int row, int col)
{
printf("------Mine Sweeper Game---------\\
");
for (int a = 0; a <=col; a + + )
{
printf("%d ", a);
}
printf("\\
");
for (int i = 1; i <=row; i + + )
{
printf("%d ", i);
for (int j = 1; j <= col; j + + )
{
printf("%c ", show[i][j]);
}
printf("\\
");
}
}

void setmine(char arr[ROWS][COLS], int row, int col)
{
int num = minenum;
while(num)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (arr[x][y] == '0')
{
arr[x][y] = '1';
num--;
}
}
\t

}


int getnum_of_mine(char mine[ROWS][COLS],int x,int y)
{
return (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] + mine[x][y + 1] + mine[x - 1][y + 1]-8*'0');
}



void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x, y = 0;
int num = 0;
while (num < ROW * COL - minenum)
{
printf("Please enter the coordinates for investigation:");
scanf_s("%d %d", & amp;x, & amp;y);
if (x >= 1 & amp; & y >= 1 & amp; & amp; x <= row & amp; & y <= col)
{
if (mine[x][y] == '1')
{
printf("The game failed and you were killed");
displayborad(mine, ROW, COL);
break;
}
else
{
//Retrieve nine nearby digits
int num1 = getnum_of_mine(mine,x,y);
show[x][y] = num1 + '0';
displayborad(show, ROW, COL);
num + + ;

}
}
else
{
printf("Illegal coordinates, please re-enter\\
");
}
}
if (num == row * col - minenum)
{
printf("Congratulations, you won! Demining completed!\\
");
displayborad(mine, ROW, COL);
}
\t
}

test.c
#include<stdio.h>
#include"game.h"

//Print menu
void menu()
{
printf("****************************\\
");
printf("***** 1.play *****\\
");
printf("***** 0.exit *****\\
");
printf("****************************\\
");
}
//Game implementation
void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];

//Initialize mine table
initboard(mine, ROWS, COLS, '0');
initboard(show, ROWS, COLS, '*');
//Print the chessboard
displayborad(show, ROW, COL);
//Thunder
setmine(mine, ROW, COL);
//displayborad(mine, ROW, COL);
//Demining
findmine(mine, show, ROW, COL);
}

int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("Please enter your choice:");
scanf_s("%d", & amp;input);
switch(input)
{
case 1:
{
game();
break;
}
case 0:
{
printf("Exit the game\\
");
break;
}
default:
{
printf("Input error, please re-enter");
break;
}
}

} while (input);
return 0;
}

Five, Difficulties

1. Analysis of the game

2. Function design

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. C Skill Tree Home Page Overview 194,261 people are learning the system