<>C/C++ Mine clearance

*
For minesweeping games , Believe a lot C/C++ All beginners hope to be able to play this game

*
I use C++ Written , If you want to use C You can modify the small partner written in language

*
Let's sort out our ideas first

*

1. Initialize chessboard , We need two chessboards here , Players who have played mine sweeping games should know , The interface for you is ***** In this way , You need to sweep some by yourself , But for the people who designed the game , How many mines have been randomly arranged .

*
2. So initialize the designer's chessboard first and Player chessboard

*
The designer's chessboard is as follows ：

*
The player's chessboard is as follows ：
*** Initialize chessboard *** void Myboard::InitBoard(char board[ROWS][COLS],int row,int col,
char ret) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j <
col; j++) { board[i][j] = ret; // Initialize playback * 0 } } } Note that the row and column coordinates are outside the red box
* 3. See the designer's board and the player's board , We should know The chessboard is 10*10 Lattice of . There are row and column coordinates outside , So our rows and columns should be set to ROWS：11,COLS：11.
*** Print chessboard *** void Myboard::DisplayBoard(char board[ROWS][COLS], int row, int col)
{ int i = 0; int j = 0; for (i = 0; i < row; i++) { cout << i << " "; } cout <<
endl; for (i = 1; i < row-1; i++) { cout << i << " "; for (j = 1; j < col; j++)
{ cout << board[i][j] << " "; } cout << endl; } cout << 10 << " "; for (i = 1; i
< row; i++) { cout << board[i] << " "; } cout << endl; }
* 4. After initialization, the designer mines the chessboard ,1 Express ray .
*** Randomly arrange thunder on the designer's chessboard , I laid mine here 10 A ray *** void Myboard::Set_mine(char board[ROWS][COLS]) {
int x= 0, y = 0; srand((unsigned)time(NULL)); // Random number seed int count = Count; while (
count) { x = rand() % 10 + 1; // produce 1-10 Fill the random number in the chessboard y = rand() % 10 + 1; if (board[
x][y] =='0') { board[x][y] = '1'; } count--; } }
* 5. The first time you sweep a mine, you usually won't be killed , So it should be set up here not to be killed for the first time void Myboard::First_safe() { int x = 0;
int y= 0; char ch = 0; // Displays the number of surrounding mines int set = 1; cout << " Please enter coordinates for minesweeping " << endl;
while (1) { cin >> x >> y; if (x >= 1 && x <= 10 && y >= 1 && y <= 10) { if (
menu[x][y] == '1') // He was killed by thunder for the first time { menu[x][y] = '0'; ch = count_mine(x, y); show[x]
[y] = ch + '0'; // Show it to players ,ch How much , It means how much thunder there is around this point open_mine(x, y); //
Surrounding coordinates ,, That coordinate expands around that coordinate without thunder while (set) { int x = rand() % 10 + 1; int y = rand() %
10 + 1; // Because I was killed by thunder for the first time , So it is necessary to randomly set the place without thunder as thunder if (menu[x][y] == '0') { menu[x][y] =
'1'; set--; break; } }break; // Jump function } if (menu[x][y] == '0') { ch = count_mine(
x, y); show[x][y] = ch + '0'; open_mine(x, y); break; } } else { cout <<
" Input error , Please re-enter !" << endl; } } }
* 6. If the eight points around the scanned coordinate point have no mines , Count the number of mines , Conditions for judging whether a player wins int Myboard::count_mine(int x, int y
) { int count = 0; if (menu[x - 1][y - 1] == '1') count++; if (menu[x - 1][y] ==
'1') count++; if (menu[x - 1][y + 1] == '1') count++; if (menu[x][y - 1] == '1')
count++; if (menu[x][y + 1] == '1') count++; if (menu[x + 1][y - 1] == '1')
count++; if (menu[x + 1][y] == '1') count++; if (menu[x + 1][y + 1] == '1')
count++; return count; }
* 7. If there are no mines at eight points around mine sweeping , Then continue to detect whether there is thunder around these eight points , Use recursion void Myboard::open_mine(int x,int y)
{ if (menu[x - 1][y - 1] == '0') show[x - 1][y - 1] = count_mine(x - 1, y - 1) +
'0'; if (menu[x - 1][y] == '0') show[x - 1][y] = count_mine(x - 1, y) + '0'; if
(menu[x - 1][y + 1] == '0') show[x - 1][y + 1] = count_mine(x - 1, y + 1) + '0';
if (menu[x][y - 1] == '0') show[x][y - 1] = count_mine(x, y - 1) + '0'; if (menu
[x][y + 1] == '0') show[x][y + 1] = count_mine(x, y + 1) + '0'; if (menu[x + 1][
y- 1] == '0') show[x + 1][y - 1] = count_mine(x + 1, y - 1) + '0'; if (menu[x +
1][y] == '0') show[x + 1][y] = count_mine(x + 1, y) + '0'; if (menu[x + 1][y + 1
] == '0') show[x + 1][y + 1] = count_mine(x + 1, y + 1) + '0'; }
* 8. Finally, mine clearance int Myboard::Sao_mine() { int x = 0; int y = 0; char ch = 0; cout
<< " Please enter coordinates for minesweeping \n"; cin >> x >> y; if (x >= 1 && x <= 10 && y >= 1 && y <= 10) { if
(menu[x][y] == '0') { ch = count_mine(x, y); show[x][y] = ch + '0'; open_mine(x,
y); // Surrounding coordinates ,, That coordinate expands around that coordinate without thunder if (Count_show_mine() == Count) { DisplayBoard(
show, ROWS, COLS); cout << " Player wins " << endl; return 0; } } else if(menu[x][y]=='1')
{ return 1; } } return 0; }
* 8. Victory judgment conditions , If the last remaining * Equal to the set number of Mines , The player wins int Myboard::Count_show_mine() { int count =
0; int i = 0; int j = 0; for (i = 1; i < ROWS; i++) { for (j = 1; j < COLS; j++)
{ if (show[i][j] == '*') { count++; } } } return count; } // Header file display #pragma once
#include<iostream> using namespace std; #include<ctime> #define ROWS 11 #define
COLS 11 #define Count 10 // Number of thunder class Myboard { public: char menu[ROWS][COLS];
// Designer chessboard char show[ROWS][COLS]; // Player chessboard // 1. Initialize chessboard void InitBoard(char board[
ROWS][COLS], int row, int col, char ret); // 2. Print chessboard void DisplayBoard(char board
[ROWS][COLS], int row, int col); // 3. Number of thunder void Set_mine(char board[ROWS][COLS]);
// 4. Count the number of mines around the coordinates int count_mine(int x, int y); // 5. Avoid being blown up for the first time void First_safe();
// 6. If there is no thunder in the surrounding eight coordinates , Then continue to expand Recursion can be used void open_mine(int x, int y); // 7. mine clearance int
Sao_mine(); // 8. Determine the remaining number of unknown areas int Count_show_mine(); }; // Main function display #include"game.h"
void menu() { cout << "***********************" << endl; cout << "****** 1.play
*****" << endl; cout << "****** 0.exit *****" << endl; cout <<
"***********************" << endl; } void Game() { Myboard game; //
1. Initialize player and designer chessboards game.InitBoard(game.menu, ROWS, COLS, '0'); game.InitBoard(game.
show, ROWS, COLS, '*'); // 2. Mine the designer's chessboard game.Set_mine(game.menu); // 3. Print designer chessboard
game.DisplayBoard(game.menu, ROWS, COLS); cout << endl; // 4. Print player chessboard game.
DisplayBoard(game.show, ROWS, COLS); // 5. Avoid being blown up for the first time game.First_safe(); cout <<
endl; // 6. One step win situation if (game.Count_show_mine() == Count) { game.DisplayBoard(game
.menu, ROWS, COLS); cout << " Player wins " << endl; }game.DisplayBoard(game.show, ROWS,
COLS); while (1) { int count = game.Sao_mine(); if (game.Count_show_mine() ==
Count) { game.DisplayBoard(game.menu, ROWS, COLS); // Print designer chessboard cout << " Player wins " <<
endl; break; } if (count) { cout << " Unfortunately , You were killed by thunder !" << endl; game.DisplayBoard(
game.menu, ROWS, COLS); // Print designer chessboard break; }game.DisplayBoard(game.show, ROWS,
COLS); } //game.Set_menu(game.show); //game.DisplayBoard(game.show, ROWS, COLS);
} int main() { int input; do { menu(); cout << " Please select : 1. Start the game 0. Exit the game " << endl; cin
>> input; switch (input) { case 1:Game(); break; case 0:cout << " Exit the game !"<<endl;
exit(0); break; default: cout << " Selection error , Please reselect " << endl; break; } } while (1); }
// The following is the function display Function function + Main function + Header file to start the game #include"game.h" // Initialize chessboard void Myboard::InitBoard
(char board[ROWS][COLS],int row,int col,char ret) { int i = 0; int j = 0; for (i
= 0; i < row; i++) { for (j = 0; j < col; j++) { board[i][j] = ret; // Initialize playback * 0 }
} } // Print chessboard void Myboard::DisplayBoard(char board[ROWS][COLS], int row, int col)
{ int i = 0; int j = 0; for (i = 0; i < row; i++) { cout << i << " "; } cout <<
endl; for (i = 1; i < row-1; i++) { cout << i << " "; for (j = 1; j < col; j++)
{ cout << board[i][j] << " "; } cout << endl; } cout << 10 << " "; for (i = 1; i
< row; i++) { cout << board[i] << " "; } cout << endl; } // Set the number of Mines void
Myboard::Set_mine(char board[ROWS][COLS]) { int x = 0, y = 0; srand((unsigned)
time(NULL)); int count = Count; while (count) { x = rand() % 10 + 1; //
produce 1-10 Fill the random number in the chessboard y = rand() % 10 + 1; if (board[x][y] =='0') { board[x][y] = '1';
} count--; } } // Count the number of mines around the current coordinate int Myboard::count_mine(int x, int y) { int count
= 0; if (menu[x - 1][y - 1] == '1') count++; if (menu[x - 1][y] == '1') count++;
if (menu[x - 1][y + 1] == '1') count++; if (menu[x][y - 1] == '1') count++; if (
menu[x][y + 1] == '1') count++; if (menu[x + 1][y - 1] == '1') count++; if (menu
[x + 1][y] == '1') count++; if (menu[x + 1][y + 1] == '1') count++; return count
; } // Avoid being blown up for the first time void Myboard::First_safe() { int x = 0; int y = 0; char ch = 0;
// Displays the number of surrounding mines int set = 1; cout << " Please enter coordinates for minesweeping " << endl; while (1) { cin >> x >> y;
if (x >= 1 && x <= 10 && y >= 1 && y <= 10) { if (menu[x][y] == '1') // He was killed by thunder for the first time
{ menu[x][y] = '0'; ch = count_mine(x, y); show[x][y] = ch + '0'; open_mine(x, y
); // Surrounding coordinates ,, That coordinate expands around that coordinate without thunder while (set) { int x = rand() % 10 + 1; int y =
rand() % 10 + 1; // Because I was killed by thunder for the first time , So it is necessary to randomly set the place without thunder as thunder if (menu[x][y] == '0') { menu[
x][y] = '1'; set--; break; } }break; // Jump function } if (menu[x][y] == '0') { ch =
count_mine(x, y); show[x][y] = ch + '0'; open_mine(x, y); break; } } else { cout
<< " Input error , Please re-enter !" << endl; } } } // If there is no thunder in the surrounding eight coordinates , Then continue to expand Recursion can be used void Myboard::
open_mine(int x,int y) { if (menu[x - 1][y - 1] == '0') show[x - 1][y - 1] =
count_mine(x - 1, y - 1) + '0'; if (menu[x - 1][y] == '0') show[x - 1][y] =
count_mine(x - 1, y) + '0'; if (menu[x - 1][y + 1] == '0') show[x - 1][y + 1] =
count_mine(x - 1, y + 1) + '0'; if (menu[x][y - 1] == '0') show[x][y - 1] =
count_mine(x, y - 1) + '0'; if (menu[x][y + 1] == '0') show[x][y + 1] =
count_mine(x, y + 1) + '0'; if (menu[x + 1][y - 1] == '0') show[x + 1][y - 1] =
count_mine(x + 1, y - 1) + '0'; if (menu[x + 1][y] == '0') show[x + 1][y] =
count_mine(x + 1, y) + '0'; if (menu[x + 1][y + 1] == '0') show[x + 1][y + 1] =
count_mine(x + 1, y + 1) + '0'; } // mine clearance int Myboard::Sao_mine() { int x = 0;
int y= 0; char ch = 0; cout << " Please enter coordinates for minesweeping \n"; cin >> x >> y; if (x >= 1 && x <= 10
&& y >= 1 && y <= 10) { if (menu[x][y] == '0') { ch = count_mine(x, y); show[x][
y] = ch + '0'; open_mine(x, y); // Surrounding coordinates ,, That coordinate expands around that coordinate without thunder if (Count_show_mine()
== Count) { DisplayBoard(show, ROWS, COLS); cout << " Player wins " << endl; return 0; } }
else if(menu[x][y]=='1') { return 1; } } else { cout << " Input error , Please re-enter !" << endl; }
return 0; } // Judge the remaining number int Myboard::Count_show_mine() { int count = 0; int i = 0;
int j= 0; for (i = 1; i < ROWS; i++) { for (j = 1; j < COLS; j++) { if (show[i]
[j] == '*') { count++; } } } return count; }

Technology
Daily Recommendation
views 9
views 8