Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C++ Code to realize Minesweeper Mini Game

Shulou Source: shulou.com Published: 2022-06-01 13:10:07 10月13日 Update

This article mainly introduces how to use C++ code to achieve mine clearance Mini Game related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone after reading this article on how to use C++ code to achieve mine clearance Mini Game will have a harvest, let's take a look.

Split into two source files and a header file

Note: this string of code is not complete and cannot be used to expand the surrounding coordinates directly when the number of mines around the checked coordinates is 0.

Header file: game.h

# include # define count 10 / / number of defined rows-ROW, column-COL#define ROW 9#define COL 9#define ROWS ROW+2 / / add more to facilitate code # define COLS COL+2// to initialize the chessboard, and all declared functions implement void InitBoard (char board [ROWS] [COLS], int rows,int cols) in game.c; / / print chessboard void DisplayBoard (char board [ROW] [COL], int row.int col) / / Mine void SetMine (char board [ROW] [COLS], int row,int col); / / Mine finding void FindMine (char mine [] [COLS], char show [] [COLS], int row,int col)

First source file: saolei.c

# include "game.h" / / what needs to be included and declared is completed in game.h void menu () {printf ("\ n"); printf ("1.play\ n"); printf ("0.exit\ n"); printf ("\ n");} void game () {/ / ROW and COL defines char mine [ROWS] [COLS] in game.h / / Mine-buried chessboard char show [ROWS] [COLS]; / / displayed chessboard / / initialize chessboard / / functions in game () are declared in game.h to implement InitBoard (mine,ROWS,COLS,'0') in game.c; / / 0 means mine-free, 1 means mine-free InitBOard (show,ROWS,COLS,'*'); / / print chessboard DisplayBoard (show,ROW,COL) / / Mine SetMine (mine,ROW,COL); / / Mine finding FindMine (mine,show,ROW,COL);} int main () {srand ((unsigned int) time (NULL)); / / generate random number int input = 0; do / / do-while cycle {menu (); / / (1--play 0--exit) is also the conditional printf of do-while cycle ("Please choose:") Scanf (% d, & input); switch (input) {case 1: printf ("start the game\ n"); game (); break; case 0: printf ("quit the game"); break; default: printf ("wrong choice, please re-enter"); break;}} while (input) return 0;}

Second source file: game.c

# include "game.h" void InitBoard (char board [ROWS] [COLS], int rows,int cols,char set) {int I = 0; int j = 0; for (I = 0 x I < rows;i++) {for (j = 0 x j < cols;j++) {board [I] [j] = set;} void DisplayBoard (char board [ROWS] [COLS], int row,int col) {int I = 0; int j = 0; for (I = 0 x I)

Tags: Code chessboard quantity Mini Game caterpillar minesweeping coordinates source file knowledge input number representative location content function file article real loop select Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi Shulou Tech Info Redmi Microsoft