Using C language to implement a simple version of console mine clearance

Mine Sweeper is a classic stand-alone game where players need to find all the mines in a grid without triggering any of them. Below I will introduce in detail the design ideas and implementation steps of the Minesweeper game.

1. Game design
1. Game interface: The game interface uses an array to display the mines. The minefields may or may not be mines, and can display the number of mines in the surrounding 8 grids.


2. Operation method: Players can obtain the number of mines by inputting relevant coordinates, and then clear the mines.
3. Game victory: When the player enters all the coordinates except mine, the game is won.
4. Game failure: When the player triggers the mine, the game fails.

2. Implement the code

Next, follow the step-by-step implementation of the code and have a preliminary understanding of the minesweeper game:

1. Print the menu first and choose to play or exit the game according to the player’s needs;

#include "game.h"

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

int main()
{
int input = 0;
do
{
menu();//menu function prints menu
printf("Please select:");
scanf("%d", & amp;input);//Player selection
switch(input)
{
case 1:
game();
break;

case 0:
printf("Exiting the game...");
break;
default:printf("Illegal input, please choose again.\\
");
}
} while (input);

return 0;
}

First make the framework of the game and use the do while statement to enter or exit the game.

Enter the game when 1 is entered, exit the game when 0 is entered, and use the default statement to remind the player to make a new choice when entering other values.

2. The overall framework has been outlined. Next, the main part of the game is implemented.

However, compiling only on the main function will make the code look cumbersome, so the main part of the game can be placed in the game function for operation.

void game(void)
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
intboard(mine, ROWS, COLS, '0');
intboard(show, ROWS, COLS, '*');
displayboard(show, ROW, COL);
buried_mines(mine, ROW, COL);
fine_mine(mine, show, ROW, COL);
}

Each part of the game is stored in the game function, and each part is implemented by a separate function, which improves the readability of the code.

3. Define two two-dimensional arrays, one for burying mines, and one for displaying the number of mines around the unknown grid and the known grid.

char mine[ROWS][COLS];//Burying mines
char show[ROWS][COLS];//display

4. Next, assign values to these two arrays respectively.

For the mine array, you can use 0 to represent non-mine and 1 to represent mine. First, assign the character 0 to all array elements. We will explain the mine later.

For the show function, we need it so that the player does not know whether the corresponding coordinates are mines, so we hide it with the character *, and we also need to let it display the number of mines around it. We will explain the number of mines later.

For array assignment, we can use a function to assign values to two arrays.

void intboard(char arr[ROWS][COLS], int rows, int cols, char set)//set is the required characters
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i + + )
{
for (j = 0; j < cols; j + + )
arr[i][j] = set;
}
}//Assign a value to the array
//Function call
intboard(mine, ROWS, COLS, '0');
intboard(show, ROWS, COLS, '*');

5. Next is the mine laying stage. We need a function to lay mines, that is, assign character 0 to character 1 in the mine array.

void buried_mines(char arr[ROWS][COLS],int row, int col)
{
int count_mine = Autonomous_input;

while(count_mine)
{
int x = rand() % row + 1;
int y = rand() % col + 1;

if(arr[x][y] == '0')
{
arr[x][y] = '1';
count_mine--;
}
}
}

6. Next, you need to display a 9*9 array to the player for demining, then you need a function to print the array to print the display array.

void displayboard(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (j = 0; j <= col; j + + )
printf("%d ", j);
printf("\\
");
for (i = 1; i <= row; i + + )
{
printf("%d ", i);
for (j = 1; j <= col; j + + )
{
printf("%c ", arr[i][j]);
}
printf("\\
");
}

7. The last step is the demining stage (because it is a simple version, you need to check it one by one.)

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] + mine[x + 1][y + 1] - 8 * '0');

}//Calculate the mines around a certain coordinate

void fine_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x;
int y;
int win = 0;

while (win < row * col - Autonomous_input)
{
printf("Please enter the coordinates you are looking for [form: (X Y)]:");
scanf("%d %d", & amp;y, & amp;x);

if (x >= 1 & amp; & amp; x <= col & amp; & amp; y >= 1 & amp; & amp; y <= row)
{
if (mine[x][y] == '1')
{
printf("Hahaha, you are shit.\\
");
displayboard(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
displayboard(show, ROW, COL);
win + = 1;
}
}
else
{
printf("The coordinates are illegal, please re-enter.\\
\a\a\a");
}
}
if (win == row * col - Autonomous_input)
{
printf("Hey, not bad.\\
");
displayboard(mine, ROW, COL);
}
}

3. The specific code is as follows

1. Game main body (source file)

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

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

//Game implementation
//1. Game interface, choose to enter the game or exit the game
//2. Define two two-dimensional arrays, one for minefield and one for display. Both are 11*11. The actual value is 9*9 for operation.
//3. Plant mines randomly, set to ten mines
//4. Check for mines. If you step on them, you will fail. If you check multiple times, the number of mines within 9*9 will be displayed every time. Until all the mines are checked, it will be successful.

int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("Please select:");
scanf("%d", & amp;input);
switch(input)
{
case 1:
game();
break;

case 0:
printf("Exiting the game...");
break;
default:printf("Illegal input, please choose again.\\
");
}
} while (input);

return 0;
}

2. Related functions (source files)

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void intboard(char arr[ROWS][COLS], int rows, int cols, char set);

void game(void)
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
intboard(mine, ROWS, COLS, '0');
intboard(show, ROWS, COLS, '*');
displayboard(show, ROW, COL);
buried_mines(mine, ROW, COL);
fine_mine(mine, show, ROW, COL);
}

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

void displayboard(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (j = 0; j <= col; j + + )
printf("%d ", j);
printf("\\
");
for (i = 1; i <= row; i + + )
{
printf("%d ", i);
for (j = 1; j <= col; j + + )
{
printf("%c ", arr[i][j]);
}
printf("\\
");
}
}

void buried_mines(char arr[ROWS][COLS],int row, int col)
{
int count_mine = Autonomous_input;

while (count_mine)
{
int x = rand() % row + 1;
int y = rand() % col + 1;

if(arr[x][y] == '0')
{
arr[x][y] = '1';
count_mine--;
}
}
}

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] + mine[x + 1][y + 1] - 8 * '0');

}

void fine_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x;
int y;
int win = 0;

while (win < row * col - Autonomous_input)
{
printf("Please enter the coordinates you are looking for [form: (X Y)]:");
scanf("%d %d", & amp;y, & amp;x);

if (x >= 1 & amp; & amp; x <= col & amp; & amp; y >= 1 & amp; & amp; y <= row)
{
if (mine[x][y] == '1')
{
printf("Hahaha, you are shit.\\
");
displayboard(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
displayboard(show, ROW, COL);
win + = 1;
}
}
else
{
printf("The coordinates are illegal, please re-enter.\\
\a\a\a");
}
}
if (win == row * col - Autonomous_input)
{
printf("Hey, not bad.\\
");
displayboard(mine, ROW, COL);
}
}

3. Related statements (header files)

#pragma once

#include <stdio.h>

#define ROW 9

#define COL 9

#define ROWS ROW + 2

#define COLS COL + 2

#define Autonomous_input 10

void game(void);

void displayboard(char arr[ROWS][COLS], int rows, int cols);

#include <time.h>

#include <stdlib.h>

void buried_mines(char arr[ROWS][COLS], int row, int col);

int get_mine_count(char mine[ROWS][COLS], int x, int y);

void fine_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

The above is my opinion on the simple version of Minesweeper. If there are any mistakes or additions, please leave a message in the comment area. Thanks for watching! ! !