I hope the content of this article is helpful to the friends in need!
catalogue
file
Simple logic
Print menu - > select whether to enter the game - > initialize array - > mine array is initialized with character zero - > mine array is initialized with '*' > Random mine - > Print mine array - > input coordinates for mine clearance and judge the position
Suppose we want to create a 9 × 9 mine clearance, we need to store mines, so we can easily think of using two-dimensional arrays. Then we establish two-dimensional arrays, one for storing mines and the other for demining, and display demining information. In this paper, we use the character 0 to represent non thunder and the character 1 to represent thunder. Therefore, we need to establish a char array to store.
OK, after understanding the basic information, let's think about a question. Will there be any problems in the process of ranking? In mine clearance, click the position you want to clear. If the position is mine, you will be killed and the game is over; If it is not thunder, it will display the number of surrounding thunder. Well, in the process of realizing this with code, it is thunder - blow up - the end of the game. This process is very simple; What about the process of "showing the number of mines around"? Will there be any problem?
Displays the number of surrounding mines
Suppose we build two arrays of char[9][9] according to the idea at the beginning.
So please think about it. When we want to check the red area in the figure above, can we check it correctly? The answer is No. ask for leave to think with me: if the location is mine, it's very simple. If it's not mine, it needs to traverse the surrounding area to display the surrounding quantity. OK, here's the key! The array we initialized has only 9 rows and 9 columns. When we set the code to traverse the target location, the periphery of the red area is an illegal zone, that is, out of bounds! So what to do? It's actually very simple. We just need to expand it one circle when we create it, that is, 11 × Two dimensional array of 11; But when we initialize the array, we only initialize 9 × 9 area.
Determine the target position
//Ray -- '1' // Non mine - '0' //Check thunder void mine_clearance(char mine[ROWS][COLS], char arr2[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int win = 0;//It is used to calculate the area to be checked. When the non mine area is checked, it will win while (win < row * col - MINENUMBER) { printf("Please enter the selected coordinates->"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1') { printf("I'm sorry to be killed\n"); Printboard(mine, ROW, COL);//Show the thunder map to make the player understand break;//It has been blown up and jumped out of the program to prevent dead circulation } else { int count = mines_num(mine, x, y);//Calculate the number of surrounding mines arr2[x][y] = count + '0'; //count is an integer. The character number can be displayed after adding the character zero. Printboard(arr2, ROW, COL); win++; } } else { printf("Illegal coordinates, please re-enter->"); //We need to judge the coordinates entered by the player to prevent the player from making mistakes } } if (win == row * col - MINENUMBER) { printf("congratulations! Demining succeeded!\n"); Printboard(mine, ROW, COL); } }
game2.h
#include<stdio.h> #include<time.h> #include<stdlib.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define MINENUMBER 20 / / number of buried mines void menu(); //initialization void initialize(char mine[ROWS][COLS], int row, int col, char set); //Print void Printboard(char mine[ROWS][COLS], int row, int col); //Buried mine void Buriedmine(char mine[ROWS][COLS], int row, int col); //Check thunder void mine_clearance(char mine[ROWS][COLS],char arr2[ROWS][COLS], int row, int col);
game2.c
#include"game2.h" //menu void menu() { printf("********************\n"); printf("****** 1.play ******\n"); printf("****** 0.exit ******\n"); printf("********************\n"); } //initialization void initialize(char mine[ROWS][COLS], int row, int col, char set) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { mine[i][j] = set; } } } //Print void Printboard(char mine[ROWS][COLS], int row, int col) { int i = 0; int j = 0; printf(" ----------- mine clearance -----------\n"); printf(" |"); for (j = 1; j <= col; j++) { printf("%2d ", j); } printf("\n"); printf(" --|-------------------------\n"); for (i = 1; i <= row; i++) { printf("%2d |", i ); for (j = 1; j <= col; j++) { printf(" %c ", mine[i][j]); } printf("\n"); } } //Buried mine void Buriedmine(char mine[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int count = MINENUMBER; while (count) { x = rand() % row + 1; y = rand() % col + 1; if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } } } //Calculate the number of mines int mines_num(char mine[ROWS][COLS], int x, int y) { return (mine[x - 1][y + 1] + mine[x][y + 1] + 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'); } //Ray -- '1' // Non mine - '0' //Check thunder void mine_clearance(char mine[ROWS][COLS], char arr2[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int win = 0; while (win < row * col - MINENUMBER) { printf("Please enter the selected coordinates->"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1') { printf("I'm sorry to be killed\n"); Printboard(mine, ROW, COL); break; } else { int count = mines_num(mine, x, y);//Calculate the number of surrounding mines arr2[x][y] = count + '0'; Printboard(arr2, ROW, COL); win++; } } else { printf("Illegal coordinates, please re-enter->"); } } if (win == row * col - MINENUMBER) { printf("congratulations! Demining succeeded!\n"); Printboard(mine, ROW, COL); } }
test2.c
#include"game2.h" void game() { char mine[ROWS][COLS] = { 0 }; char demining[ROWS][COLS] = { 0 }; //initialization initialize(mine, ROWS, COLS, '0'); initialize(demining, ROWS, COLS, '*'); //Print //Printboard(demining, ROW, COL); //Buried mine Buriedmine(mine, ROW, COL); Printboard(mine, ROW, COL); //Check thunder mine_clearance(mine,demining, ROW, COL); } int main() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("Please select->"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("You have quit the game\n"); break; default: printf("Input error, please re-enter\n"); } } while (input); }