C language: Minesweeper (simple production version)

Everyone should have played the game Minesweeper, so I won’t go into details about the rules of Minesweeper. I will explain it using a 9*9 Minesweeper board.

Making a minesweeper game is definitely more complicated, so we choose multiple files and multiple functions to write the code.

One, h header file, two .c files

Let’s take a look at the files first

#pragma once
#include"game.h"
#include<stdio.h>
#define H 9
#define L 9
#define HS H + 2
#define LS L + 2
void game(char arr2[H][L], int h, int l);
void set_mine(char arr1[HS][LS], int hs, int ls);
#include<stdlib.h>
#include<time.h>
void play(char arr1[HS][LS],char arr2[H][L], int h, int l);
static int GetMineCount(char arr1[HS][LS], int x, int y);
void sign(char arr1[HS][LS], char arr2[H][L], int x, int y,int h,int l);

Create the game interface

This is not difficult. It’s the same as guessing numbers in the last issue (CSDN). There is no problem. You can also have your own ideas. Make the following more enjoyable and try to be related to Minesweeper.

Menu function

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

}

In-game interface

The Minesweeper game is an n*n plane, so we can think of using a two-dimensional array. The previous issue talked about the creation of a two-dimensional array.

This is our chessboard. The character ‘0’ is the coordinate where there is no bomb, and the character ‘1’ is the bomb we buried.

According to the rules, when we select a coordinate, if there is no bomb at this coordinate, the number of bombs in the 8 coordinates near it will be displayed; if this is a bomb, the game will fail and end.

We can imagine that it would be too difficult to set up only one two-dimensional array, so we set up two two-dimensional arrays, which can be understood as setting up two chessboards. As long as the parameters are passed using functions, the parameter information can be communicated. One is for players to see, the other is for planting bombs, and the other cannot be seen by players.

Use for loop and use ‘*’ to lay out Figure 1

And because it is inconvenient to view these coordinates like this, we add some code to make it have abscissa and ordinate, and then combine them to form a function that does not need to return a value.

Player interface function

void game(char arr2[H][L],int h,int l)
{
\t
for (int i = 1; i <= h; i + + )
{
for (int n = 1; n <= l; n + + )
{
arr2[i][n] = '*';
}
}
for (int i = 0; i <= 9; i + + )
{
printf("%d ", i);
}printf("\
");
for (int i = 1; i <= h; i + + )
{
printf("%d ", i);
for (int n = 1; n <= l; n + + )
{
printf("%c ", arr2[i][n]);
}
printf("\
");
}
 }

Success! !

There is a problem, how to calculate the number of bombs at 8 nearby coordinates, and some coordinates have only three coordinates nearby. In order to solve this problem, we can change the chessboard where bombs are buried to 11*11, and it will be solved.

In this way, changing all the coordinates of the outermost layer to the character 0’ can facilitate calculation.

Okay, then the chessboard is done. Next, we bury the bomb. The bomb must be released immediately. We can use rand(), which can generate 0~32767. However, we have a 9*9 chessboard, int ret = rand()% 9 + 1 is fine, use ret to accept the return value

Because it is a plane coordinate, we set two

int retx = rand()%9 + 1;
int rety = rand()%9 + 1;

In this way, we randomly generate coordinates. However, there is a BUG. If two identical coordinates are generated, then there will be no bombs. So we need to add a judgment statement, a while loop and count to set 10 bomb, and then pass the coordinates into the two-dimensional array. This is a chessboard that cannot be shown to players.

int count = 10;

while (count) {
int ret1 = rand() % 9 + 1;
int ret2 = rand() % 9 + 1;
if (arr1[ret1][ret2] != '1') {
arr1[ret1][ret2] = '1';
count--;
}
}

Since the base value of rand() will not change, we have to refer to srand() and time functions, which will be discussed in the issue of guessing numbers.

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

Then planting bombs will be no problem

Line buried function

void set_mine(char arr1[HS][LS], int hs, int ls)
{
for (int i = 0; i < hs; i + + )
{
for (int n = 0; n < ls; n + + )
{
arr1[i][n] = '0';
}
}
int count = 10;

while (count) {
int ret1 = rand() % 9 + 1;
int ret2 = rand() % 9 + 1;
if (arr1[ret1][ret2] != '1') {
arr1[ret1][ret2] = '1';
count--;
}
}
}

Game rules (function implementation)

Let’s first calculate the number of bombs nearby. It’s very simple. Just add them together. However, because characters are added and subtracted to get numerical values, we need to add the character ‘0’ to convert the resulting value into characters, and then pass it on. Give a two-dimensional array. You can solve it with functions

Count the number of nearby mines

int GetMineCount(char mine[HS][LS], 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';
}

Mark mine coordinate function

void sign(char arr1[HS][LS], char arr2[H][L],int x,int y,int h,int l)
{
re:
printf("Whether to mark thunder, please press 1 if yes, press 0 if not\
");
int s = 0;
scanf("%d", & amp; s);
while(s)
{
printf("Please enter the marker coordinates:");
int x, y;
scanf("%d%d", & amp;x, & amp;y);
if (x > 9 || x < 1 || y > 9 || y < 1)
{
printf("Incorrect input, please re-enter!\
");
goto re;
}
else
{
system("cls");
for (int i = 0; i <= 9; i + + )
{
printf("%d ", i);
}printf("\
");
arr2[x][y] = '!';
for (int i = 1; i <= h; i + + )
{
printf("%d ", i);
for (int n = 1; n <= l; n + + )
{
printf("%c ", arr2[i][n]);
}
printf("\
");
}
goto re;
}
}
}

1. First determine whether the filled in coordinates are legal, and then determine whether it is a bomb. Then use the judgment statement to solve the problem easily.

2. The next step is to convert the selected coordinates into nearby bomb values.

3. If the game fails, the player must be shown the distribution of bombs.

4. Mark the mine coordinates

void play(char arr1[HS][LS],char arr2[H][L], int h, int l)
{
int count = H*L - 10;
while(count)
{
count--;
int x = 0, y = 0;
printf("-----------------------\
");
printf("Please enter your mine clearance coordinates:");
scanf("%d%d", & amp;x, & amp;y);
if (x > 9 || x < 1 || y > 9 || y < 1)
{
printf("Incorrect input, please re-enter!\
");
}
else
{
if (arr1[x][y] != '1')
{
int g = GetMineCount(arr1, x, y);
arr2[x][y] = g + '0';
system("cls");
for (int i = 0; i <= 9; i + + )
{
printf("%d ", i);
}printf("\
");
for (int i = 1; i <= h; i + + )
{
printf("%d ", i);
for (int n = 1; n <= l; n + + )
{
printf("%c ", arr2[i][n]);
}
printf("\
");
}sign(arr1, arr2, x, y,h,l);
}
else
{
printf("BOOM!\
GAME OVER!\
You failed\
");
for (int i = 0; i <= 9; i + + )
{
printf("%d ", i);
}printf("\
");
for (int i = 1; i <= h; i + + )
{
printf("%d ", i);
for (int n = 1; n <= l; n + + )
{
printf("%c ", arr1[i][n]);
}
printf("\
");
}break;
}
}
\t\t
}
if (count==0){printf("Congratulations on your successful demining!!!\
");
for (int i = 0; i <= 9; i + + )
{
printf("%d ", i);
}printf("\
");
for (int i = 1; i <= h; i + + )
{
printf("%d ", i);
for (int n = 1; n <= l; n + + )
{
printf("%c ", arr1[i][n]);
}
printf("\
");
}
}

}

Main function

We want the player to choose whether to play, so we need to use a do while loop, which must be executed once to see if the player plays or not.

Then bring the function we wrote into

int main()
{
int a = 0;
srand((unsigned int)time(NULL));
char arr2[H][L];
char arr1[HS][LS];
do
{
menu();
scanf("%d", & amp;a);
switch (a)
{
case 1:
{
game(arr2, H, L); set_mine(arr1, HS, LS); play(arr1,arr2, H, L);
break;
}
case 0:printf("Exit game\
"); break;
default:printf("Input error\
");
}
} while (a);

return 0;
}

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