“This is the fourth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Mine clearance

The same familiar smell, optimized minesweeper, infinitely closer to real minesweeper.

test.c

I prefer to start with main, I don’t like to go directly to the code, I feel that bloggers will not understand it, I prefer to break it down. Without further ado, directly on the code (ha ha just kidding).

main

{ int input = 0; int num = 3; Int flag = 1; Srand ((unsigned)time(NULL)); // random number start, use rand (); /* Random number start, use rand (); Printf (" Please select :>"); scanf("%d", &input); switch (input) { case 1: if (flag == 0) { flag = 1; Num = 3; } game(); // Play the game break; Case 0: printf(" Exit game \n"); break; default: if (flag == 1) { flag = 0; } num--; If (num == 0) // at this point you have no chance {printf(" You have no chance. \n"); break; } printf(" select wrong, you still have %d chance \n", num); break; } } while (input&&num); // Return 0; }Copy the code

Meun menu function

{
	printf("****************************\n");
	printf("****1.play       0.exit*****\n");
	printf("****************************\n");
}
Copy the code

Game game function

{ char mine[ROWS][COLS]; Char show[ROWS][COLS]; InitBoard(mine, ROWS, COLS,'0'); InitBoard(show,ROWS,COLS,'*'); //DisplayBoard(mine, ROW, COL); DisplayBoard(show, ROW, COL); // Print the chessboard SetMine(mine, ROW, COL); //DisplayBoard(mine, ROW, COL); FindBoard(mine,show, ROW, COL); }}Copy the code

Game. C file

InitBoard Initializes the checkerboard function

Checkerboard initialization, what kind of characters are initialized here? Write dead ‘0’, not show array. Write dead ‘*’, mine array does not work, what to do? So I’m not going to do any of that, I’m going to take a set argument, what do you want to initialize, the argument that you pass to me, and then do it.

{
	int i = 0;
	for (i = 0; i < rows; i++)
	{
		int j = 0;
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;//set参数,你来啥我接啥
		}
	}
}
Copy the code

DisplayBoard Prints checkerboard parameters

Print the checkerboard, and here we print the checkerboard that we see, but we don’t need the outer circle to prevent overflow. Although we are dealing with an array of 99 here, we are still passing an array of 1111, so we use board to receive an array of 11*11

{ int i = 0; printf("---------------------------------------\n"); /* This loop prints out the sequence number before printing the column */ for (I = 0; i <= col; i++) { printf("%2d ", i); %2d} printf("\n"); for (i = 0; i <= col; i++) { if (0 == i) printf(" "); else printf("---"); } printf("\n"); for (i = 1; i <= row; i++) { int j = 0; printf("%2d|",i); For (j = 1; j <= col; j++) { printf(" %c ",board[i][j]); } printf("|"); / / the end of the line to print a | line printf (" \ n "); } for (i = 0; i <= col; If (0 == I) printf(" "); Else printf("-- "); } printf("\n"); printf("---------------------------------------\n"); }Copy the code

SetMine places the mine function

Lay out the mines, put the mines in the mine array

{ int count = EASY_COUNT; Int x = rand() % row + 1; int x = rand() % row + 1; int y = rand() % col + 1; If (board[x][y]! // board[x][y] = board[x][y]! = '*') { board[x][y] = '*'; count--; }}}Copy the code

GetMineCount gets the thunder number function

Get the number of thunder function, because we put the array initialization is ‘0’, ray is’ ‘, but we check from the ray and the surrounding array is a dead 33, so we loop 9 characters add and subtract 9 ‘0’ to get the total number of ‘*’-‘0’, and divide by it to get count

{ int i = 0; int sum = 0; int count = 0; for (i = x - 1; i <= x + 1; i++) { int j = 0; for (j = y - 1; j <= y + 1; j++) { sum = sum + (board[i][j] - '0'); } } return count = sum / ('*' - '0'); // We want to be flexible with character and integer toggling}Copy the code

PutOneMine mine discharging function

This code is designed to protect those with black faces from playing the game

{ while (1) { int x = rand() % ROW + 1; int y = rand() % COL + 1; if (board[x][y] ! = '*') { board[x][y] = '*'; // Put the thunder into the break; }}}Copy the code

ShowSpread displays the expansion function

Display expansion function, also known as recursive expansion of mine clearance, this function greatly increases the game’s playability, I want to multiple recursion

{ int count = 0; Count = GetMineCount(mine, x, y); If (count == 0) {/* show[x][y] = "; if ((x - 1) > 0 && (y - 1) > 0 && show[x - 1][y - 1] == '*') ShowSpread(mine, show, x - 1, y - 1); if((y-1)>0&&show[x][y-1] == '*') ShowSpread(mine, show, x , y - 1); if ((x + 1) <= ROW && (y - 1) > 0 && show[x - 1][y - 1] == '*') ShowSpread(mine, show, x + 1, y - 1); if ((x - 1) > 0&& show[x - 1][y] == '*') ShowSpread(mine, show, x - 1, y); if ((x + 1) <= ROW&& show[x + 1][y] == '*') ShowSpread(mine, show, x + 1, y); if ((x - 1) > 0 && (y + 1) <= COL && show[x - 1][y + 1] == '*') ShowSpread(mine, show, x - 1, y + 1); if ((y + 1) <= COL && show[x][y + 1] == '*') ShowSpread(mine, show, x, y + 1); if ((x + 1) <= ROW && (y + 1) <= COL && show[x + 1][y + 1] == '*') ShowSpread(mine, show, x + 1, y + 1); } else { show[x][y] = count + '0'; }}Copy the code

FindBoard searches for thunder functions

For mine detection, we check the mine array and then transfer the information to the show array. We still operate the 99 array, but mine Show has always been the 1111 array, in order to prevent mine overflow

{ int x = 0; int y = 0; int num = 3; int flag = 1; int mine_flag = 1; int win = 0; While (num&&(win<row* col-easy_count)) {printf(" please enter the target :>"); scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (flag == 0) { flag = 1; num = 3; } if (mine[x][y] == '*') { if (mine_flag == 1) { mine[x][y] = '0'; PutOneMine(mine); //DisplayBoard(mine, ROW, COL); */ int count = GetMineCount(mine, x, y); */ int count = GetMineCount(mine, x, y); show[x][y] = count + '0'; DisplayBoard(show, ROW, COL); DisplayBoard(show, ROW, COL); DisplayBoard(show, ROW, COL); } else {printf(" I'm sorry you were killed \n"); DisplayBoard(mine, ROW, COL); break; }} else {/* if it is not a mine, we search for the mines in 8 locations around the location */ win++; mine_flag = 0; / / here thunder tag, there is no the if (mime [x] [y] = = '0' && show [x] [y] = = '*') {ShowSpread (mime, show, x, y); DisplayBoard(show, ROW, COL); } //int count = GetMineCount(mine,x,y); //show[x][y] = count + '0'; DisplayBoard(show, ROW, COL); DisplayBoard(show, ROW, COL); } } else { if (flag == 1) { flag = 0; } num--; If (num == 0) {printf(" you have no chance "); break; } printf(" input error, you still have %d input opportunity ", num); }} if (win == row * col-easy_count) printf(" Congratulations! \n"); }Copy the code

game.h

The macro

So I’m going to go straight to the medium 15 by 15

#define COL 15 #define ROWS ROW+2 #define COLS COL+2 #define EASY_COUNT 30Copy the code

Function declaration

 
void DisplayBoard(char board[ROWS][COLS], int row, int col);
 
void SetMine(char board[ROWS][COLS], int row, int col);
 
void FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS],int row, int col);
 
int GetMineCount(char board[ROWS][COLS], int x, int y);
 
void PutOneMine(char board[ROWS][COLS]);
 
void ShowSpread(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y);
Copy the code

Test pattern

The number of restrictions

killing


The first step began to step on thunder, spring invincible effect coordinate 2 3 are thunder

But just in case you die in the first place, look at two pictures 2 and 3, the thunder is removed, and then it doesn’t happen as long as you don’t hit it continuously

Won the

Nine, two is the only one without thunder