Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C++ to make minesweeper games

Shulou Source: shulou.com Published: 2022-05-31 22:11:06 09月24日 Update

This article mainly explains "how to use C++ to make minesweeper games". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to use C++ to make minesweeper games.

The specific code is as follows

# ifndef SAOLEI_H#define SAOLEI_Hclass Block {friend class Saoleigame;public: Block (); bool isShown (); void setnum (int); int getnum (); bool isbomb (); protected: int num; bool flag_show; int x; int y;}; class Saoleigame {public: Saoleigame (); ~ Saoleigame (); void _ init_ (); void gameStart (); void reflash (); void check (int x, int y); void click (int x, int y); void gameOver (); private: Block juzheng [100]; bool flag Int b [10]; unsigned int score;}; # endif

The above is the header file written.

# include#include#include#include "Saolei.h" using namespace std;Saoleigame::Saoleigame () {_ init_ (); flag = true; score = 0;} Saoleigame::~Saoleigame () {} void Saoleigame::_init_ () {srand (time (NULL)); for (int I = 0; I

< 10; i++ ) { b[i] = -1; } for(int i = 0; i < 10; i ++ ) { bool temp_flag = false; do { int temp = (unsigned int)rand()0; for( int j = 0; j < i; j ++) { if(temp == b[i]) { temp_flag = true; } } if(!temp_flag) { b[i] = temp; } }while(temp_flag); } for(int i = 0; i < 10; i++ ) { juzheng[b[i]].setnum(-1); } for( int i = 0; i < 10; i ++ ) { for( int j = 0; j < 10;j++) { juzheng[i*10+j].x = i+1; juzheng[i*10+j].y = j+1; } } for(int m = 0; m < 10; m ++ ) { for( int n = 0; n < 10 ; n++ ) { check(m + 1, n + 1); } }}void Saoleigame::check(int x, int y){ if(juzheng[(x - 1)*10 + (y - 1)].num == -1)return; int trans = (x - 1)*10 + (y - 1); int number = 0; for( int i = -1; i < 2; i ++ ) { for(int j = -1; j < 2; j ++) { if(!(x + i= 10 || j + y = 10)) { if(juzheng[(x + i - 1)*10 + (y + j - 1)].num == -1) number ++; } } } juzheng[(x - 1)*10 + (y - 1)].setnum(number);}void Saoleigame::click(int x, int y){ if(juzheng[(x- 1)*10 + (y - 1)].num == 0) { for( int i = -1; i < 2; i ++ ) { for(int j = -1; j < 2; j ++) { if(!((x + i 10 )|| (j + y 10)) && !(i == 0&& j ==0) && !juzheng[(x+i- 1)*10 + (y +j- 1)].flag_show){ juzheng[(x+i- 1)*10 + (y +j- 1)].flag_show = true; click(x + i, y + j); } } } } juzheng[(x- 1)*10 + (y - 1)].flag_show = true; return;}void Saoleigame::gameStart(){ do { reflash(); int x, y; cout x>

> y; if (juzheng [(Xmur1) * 10 + (yMuth1)] .isometric () {gameOver (); return;} else {click (x, y);}} while (flag);} void Saoleigame::reflash () {system ("cls"); score = 0; cout

Tags: Minesweeping Clearing production content Learning practicality deeper Code interest practicality practicality simplicity Files methods more Friends websites channels inquiries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft NVidia Shulou Technology OPPO Reno Shulou Tech Info