<> Minesweeper games

*
First of all, let's analyze the game of mine clearance , Lattice in point , Bomb game over , There is no bomb to indicate how many mines are around this grid , And write the number in the grid . Finally, all mines are discharged , Game victory .
The files to be created are
test.c : Used to write the main function and start the selection menu section
game.c: The implementation of the function is here
game.h : The declaration of the function and the header file to be referenced
<> Game realization ideas

*
Create two 2D arrays , To simulate two chessboards , first mine checkerboard 11x11 There are no bombs . Why 11x11 We hope to count bombs in the future , It won't cross the border , Because the scale of the chessboard is 9x9

* the second show Chessboards are used to store bombs , Random storage 10 A bomb , And it will need to be printed to players in the future .

*
Next, the player needs to check the bomb , Checking bombs requires counting the number of bombs around the grid , If a bomb dies , game over . If there were no bombs , You need to get the number of bombs around and print it on the show In the chessboard of .
<> Specific implementation code

test.c file
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include "game.h" void
menu() { printf(" ******* 1 Start the game **********\n"); printf(" ******* 0 Quit the game
**********\n\n"); } void game() { // Create two arrays of storage mines char mine[ROWS][COLS] = { 0 };
// Lay out the chessboard of thunder char show[ROWS][COLS] = { 0 }; // After checking, the chessboard of Lei is displayed // Function to initialize two chessboards InitBoard(mine
,ROWS,COLS,'0'); InitBoard(show, ROWS, COLS, '*'); // Print a checkerboard
//DisplayBoard(mine, ROW, COL ); //DisplayBoard(show, ROW, COL); // Bomb placement SetMine(
mine, ROW, COL); //DisplayBoard(mine, ROW, COL); // Print and see the bomb DisplayBoard(show ,ROW
, COL); // Bomb detection FindMine(mine,show, ROW, COL); } void test () { int input = 0;
srand((unsigned int)time(NULL)); // Get a random number We need a bomb in the back do { menu(); printf(" Please select :");
scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf(
" Quit the game "); printf("\n"); break; default: system("cls"); break; } } while (input);
} int main() { test(); }
game.h file
#define _CRT_SECURE_NO_WARNINGS 1 #define ROW 9 // Chessboard expected scale At present, it is 9x9 #define COL 9 #
define ROWS ROW+2 #define COLS COL+2 #define EASY_COUNT 10 // Number of bombs placed At present, it is 10 individual #
include <stdio.h> #include<time.h> #include <stdlib.h> void InitBoard(char board
[ROWS][COLS], int rows, int cols,char set); // Initialize chessboard void DisplayBoard(char board
[ROWS][COLS], int row, int col); // Print chessboard void SetMine(char mine[ROWS][COLS], int
row, int col); // Bomb placement void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS]
,int row,int col); // Bomb removal
game.c file
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void InitBoard(char board[
ROWS][COLS], int rows, int cols,char set) // Initializing chess and cards { int i = 0; int j = 0; for (i
= 0; i < rows; i++) { for(j=0;j<cols;j++) { board[i][j] = set; } } } void
DisplayBoard(char board[ROWS][COLS], int row, int col) // The message is 11 Chessboard , But just print 99 checkerboard {
int i = 0; int j = 0; for (i = 0; i <= col; i++) // Print the horizontal coordinates of the chessboard Easy to view { printf("%d ",
i); } printf("\n"); for (i = 1; i <= row; i++) { printf("%d ", i); // Print the ordinate of chessboard
Easy to view for (j = 1; j <= col; j++) { printf("%c ", board[i][j]); // Print chessboard } printf(
"\n"); } } void SetMine(char mine[ROWS][COLS], int row, int col) // Set up the bomb { int
count= EASY_COUNT; while (count) { int x = rand() % row + 1; // The bomb's setting range is random 1
-9 coordinate int y = rand() % col + 1; if (mine[x][y] == '0')// Drop the bomb to { mine[x][y] = '1';
count--; } } } int GetMineCount(char mine[ROWS][COLS], int x, int y)// Count the number of bombs around
{ return (mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y -
1] + mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] -
8 * '0'); // At first, bombs were character numbers Character number Subtract character zero That's the number } void FindMine(char mine[ROWS][COLS],
char show[ROWS][COLS], int row, int col) // Bomb removal { int x = 0; int y = 0; int win
= 0; while (win < row*col - EASY_COUNT) // It's not the number of bombs { printf(" Please enter the coordinates to be checked , format : That's ok Space column
enter "); scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if
(mine[x][y] == '1') // discharge '1' The location of the bomb represents the bomb { printf(" Crackling , It was blown down , game over \n"); DisplayBoard(
mine, row, col); break; } else { int count = GetMineCount(mine, x, y);
// Count the number of thunder and circle around show[x][y] = count + '0'; // Number plus character zero = Character number The number of thunder will exist show In the chessboard system(
"cls"); DisplayBoard(show, row, col); // A thunderbolt Print results once win++; // Statistics of mine clearance times } } else {
printf(" Input coordinate error , Please re-enter !\n"); } } if (win == row * col - EASY_COUNT) // Conditions for the end of the game {
printf(" congratulations , Successful mine clearance \n"); DisplayBoard(mine, row, col); } }
<> Running results

Technology
©2019-2020 Toolsou All rights reserved,
A summary of the computer retest interview questions var a = 0, b = 0; function A(a) {A = function (b)... An interview question about scope 【 Data structure and algorithm 1】 Sparse array 2020—— Net tripod cup part writeupPTA-MOOC《Python Programming Zhejiang University 》 Puzzle A Chapter 2 programming problems python closure uniapp Dynamic routing parameter transfer ( Delivery object ) How to use it Python Batch import Excel Combined use Pandas integration Lambda Expression and functional interface 【Java Details of knowledge points 3】 Serializable and Deserialize