catalogue
Overall idea: (test. C) (game. H) (game. C) )
Step 1: (game start interface)
Step 2: Initialize and print mine disk
game .h Document code summary:
test .c Document code summary:
game .c Document code summary:
For beginners, it is relatively difficult to write the code of minesweeping game completely, but the length of the code will be slightly shorter than that of Sanzi game. At this time, we can divide it into various functions, and finally reference it to the main function to realize the operation of the code. First, the overall idea should be established completely.
Overall idea: (test. C) (game. H) (game. C) )
We can first split the Gobang into three source files to make the overall look and feel of the code more concise and clear
test .c Test the logic of the game
game .h About the declaration of game related functions, the inclusion of symbol declaration header files
game .c Implementation of game related functions
Step 1: (game start interface)
We need to create the start interface of the minesweeping game. We can first write a menu function. Judge the start or end of the game by the value 1 or 0 entered by the player. At the same time, we should also consider the number other than 1 and 0 entered by the player, print and remind the player to re-enter the two numbers 1 or 0. We can associate the circular statement and switch statement to realize the repeatable minesweeping game and pair Player selection judgment.
The initial interface functions in test.c are as follows:
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include"game.h" void menu() { printf("************************************\n"); printf("***** 1. play *******\n"); printf("***** 0. exit *******\n"); printf("************************************\n"); } int main() { int input = 0; do { menu(); printf("Please select"); scanf("%d", &input); switch (input) { case 1: printf("mine clearance\n"); break; case 2: printf("Exit the game\n"); break; default: printf("Selection error, please re-enter\n"); break; } } while (input); return 0; }
The operation results are as follows:
Step 2: Initialize and print mine disk
The effect diagram to be realized is as follows: (definition: "0" is non mine "1" is thunder )
First of all, we need to clarify the following points:
a: The Minesweeper game's Minesweeper disc contains two kinds of information: one is to store thunder or non thunder, the other is to store the character "*" to hide the information of our thunder and check the thunder at the same time, so we can create two checkerboards, one is to store the information of the arranged thunder, the other is to store the information of the checked thunder, so as to realize the separate processing of the information.
b: For the completion of the mine sweeping game, we first need to store mines in the 9 * 9 mine disks, that is, we can initially change all the mine disks to "0" and use the time stamp to randomly store the mines in the mine disk "0" is replaced by "1", so we can complete the random storage of mines.
c: After storing the thunder, the player selects the coordinates. When the coordinates are thunder, the player will be killed and the game is over. When the coordinates are not thunder, the surrounding thunder will be checked and counted. The number of thunder will be counted based on the Jiugong grid centered on the coordinates selected by the player. However, when we select the coordinates at the edge of the Jiugong grid, we will count the thunder of the Jiugong grid centered on the coordinates When checking the quantity, it will cross the boundary. Obviously, we can initialize two thunder disks with the row and column of 11 * 11, and set all the thunder disks with the overflow row and column of 9 * 9 to "0" to solve the overflow problem of checking the edge coordinate thunder.
As shown in the figure:
d: We can replace the rows and columns of the array with defined constants so that the size of the thunder disk can be modified in the future.
As shown in the figure:
e: While printing the chessboard, in order to facilitate players to find coordinate points, we can print the numbers corresponding to its rows and columns on the edge of the 9 * 9 thunder board.
The code for printing the thunder disk is as follows:
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include"game.h" void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; printf("---Minesweeping game——————\n"); //Print column for (i = 0; i <= col; i++) { printf("%d ", i); } printf("\n"); for (i =1; i <= row; i++) { printf("%d", i); //Print line for (j =1; j <= col; j++) { printf(" %c", board[i][j]); } printf("\n"); } }
Print results: (Note: at last, we only need to print the second disk, and the first disk only has information, but does not print)
Step 3: lay lightning
Let's clarify our ideas first (let's arrange 10 mines first) (coordinates (x,y) are the coordinates of arranging mines)
a: Although the size of the thunder disk we printed is 9 * 9, we actually arranged an 11 * 11 thunder disk.
b: The thunder we arranged should be in the 9 * 9 thunder disk, so the X and y random numbers we obtained by using the timestamp can take the modulus of the row (9) and column (9) respectively, and the remainder range is 0-8. We can add 1 on the basis of 0-8 to make our x and y values within the range we need, and make the arranged thunder in the 9 * 9 thunder disk.
The code of mine layout is as follows:
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include"game.h" #include<stdlib.h> //Lay thunder void SetMine(char mine [ROWS][COLS], int row, int col) { //Arrange 10 mines int count =0; while (count<10) { //Generate random subscript int x = rand()%row+1; // We will generate random valued modules on our row (row 9) and col (column 9) int y = rand()%col+ 1; if (mine [x][y] == '0') { mine [x][y] = '1'; count++; } } }
The print result is:
Step 4: mine detection
Let's clarify the good idea first:
1. Enter the coordinates for troubleshooting
2. Check whether there is thunder at the coordinates
(1) It's ray Blow up game over
(2) Not ray Count the number of surrounding mines Store the demining information into the show array, and the game continues
Counting the number of thunder around the thunder is to count the number of thunder in the Jiugong grid centered on the coordinates selected by the player. If there are several thunder, we will display the number of thunder around based on the coordinates selected by the player. (define the variable count to add up the ASCII code values of all coordinates in the Jiugong grid, because the character number "1" is thunder and the character number "0" For non thunder, we can know the number of surrounding thunder by subtracting the ASCII value of our total eight character numbers "0" from the total ASCII value of the added character numbers
As shown in the figure: (the value of its own coordinate (x, y) is 0, which does not need to be added, but only the size of the ASCII code value of characters and numbers in the eight surrounding coordinates)
We should also make it clear that what we create is a character type two-dimensional array. We need to convert the corresponding number into the decimal ASCII code value of the corresponding character. (the ASCII code value of character "0" is 48, the decimal ASCII code value of character "2" is 2 + "0" = 50, and the decimal ASCII code value of character "3" is 3 + "0" = 51, from which we can get count + "0" to achieve our goal)
The code of mine troubleshooting is as follows:
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include"game.h" #include<stdlib.h> // The function we use to count the number of mines int get_mine_count (char mine[ROWS][COLS], int x, int y) { return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0'; //The values we add are the ASCII code values of each character number. Finally, we need to subtract the ASCII code value of 8 characters "0" } //Check thunder void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { //1. Enter the coordinates for troubleshooting //2. Check whether there is thunder at the coordinates // (1) It's thunder. The game is over // (2) Instead of counting the coordinates of the surrounding mines, store the demining information into the show array. The game continues int x = 0; int y = 0; while (1) { printf("Please enter the coordinates to check\n"); scanf("%d%d", &x, &y); //Judge the legitimacy of coordinates if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1') { printf("I'm sorry you were killed\n"); DisplayBoard(mine, row, col); break; } else { //If it's not thunder, let's count how many thunder in the nine palaces with the coordinates as the center int count = get_mine_count (mine, x, y); //The two-dimensional array show we defined is a character array, so we have to convert it into the ASCII code value of the corresponding number show[x][y] = count + '0'; //Display the troubleshooting information DisplayBoard(show, row, col); } } else { printf("The coordinates entered are illegal, please re-enter\n"); } } }
At this point, the production of our minesweeping game is completely over. The following is a summary of all code segments of each file
test .c Test the logic of the game
game .h About the declaration of game related functions, the inclusion of symbol declaration header files
game .c Implementation of game related functions
game .h Document code summary:
#pragma once #include<stdlib.h> #include <stdio.h> //Define constants in game.h //When we want to use the header file defined by ourselves, we need to type (#include"game.h") in the source file #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 //Initialize thunder disk void InitBoard(char board[ROWS][COLS], int rows, int cols, char set); //Printing of chessboard void DisplayBoard(char board[ROWS][COLS], int row, int col); //Lay thunder void SetMine(char mine[ROWS][COLS], int row, int col); //Check thunder void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
test .c Document code summary:
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void menu() { printf("************************************\n"); printf("***** 1. play *******\n"); printf("***** 0. exit *******\n"); printf("************************************\n"); } void game() { //Thunder disk for storing information of arranged thunder char mine[ROWS][COLS] = {0}; //Mine disk for storing information of mine detection char show[ROWS][COLS] = {0}; //Initialize thunder disk InitBoard(mine, ROWS, COLS, '0'); //The mine disk for initializing mine storage is "0"“ InitBoard(show, ROWS, COLS, '*'); //The mine disk for initial mine troubleshooting is "*“ //Print thunder disk DisplayBoard(show, ROW, COL); //Lay thunder SetMine(mine, ROW, COL); //DisplayBoard(mine, ROW, COL); //Check thunder FindMine(mine, show, ROW, COL); } int main() { int input = 0; srand((unsigned )time(NULL)); // The time stamp is used to randomly produce the coordinates of the mine, so as to arrange the mine do { menu(); printf("Please select"); scanf("%d", &input); switch (input) { case 1: game(); break; case 2: printf("Exit the game\n"); break; default: printf("Selection error, please re-enter\n"); break; } } while (input); return 0; }
game .c Document code summary:
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" //Initialize 11 * 11 thunder disk void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) { int i = 0; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < rows; j++) { board[i][j] = set; } } } //Print 9 * 9 thunder disk void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; printf("---Minesweeping game——————\n"); //Print column for (i = 0; i <= col; i++) { printf("%d ", i); } printf("\n"); for (i =1; i <= row; i++) { printf("%d", i); //Print line for (j =1; j <= col; j++) { printf(" %c", board[i][j]); } printf("\n"); } printf("---Minesweeping game——————\n"); } //Lay thunder void SetMine(char mine [ROWS][COLS], int row, int col) { //Arrange 10 mines int count =0; while (count<10) { //Generate random subscript int x = rand()%row+1; // We will generate random valued modules on our row (row 9) and col (column 9) int y = rand()%col+ 1; if (mine [x][y] == '0') { mine [x][y] = '1'; count++; } } } // The function we use to count the number of mines int get_mine_count (char mine[ROWS][COLS], int x, int y) { return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0'; //The values we add are the ASCII code values of each character number. Finally, we need to subtract the ASCII code value of 8 characters "0" } //Check thunder void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { //1. Enter the coordinates for troubleshooting //2. Check whether there is thunder at the coordinates // (1) It's thunder. The game is over // (2) Instead of counting the coordinates of the surrounding mines, store the demining information into the show array. The game continues int x = 0; int y = 0; while (1) { printf("Please enter the coordinates to check\n"); scanf("%d%d", &x, &y); //Judge the legitimacy of coordinates if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1') { printf("I'm sorry you were killed\n"); DisplayBoard(mine, row, col); break; } else { //If it's not thunder, let's count how many thunder in the nine palaces with the coordinates as the center int count = get_mine_count (mine, x, y); //The two-dimensional array show we defined is a character array, so we have to convert it into the ASCII code value of the corresponding number show[x][y] = count + '0'; //Display the troubleshooting information DisplayBoard(show, row, col); } } else { printf("The coordinates entered are illegal, please re-enter\n"); } } }
Try:
The above is about the content of mine sweeping game. If you like it, you can like it or collect it!
Thank you for browsing! (if there is any problem, please point it out and I will correct it in time!)