Minesweeping game rules of the game is in the minefield (form a) mark, if not careful will ray mistaken for security will be killed, and marked all the thunder will win in the minefield!
thinking
First we should make it clear that we are going to use a two-dimensional array to simulate a minefield, randomly generating a distribution of lightning in a minefield (a two-dimensional array), which requires a two-dimensional array. The player is also shown a two-dimensional array, so these are two two-dimensional arrays.
For this analysis, we need two two-dimensional arrays, each of which is exactly the same size. One for storing thunder information, one for storing information about the game the player is playing (which places he has scanned and which thunder he has marked……) .
If we were making a 9 by 9 minefield, we would declare the minefield to be 11 by 11.
Why is that? When we play minesweeper, we click on a coordinate, and if the coordinate is safe, it shows the number of mines in eight directions around it. This involves accessing the surrounding coordinates of the boundary coordinates of the minefield, which creates the problem of crossing boundaries.
So we want the 9*9 minefield, declare the 11*11 minefield, at the 9*9 boundary coordinates to access its surrounding coordinates will not be out of bounds, and we can show the player or manipulate the minefield by controlling the coordinate value.
Building a framework
The program will start with the main function.
The main function
Call the test function and return 0.
The test function
Before starting the game, we can create a menu interface where the player can choose to enter the game or exit the program. Menu () implements the menu interface. By obtaining the value entered by the player, in this program, the player enters 1 to enter the game, calls game(), enters 0 to exit the program and breaks out of the loop. Enter other warnings and get the player’s input again. There is a do while loop that can be entered again after the game has finished.
Game function
Declare two two-dimensional arrays, one used to store thunder information mine, one used to store the player’s level of information show. You start by initializing a two-dimensional array with InitBoard(). The mine array is used to store mine information. We treat ‘1’ as mine and ‘0’ as safe, so we initialize the values in the mine array to be all ‘0’. Show stores the player’s game information. We set it to start with all ‘*’, and when the player enters a coordinate, the number of mines around the coordinate will be displayed.
After initialization, print the show array to show the player, and use the DisplayBoard function to show minefields. Mines are then randomly placed in mine, using the SetMine function to accomplish this operation. Use the FindMine function to do the following: Keep asking the player for coordinates until all mines are found, and then determine if the coordinates are safe. If it is not safe, it is mine, output prompt, display mine array, break out of the loop, end the local game. If it is safe, count the number of mines around the coordinate, using the get_mine_count function. Set int to char in the coordinates of the show array using the “number +’0” method.
The way to determine if thunder is completely found is to set a win variable of size 0. When the player finds a safe coordinate, win plus 1,9 *9 minefield has 81 coordinates, among which there are 10 mines, then the safe coordinate has 71. When win<71 proves that all mines have not been found, the program constantly asks the player to input coordinates. When win=71, break out of the loop, output the hint and end the game.
The advanced
This enables a simple version of mine clearance. We played with minesweeper, when clicking on a coordinate, if it is safe and its surrounding coordinates are safe, can open a large area. Our implementation above does not do this. When the player input the coordinate is a safe row, and determine whether there are mines around it, if there are no mines to visit the current coordinates around the eight mines, determine whether there are mines around them, and fill in the number of mines around it, this will use recursion.
Problems encountered: When players enter a coordinate, and this is the coordinates of a safe, so there is whether coordinates around it security, coordinate is also a safe coordinate, around around around continue to determine the coordinates of coordinates, but the player input is around coordinates, the coordinates of the final judging players will enter the input coordinates to continue, which makes it impossible to jump out of the recursion, Keep recursing until the computer crashes……
Solution: declare a two-dimensional array ju of character type. If the coordinate is judged, set the value of the coordinate in JU to 1, so as to distinguish judged and unjudged coordinates.
Code implementation
Gitee.com/Huangy-4826…
Deficiency in
Sometimes the player doesn’t know exactly where all the mines are, but in mine clearance 1, the player points out the safe coordinates, starts with the safe coordinates, opens up a large number of safe coordinates, and exposes the location of the mines, and the program automatically confirms that the game is successful.