Get the App
SLTechnology News&Howtos  ›  Development  › 

An example Analysis of the implementation of Minesweeping Game in C language

Shulou Source: shulou.com Published: 2022-06-01 18:14:51 10月03日 Update

This article is to share with you the content of an example analysis of the implementation of minesweeping games in C language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

One, create a menu

First make it clear what to do and choose the appropriate sentence to implement the idea:

Test.c void menu () {printf ("*\ n"); printf ("* 1.play *\ n"); printf ("* 0.exit * *\ n"); printf ("*\ n") } void test () {int input = 0; do {menu (); printf ("Please select:"); scanf ("% d", & input) Switch (input) {case 1: / / minesweeping break; case 0: printf ("quit the game\ n"); break Default: printf ("selection error\ n"); break;}} while (input);} int main () {test (); return 0;} II, create game content

Create a heavy blow in the game function. In the test () function, case1: call game.

Case 1 game / minesweeping (); 1. Scene creation and initialization

Here we have to understand that the mine in our game is arranged in advance, and the scene presented in front of us is masked, so we create two scenes, mine and show, both of which are two-dimensional arrays.

When minesweeping, we have to calculate whether there is a circle around the input coordinates, so we have to consider the problem of crossing the boundary in the boundary coordinates, so if the array shown in front of us is the array of xroomy, then the size of the actual array should be the size of (xroom2) * (yellow2).

To make it easy to change the size of the scene, we use the macro definition (in game.h) when creating rows and columns.

Game.h # include / / scene size for players # define ROW 9#define COL 9 / / scene true size anti-crossing limit # define ROWS ROW+2#define COLS COL+2 / / function declaration void initboard (char board [ROWS] [COLS], int rows, int cols, char set); / / initialize game.c / / function definition void initboard (char board [ROWS] [COLS], int rows, int cols, char set) / / initialize scene {int I = 0 Int j = 0; for (I = 0; I < rows; iTunes +) {for (j = 0; j < cols; jacks +) {board [I] [j] = set / / set receives characters from test.h} test.c void game () {/ / create array char mine [ROWS] [COLS] = {0}; / / char show [ROWS] [COLS] = {0} / / check mine / / initialize the mine array to full'0' initboard (mine,ROWS,COLS,'0'); / / initialize the show array to full'* 'initboard (show, ROWS,COLS,' *);} 2. Scene printing game.h # include / / scene size for players # define ROW 9#define COL 9 / / scene true size anti-crossing limit # define ROWS ROW+2#define COLS COL+2 / / function declaration void initboard (char board [ROWS] [COLS], int rows, int cols, char set); / / initialize void displayboard (char board [ROWS] [COLS], int row, int col) / / print scene game.c void displayboard (char board [ROWS] [COLS], int row, int col) / / print scene {int iTuno; int j = 0; for

Tags: Scenarios arrays functions sizes coordinates minesweeping inputs players selections numbers jacks numbers contents chessboards random numbers examples language analysis success regret Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno Xiaomi MariaDB NVidia