Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C++ to make minesweeper games

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report