Implementation of minesweeper code and bugs

 1. The implementation of the minesweeping code is very similar to that of backgammon. It is essentially an application of arrays. First, we need to write out the general idea of ​​the entire project. For example, the game title is drawn by the do while () function, and then the switch statement is used to select Call a certain function of the game, such as starting the game from 1 and ending the game from 0. As shown in the picture:

The implementation of the game needs to initialize two boards, one board is used to place mines, and the other board is printed for the player to see. As for why not use an array to solve this problem, it is because the player needs to display the number of mines around after entering the coordinates for minesweeping , and it is obviously difficult for a chessboard to do these functions, because when calculating how many mines are around, the number of mines will be displayed in the coordinates, which may cause the displayed number of mines and the existing mines to be changed, and if When using two chessboards to calculate the number in the later stage, you only need to return the sum of the 8 numbers around the coordinates of the chessboard where the mine is placed, so it is recommended to use two arrays in this way. 2. First, initialize two arrays. The first array is the array where mines are placed. Initialize this array to 0. The second array is the array to be mineswept, which is the array displayed to the player. Initialize this array as ' *'. When initializing the array, we need to consider a problem. If you print a 9*9 chessboard, then if the coordinates are at the corner or on the edge when calculating the surrounding thunder, printing the 9*9 array will cause the coordinates to be missing. The best way That is, one more line at the top and bottom, and one more column at the left and right. When printing, you only need to print 9*9. As shown in the picture:

 

 

The second step is to print two arrays. In order to make the coordinates entered by the player start from 0 when printing the arrays, it is recommended that you print one more line when printing the number of rows, and print one more column as the coordinates of the array when printing the number of columns. This not only solves the problem that the input coordinates start from 1, but also allows players to see where the coordinates are located more intuitively. When initializing the array and printing the array, it must be satisfied that the arrays are the same, otherwise, when inputting coordinates or calculating the number of mines, it is very easy to find that the input coordinates are not at the position of the coordinates, and the calculated number of mines is also incorrect. As shown in the picture:

 

After printing the chessboard, it is time to consider placing the number of mines. In order to make the position of placing the number of mines different each time, you must use random numbers. We have already implemented random numbers in the game of backgammon. You only need to use them in the main function. Call the srand function, add a timestamp to srand, srand((unsigned int)time(NULL)), why time is forced to be converted to unsigned int, you can check the use of the time function. When placing the number of mines, you can create an int variable to calculate the number of mines placed. You need to keep placing the number of mines, so this requires a cycle. Every time you place a number of mines, you will subtract and subtract. When the variable is 0, the loop stops. As shown in the picture:

After placing mines, it’s time to clear mines. In the minesweeping function, first create two variables for the player to input the coordinates. After inputting the position, it is necessary to judge whether the input coordinates are legal. If they are legal, they will enter the next step. If they are not legal, they will remind the player to re-enter. , after the coordinates are legal, it is necessary to judge whether the coordinate position has been cleared by mines. If it has been cleared, the player will be reminded to re-enter. Otherwise, it will start to judge whether there are mines at this coordinate. If there are mines, it will be directly killed. In the step of counting, we create a function for calculating the number of mines around, put the input of placing mines, then put in the x and y coordinates, and finally use an integer variable to receive the value returned by the function, this return value is the number of mines, It needs to be explained here that when the array is initialized, the array is filled with characters, how to make the characters into integers and pass them to variables? For example, firstly, the ascll value of 1 in the ascll table is 49, and after subtracting the ascll value 48 of the character 0, it becomes the number 1, that is to say, the corresponding integer number is obtained after subtracting the character 0, so that the thunder number is returned to Variable, the next step is to assign the variable to the coordinates of the display array. When the display array is initialized, it is also a character, so the array needs to be converted into a character. Then with the example just now, just add the number to the ascll value of the character 0 to get the corresponding characters, so that the number of mines will be displayed and solved. The process of demining is a cycle, what is the cycle condition? It’s very simple, as long as the positions that are not mines have been sorted out, the mine clearance is successful, so create a variable. The loop condition is that the variable is less than the number of rows times the number of columns and then subtract the number of mines. When the variable is equal to the number of rows times the number of columns and then When the number of mines is subtracted, the player clears mines successfully, otherwise the player keeps clearing mines. As shown in the picture:

The complete code is shown below:

 

 

 

 

 

Guess you like

Origin blog.csdn.net/Sxy_wspsby/article/details/126695946