Minesweeper Game: Practice with Functions and Arrays

Table of Contents

1. Analysis and design of minesweeper game

2. Implementation of minesweeper game code


Text

1. Analysis and design of minesweeper game

1.1 Minesweeper game function description

· Minesweeper has a simple menu

·The minesweeper chessboard is a 9*9 chessboard

·Minesweeper defaults to 10 mines

·Check for mines: If it is a mine, the game ends and you return to the menu;

If there is no mine, the game continues, showing how many mines are around;

If all the mines are found, the game is won and the mine clearance is successful.

1.2 Game analysis and design

For the convenience of subsequent data use, the data defined first is as follows:

#define ROW 9 //The number of chessboard rows can be modified
#define COL 9//The number of chessboard columns can be modified
#define ROWS ROW + 2//Number of rows that can be checked
#define COLS COL + 2//Number of columns that can be checked
#define EASY_COUNT 10//The number of mines that can be set

·Game interface

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

·9*9 chessboard setting

First, we need to create a 9*9 empty array. We use the character 0’ to represent non-thunder and the character 1’ to represent thunder.

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)//Initialize the board
{
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)//Set the chessboard
{
int i = 0;
printf("--------Mine Sweeper Game---------\
");
for (int i = 0; i <= col; i + + )
{
printf("%d ", i);
}
printf("\
");
for (int i = 1; i <= row; i + + )
{
printf("%d ", i);
int j = 0;
for (int j = 1; j <= col; j + + )
{
printf("%c ",board[i][j]);
}
printf("\
");
}
}

Secondly, in order to maintain a sense of mystery, we need to create another show array and use *’ to initialize the chessboard

Next, we place the mines, create a mine array, and use the rand() function to randomly generate ten mines through the timestamp.

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

void SetMine(char board[ROWS][COLS], int row, int col)//arrange mine
{
int count = EASY_COUNT;
while(count)
{
int x = rand() % row + 1;//1-9
int y = rand() % col + 1;//1-9
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}

}

·Lei’s investigation

Suppose we check the position with coordinates (1, 1). We visit eight positions in a circle. If the coordinates are at the outer position, the places beyond the array will be out of bounds. In order to prevent out of bounds, we directly store the data in 11* 11 in the array.

int GetMineCount(char mine[ROWS][COLS], int x, int y)//Find the number of surrounding mines
{
return (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x - 1][y] + 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 = 0;
int y = 0;
int win = 0;
while (win < row * col - EASY_COUNT)
{
printf("Enter the coordinates to be checked:");
scanf_s("%d %d", & amp;x, & amp;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\
");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("Illegal coordinates, re-enter\
");
}
}
if (win == row * col - EASY_COUNT)
{
printf("Congratulations, the demining was successful\
");
DisplayBoard(mine, ROW, COL);
}
}

2. Implementation of minesweeper game code

The overall code is as follows:

#include 
#include 
#include 

#define ROW 9//The number of chessboard rows can be modified
#define COL 9//The number of chessboard columns can be modified
#define ROWS ROW + 2//Number of rows that can be checked
#define COLS COL + 2//Number of columns that can be checked
#define EASY_COUNT 10//The number of mines that can be set
void menu()//Menu
{
printf("******************************\
");
printf("******** 1.play ********\
");
printf("******** 0.exit ********\
");
printf("******************************\
");
}

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)//Initialize the board
{
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)//Set the chessboard
{
int i = 0;
printf("--------Mine Sweeper Game---------\
");
for (int i = 0; i <= col; i + + )
{
printf("%d ", i);
}
printf("\
");
for (int i = 1; i <= row; i + + )
{
printf("%d ", i);
int j = 0;
for (int j = 1; j <= col; j + + )
{
printf("%c ",board[i][j]);
}
printf("\
");
}
}

void SetMine(char board[ROWS][COLS], int row, int col)//arrange mine
{
int count = EASY_COUNT;
while(count)
{
int x = rand() % row + 1;//1-9
int y = rand() % col + 1;//1-9
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}

}

int GetMineCount(char mine[ROWS][COLS], int x, int y)//Find the number of surrounding mines
{
return (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x - 1][y] + 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 = 0;
int y = 0;
int win = 0;
while (win < row * col - EASY_COUNT)
{
printf("Enter the coordinates to be checked:");
scanf_s("%d %d", & amp;x, & amp;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\
");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("Illegal coordinates, re-enter\
");
}
}
if (win == row * col - EASY_COUNT)
{
printf("Congratulations, the demining was successful\
");
DisplayBoard(mine, ROW, COL);
}
}

void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
DisplayBoard(show, ROW, COL);
SetMine(mine, ROW, COL);
FindMine(mine, show, ROW, COL);
}

int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("Please select:");
scanf_s("%d", & amp;input);
switch(input)
{
case 1:
game();
break;
case 0:
printf("Exit the game\
");
break;
default:
printf("Wrong selection, select again\
");
break;
}
} while (input);

return 0;
}