More than the previous blog “ Deep fried ” And marking , Unmark function !!!

Article catalogue

<> preface

Improve the shortcomings of the previous blog .

<> one , The number of surrounding mines is 0, Continuous automatic turning chess pieces

Mine sweeping doesn't have to click on a chess piece , If it is displayed as 0, It means that there is no thunder around , In this way, several pieces around will automatically turn over , If there is something around it, it shows as 0 of , There are automatic turns around , In turn ; Such a function I call “ Explosion screen ”, Sometimes it really blows up a lot , It will be better to clear the mine then ; Ray, let me go 10 individual , I wrote about the last blog , They are all very detailed ;

The code is as follows :
void Findmines(char mine[Rows][Cols], char show[Rows][Cols], int x, int y) {
if (x == 0 || y == 0 || x == Rows - 1 || y == Cols - 1) { return ; } if
(show[x][y] != '*') return; if (show[x][y] == '*') { int ret = minenum(mine, x,
y); show[x][y] = ret + '0'; } if (show[x][y] == '0') { Findmines(mine, show, x
- 1, y - 1); Findmines(mine, show, x - 1, y); Findmines(mine, show, x - 1, y +
1); Findmines(mine, show, x, y - 1); Findmines(mine, show, x, y + 1);
Findmines(mine, show, x + 1, y - 1); Findmines(mine, show, x + 1, y);
Findmines(mine, show, x + 1, y + 1); }
<> two , Marking and unmarking

Marking and canceling functions are similar , When we can determine the location of the mine, we can mark the location of the mine , When we mark the wrong position, we can cancel the mark ; The code implementation is as follows :
void concel(char show[Rows][Cols], int x, int y) { if (x == 0 || y == 0 || x
== Rows - 1 || y == Cols - 1) { printf(" Illegal coordinates , Please re-enter :\n"); return; } if
(show[x][y] != '!') return; if (show[x][y] == '!') { show[x][y] = '*';
Initshow(show, Row, Col); } } void add(char show[Rows][Cols], int x, int y) {
if (x == 0 || y == 0 || x == Rows - 1 || y == Cols - 1) {
printf(" Illegal coordinates , Please re-enter :\n"); return; } if (show[x][y] != '*') return; if
(show[x][y] == '*') { show[x][y] = '!'; Initshow(show, Row, Col);; } }
3. The total code is as follows

game.c
#include "game.h" void Mine(char board[Rows][Cols], int rows, int cols, char
set) { int i = 0, j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols;
j++) { board[i][j] = set; } } } void Initshow(char show[Rows][Cols], int row,
int col) { int i = 0, j = 0; printf("-------- Welcome to the minesweeping game --------\n"); for (i = 0;
i <= col; i++) { printf("%d ", i); } printf("\n"); for (i = 1; i <= row; i++) {
printf("%d ", i); for (j = 1; j <= col; j++) { printf("%c ", show[i][j]); }
printf("\n"); } printf("-------- Welcome to the minesweeping game --------\n"); } void Mineset(char
mine[Rows][Cols], int row, int col) { int n = mines; while (n) { int x = rand()
% row + 1; int y = rand() % col + 1; if (x <= row && x >= 1 && y <= col && y >=
1) { mine[x][y] = '1'; n--; } } } void concel(char show[Rows][Cols], int x, int
y) { if (x == 0 || y == 0 || x == Rows - 1 || y == Cols - 1) {
printf(" Illegal coordinates , Please re-enter :\n"); return; } if (show[x][y] != '!') return; if
(show[x][y] == '!') { show[x][y] = '*'; Initshow(show, Row, Col); } } void
add(char show[Rows][Cols], int x, int y) { if (x == 0 || y == 0 || x == Rows -
1 || y == Cols - 1) { printf(" Illegal coordinates , Please re-enter :\n"); return; } if (show[x][y] != '*')
return; if (show[x][y] == '*') { show[x][y] = '!'; Initshow(show, Row, Col);; }
} int minenum(char mine[Rows][Cols], int x, int y) { return mine[x - 1][y - 1]
+ mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] +
mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0'; } void
Checkmines(char show[Rows][Cols],int* x, int* y) { int i = 0, j = 0; for (i =
0; i < Row; i++) { for (j = 0; j < Col; j++) { if (show[i][j] != '*') x++; if
(show[i][j] == '!') y++; } } } void Findmines(char mine[Rows][Cols], char
show[Rows][Cols], int x, int y) { if (x == 0 || y == 0 || x == Rows - 1 || y ==
Cols - 1) { return ; } if (show[x][y] != '*') return; if (show[x][y] == '*') {
int ret = minenum(mine, x, y); show[x][y] = ret + '0'; } if (show[x][y] == '0')
{ Findmines(mine, show, x - 1, y - 1); Findmines(mine, show, x - 1, y);
Findmines(mine, show, x - 1, y + 1); Findmines(mine, show, x, y - 1);
Findmines(mine, show, x, y + 1); Findmines(mine, show, x + 1, y - 1);
Findmines(mine, show, x + 1, y); Findmines(mine, show, x + 1, y + 1); } } void
menu1() { printf("***************************\n"); printf("**** 0. Exit the game
****\n"); printf("**** 3. Marker mine ****\n"); printf("**** 4. Unmark ****\n");
printf("**** 5. Continue the game ****\n"); printf("***************************\n"); } void
Findmine(char mine[Rows][Cols], char show[Rows][Cols], int row, int col) { int
rets = 0,qi=0; while (1) { int x = 0, y = 0; printf(" Please enter coordinates >\n"); scanf("%d%d",
&x, &y); if (x <= row && x >= 1 && y <= col && y >= 1) { Findmines(mine, show,
x, y); Initshow(show, Row, Col); if (mine[x][y] == '1') {
printf(" It must have been a mistake , How can you lose ? Only one more round \n"); show[x][y] = '1'; Initshow(mine, Row, Col);
return; } } else { printf(" Illegal input coordinates , Please re-enter :\n"); } Checkmines(show,&rets, &qi); if
(rets == Row * Col - mines || qi == mines) { printf(" congratulations , Simply win , Little fun \n");
return; } while (1) { menu1(); int g = 0; printf(" Please enter >\n"); scanf("%d", &g); if
(g == 3) { int x = 0, y = 0; printf(" Please enter coordinates :\n"); scanf("%d%d", &x, &y);
add(show, x, y); } else if (g == 4) { int x = 0, y = 0; printf(" Please enter coordinates :\n");
scanf("%d%d", &x, &y); concel(show, x, y); } else if (g == 5) break; else goto
again; } } again:return; }
<>text.c
#include "game.h" void menu() { printf("***************************\n");
printf("**** 1. play a game ****\n"); printf("**** 0. Exit the game ****\n");
printf("***************************\n"); } void game() { char mine[Rows][Cols]
= { 0 }; char show[Rows][Cols] = { 0 }; Mine(mine, Rows, Cols, '0'); Mine(show,
Rows, Cols, '*'); Initshow(show, Row, Col); Mineset(mine, Row, Col);
//Initshow(mine, Row, Col); //Mineset(mine, Row, Col); Findmine(mine, show,
Row, Col); } int main() { int input = 0; srand((unsigned int)time(NULL)); do {
menu(); printf(" Please enter >\n"); scanf("%d", &input); switch (input) { case 1:game();
break; case 0:printf(" Exit the game \n"); break; default: break; } } while (input);
return 0; }

game.h
#include <stdio.h> #include <math.h> #include <stdlib.h> #include <time.h>
#define mines 10 #define Row 9 #define Col 9 #define Rows Row+2 #define Cols
Col+2 void Mine(char board[Rows][Cols], int rows , int cols, char set); void
Initshow(char show[Rows][Cols], int row, int col); void Mineset(char
mine[Rows][Cols], int row, int col); void Findmine(char mine[Rows][Cols], char
show[Rows][Cols], int row, int col); void Findmines(char mine[Rows][Cols], char
show[Rows][Cols], int y, int x); void Check(char show[Rows][Cols],int* x, int*
y); void add(char show[Rows][Cols], int x, int y); void concel(char
show[Rows][Cols], int x, int y);
summary

Thank you for reading , I think this improved minesweeping code is much more optimized than the code in the previous issue , You can try the game experience by yourself , Personally, I think it's better ; Sanzi will be improved later , Gobang will also come true , However, there is no time to improve the code recently , All may be a little slow , It will take a little more time ;

Technology
Daily Recommendation
views 6
views 5