C language to implement minesweeping (specific steps and codes)

Directory

1. Problem description

2. Basic process

3. Steps

1. Menu interface

2. Create the chessboard

3. Board initialization

4. Print the chessboard

5. Arrange mines

6. Check mine

4. Results demonstration

1. Get killed

2. Successful demining

5. Code implementation

1.test.c

2. game.c

3. game.h


1. Description of the problem

Minesweeper implemented in C language.

2. Basic process

1. On the menu interface, choose to start or exit the game.

2. Create a chessboard and initialize it.

3. Print the chessboard.

4. Arrange mines.

5. Check for mines.

6. Go back to step 2 and continue.

3. Steps

1. Start the game 0. Exit the game

void menu()
{
printf("****************************\
");
printf("******** 1.paly ***********\
");
printf("******** 0.exit ***********\
");
printf("****************************\
");
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("Please select:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("Exit the game\
");
break;
default:
printf("Choose wrong, choose again\
");
break;
}
} while (input);
return 0;
}

2. Create a chessboard

We first need to know that if we create a chessboard with 9 rows and 9 columns, we need an array of 11 rows and 11 columns to store all the data. This is to prevent the coordinates from crossing the boundary when checking the 8 coordinates around the mine.

Chessboard: It is represented by a two-dimensional array of 9 rows and 9 columns, and the element type is char.

Reasons for using macro definitions for rows and columns:

1. Improve the readability of the code. If you encounter 9 later, you can easily understand the meaning.

2. Improve code scalability. If you want to modify the size of the chessboard, just change the macro definition.

Respectively macro define ROW and RWOS for easy use

#define ROW 9
#define COL 9

#define ROWS ROW + 2
#define COLS COL + 2

Creating these two boards separately can be better understood:

char mine[ROWS][COLS];//store the arranged mines
char show[ROWS][COLS];//store the information of mines detected

3. Chessboard initialization

The four parameters passed in are the address of the chessboard, row number, column number, and stored elements

1. The mine array is all 0′ at the beginning
2. The show array is all ‘*’ at the beginning

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;
}
}
}

4. Print chessboard

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

result:

5. Arrange mines

The computer randomly generates coordinates, and “1” represents the mines arranged by the computer.

Notice:

1. To be used in the main function

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

Timestamps are used to ensure that the generated coordinates are truly random.

2. The computer can only place mines where it is ‘0’.

EASY_COUNT is the total number of mines arranged on the board

#define EASY_COUNT 10
void SetMine(char board[ROWS][COLS], int row, int col)
{
// Arrange ten mines
//1. Generate random coordinates and arrange mines
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % row + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}

6. Mine-checking

Here you need to create a GetMineCount() function to count how many mines are around this coordinate (the number of mines in eight coordinates around the input coordinates)

int GetMineCount(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');
}

1. If the input coordinates are Lei’s coordinates, it will show that he was killed by the explosion, and the game is over.

2. If the input coordinates are not mine coordinates, count how many mines are around this coordinate and check in circles.

3. If the input coordinates exceed the range of the chessboard, re-enter.

4. If you check 10 times and are not killed, the game wins.

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - EASY_COUNT)
{
printf("Please enter the coordinates to check:>");
scanf("%d %d", &x, &y);
if (x >= 1 & amp; & amp; x <= row & amp; & amp; y >= 1 & amp; & amp; y <= col)
{
if (mine[x][y] == '1')
{
printf("Unfortunately, you were killed\
");
break;
}
else
{
//This position is not a mine, just count how many mines are around this coordinate
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
win ++ ;
}
}
else
{
printf("The coordinates are illegal, please re-enter\
");
}
}
if (win == row * col - EASY_COUNT)
{
printf("Congratulations! Successful demining\
");
DisplayBoard(mine, ROW, COL);
}
}

4. Results presentation

1. Was bombed to death

2. Successful demining

V. Code Implementation

Write in three files

1.test.c

2.game.c (write the function in the game() function)

3.game.h (used to declare functions and header files)

1.test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

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

void game()
{
char mine[ROWS][COLS];//store the arranged mines
char show[ROWS][COLS];//store the information of mines detected
//Initialize the chessboard
//1.mine array is all '0' at the beginning
//2.show array is all '*' at the beginning
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
// print the chessboard
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//1. Arrange thunder
SetMine(mine, ROW, COL);
DisplayBoard(mine, ROW, COL);
//2. Check mine
FindMine(mine, show, ROW, COL);
}

int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("Please select:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("Exit the game\
");
break;
default:
printf("Choose wrong, choose again\
");
break;
}
} while (input);
return 0;
}

2.game.c

#define _CRT_SECURE_NO_WARNINGS 1

#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 DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
printf("----------Minesweeper Game----------\
");
for (i = 0; i <= col; i ++ )
{
printf("%d ", i);
}
printf("\
");
for (i = 1; i <= row; i ++ )
{
printf("%d ", i);
int j = 0;
for (j = 1; j <= col; j ++ )
{
printf("%c ", board[i][j]);
}
printf("\
");
}
}

void SetMine(char board[ROWS][COLS], int row, int col)
{
// Arrange ten mines
//1. Generate random coordinates and arrange mines
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % row + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}

int GetMineCount(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 FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - EASY_COUNT)
{
printf("Please enter the coordinates to check:>");
scanf("%d %d", &x, &y);
if (x >= 1 & amp; & amp; x <= row & amp; & amp; y >= 1 & amp; & amp; y <= col)
{
if (mine[x][y] == '1')
{
printf("Unfortunately, you were killed\
");
break;
}
else
{
//This position is not a mine, just count how many mines are around this coordinate
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
win ++ ;
}
}
else
{
printf("The coordinates are illegal, please re-enter\
");
}
}
if (win == row * col - EASY_COUNT)
{
printf("Congratulations! Successful demining\
");
DisplayBoard(mine, ROW, COL);
}
}

3.game.h

#pragma once

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

#define EASY_COUNT 10
#define ROW 9
#define COL 9

#define ROWS ROW + 2
#define COLS COL + 2

//Initialize the chessboard
void InitBoard(char mine[ROWS][COLS], int rows, int cols, char set);

// print the chessboard
void DisplayBoard(char show[ROWS][COLS], int row, int col);

//layout mine
void SetMine(char board[ROWS][COLS], int row, int col);


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