# C language based minesweeper game

• 2020-11-26 18:56:44
• OfStack

Minesweeper is similar to the framework of the 3 board games I wrote above, for code that is a little more complicated than this. We should apply the same approach, build a framework, clarify our thinking, and then write code

1. First of all, for minesweeper, we should create two 2-dimensional arrays, one for the player to see, the other should be a 2-dimensional array containing the exact location of the mine, only one is not enough.

2. Print out the table that the player can see

3. Let the player input the coordinates to be entered and verify them

4. Determine if there are any mines. If there are any, the game will end directly

5. If there are no errors, hit the number of grenades on the screen

6. To judge the winning or losing, look at the number of squares opened if 71=9*9-10

7. If the number of squares turned over is 71, the game is over and the player wins

Train of thought 1 kind, basically be according to the train of thought that lists oneself to write code reasonably, good

Take the following example:

``````
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX_COL 9   // Macro definition
#define MAX_ROW 9
#define DEFAULT_MINE_COUNT 10  // Macro definition 1 a 10 A ray

// minesweeper
//1. Create two 2 Dimension array and initialize
//2. print 1 map
//3. The player enters the coordinates to read , The coordinates entered by the player are verified
//4. Determine if the input coordinates are mined , If there is , Then the game is over
//5. If there is no , Count the number of surrounding mines and display it on the screen
//6. Judge the winning or losing , And check the number of open cells ,9*9-10=71;
//7. If you have 71 Turn over all the squares , The game is over , Players win

printf("\n\n=======================\n");
printf("1. Start the game \n");
printf("2. End of the game \n");
printf("=======================\n");
printf(" Please enter the serial number :");
int choice = 0;
scanf("%d",&choice);
return choice;
}
void init(char showMap[MAX_ROW][MAX_COL], char mineMap[MAX_ROW][MAX_COL]){ // Of the two 2 An array of dimensions is given an initial value , And change the number of deposits
for (int row = 0; row < MAX_ROW; row++){  //for nested (2 Dimensional array )
for (int col = 0; col < MAX_COL; col++){
showMap[row][col] = '*';
}
}
for (int row = 0; row <MAX_ROW; row++){
for (int col = 0; col <MAX_COL; col++){
mineMap[row][col] = '0';
}
}
int n = DEFAULT_MINE_COUNT;   // Assign a value to n
while(n > 0){     // Loop and let the computer pick a random number
int row = rand() % MAX_ROW;
int col = rand() % MAX_COL;
if (mineMap[row][col] == '1'){ // Met ray
continue;
}
mineMap[row][col] = '1';
n--;  // Successive decline , Ensure that only 10 A ray
}
}
void printMap(char theMap[MAX_ROW][MAX_COL]){  // This is the interface that the player can see
printf("1 2 3 4 5 6 7 8 9\n");
printf("-----------------\n");
for (int row = 0; row < MAX_ROW; row++){
for (int col = 0; col < MAX_COL; col++){
printf("%c ",theMap[row][col]);
}
printf("\n");
}
printf("-----------------\n");

}
void updateShowMap(char showMap[MAX_ROW][MAX_COL], // Let show around on the open grid 8 The number of mines in each grid
char mineMap[MAX_ROW][MAX_COL],int row, int col){
int count = 0;
for (int r = row - 1; r<= row + 1; r++){
for (int c = col - 1; c <= col + 1; c++){
if (r < 0 || r >= MAX_ROW || c < 0 || c >= MAX_COL){ // check , Let's make it within a given range
continue;
}
if (mineMap[r][c] == '1'){   // There are around , the ++
count++;
}
}
}
showMap[row][col] = count + '0';  // Convert it to 10 Into the system
}

void game(){

char showMap[MAX_ROW][MAX_COL] = { 0 }; // define 2 Dimensional array
char mineMap[MAX_ROW][MAX_COL] = { 0 };
init(showMap, mineMap);   //1. The two 2 An array of dimensions is given an initial value , define
int openedBlockCount = 0;
while (1){

printMap(showMap);  //2. Display the diagram
int row = 0;
int col = 0;
printf(" Please enter the coordinates you want to turn over (row,col):");
scanf("%d %d",&row, &col); // Give the address
if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL){ // Here's a check on the function
printf(" You entered the wrong coordinates !\n");
continue;
}
if (showMap[row][col] != '*'){  // Error reminder
printf(" The current position has been turned over !\n");
continue;
}
if (mineMap[row][col] == '1'){  // Meet with ray , Game over
printf("Game over!\n");
printMap(mineMap); // And print mineMap()
break;
}
updateShowMap( showMap, mineMap, row, col);  // Displays the number of surrounding mines on the screen
openedBlockCount++;   // The number of cells that have been opened
if (openedBlockCount == MAX_ROW*MAX_COL - DEFAULT_MINE_COUNT){ // Number of cells turned over 71, The player wins
printf(" The game wins !;");
printMap(mineMap);   // Print the thunder figure
break;
}
}
}

int main(){
while (1){
if (choice == 1){
game(); // And in the choice=1 Called when the game() function
}else if (choice == 0){
printf("Goodbye!");
break;
}else{
printf(" Your input is incorrect ");
}
}
system("pause");
return 0;
}``````

Be careful and careful when writing this code, especially on row < =MAX_ROW this part of time, 1 must be careful, I also debugging for a long time in the morning, is because of added equal sign, tired for most of the day, or to type more code.

More interesting classic games to realize the special topic, to share with you:

C++ classic game summary

python classic game summary

python Tetris game collection

JavaScript classic games don't stop playing

java classic game summary

javascript classic game summary

Related articles: