In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to realize the minesweeping game with recursive clearing field in c language". In the daily operation, I believe that many people have doubts about how to realize the minesweeping game with recursive clearing field in c language. The editor has consulted all kinds of data and sorted out the simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about how c language can realize the minesweeping game with recursive clearing field. Next, please follow the editor to study!
First, design ideas
Everyone must have played minesweeping.
This is a standard minesweeper. For code implementation, we need to consider the following points:
1. Design and initialization of chessboard
two。 Put a mine on the chessboard
3. Statistical number of mines
4. How to achieve the "one piece" effect
5. The judgment of winning or losing
Next, let's do some specific operations.
two。 Implementation method 1. Printing of menus
For any game, the menu is essential, but also the simplest part, directly on the code
Void menu () {printf ("- minesweeping -\ n"); printf ("- 1. Start the game -\ n "); printf ("-0. Quit the game -\ n ");} int main () {srand ((unsigned int) time (NULL)); int a = 0; do {menu (); scanf ("% d ", & a) If (a = = 0) {break;} game ();} while (a); return 0;}
Among them, srand is to find the random value, which is used to arrange the mine.
2.game function
After the main menu, enter the game function. In the game function, we will start the main game parts, such as chessboard initialization and printing. Of course, these steps are completed by functions. The game function is only equivalent to a series of game modules.
Void game () {char mine [ROWS] [COLS]; char show [ROWS] [COLS]; initeboard (mine, ROWS, COLS,'0'); initeboard (show, ROWS, COLS,'*'); / / initial chessboard displayboard (show, ROW, COL); / / print chessboard mine_make (mine, ROW, COL); / / set mine / / displayboard (mine, ROW, COL) Find_mine (mine, show, ROWS, COLS); / / search mine} 3. Initialization and printing of chessboard
As you can see, I started two chessboards in the game function. Why two?
In fact, one is to place mines, and the other is to show players, why not use one? we need to use '1mines in the mine release place, which means that we use' 0mines in the mine release place, so it will be easier to calculate the number of mines around a coordinate. Next, take a look at a piece of code:
# define _ CRT_SECURE_NO_WARNINGS#include # define ROW 10#define COL 10#define ROWS ROW+2#define COLS COL+2
Here we see that a ROWS and COLS are defined. Why?
When minesweeping, when you confirm a point, it will check the eight points around this point to see if there are tears. When the coordinates are located on the red line, it is not impossible to judge eight, so with ROWS and COLS, someone will ask: the place where the mine is buried is fine with ROW COL, and there is no need to worry about the mine running to the frame we added. It's a good idea, but we have two chessboards that must correspond, and there is no case of running out of the mine. Analyze it further and you'll know.
Void initeboard (char board [ROWS] [COLS], int cols, int rows,char s) / / chessboard initialization. Here we use a step-saving method to initialize {int I = 0; int j = 0; for (I = 0; I < rows; iTunes +) {for (j = 0; j < cols) instead of two chessboards. Int +) {board [I] [j] = s;} void displayboard (char board [ROWS] [COLS], int row, int col) / / Checkerboard print {int I = 0; int j = 0; printf (""); / / corresponds to for (j = 0; j) with 0% d printed in the line
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 291
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.