1、扫雷游戏代码/*block.h*/#ifndef BLOCK_H_#define BLOCK_H_#includeclass QWidget;class Block:public QLabel Q_OBJECTpublic: explicit Block(bool mine_flag,QWidget* parent=0); void set_number(int number); void turn_over(); bool is_mine()const; bool is_turn_over()const; signals: void turn_over(bool is_mine); prot
2、ected: void mousePressEvent(QMouseEvent* event); private: bool mine_flag_; bool mark_flag_; bool turn_over_flag_; int number_;#endif#includeblock.h#include#include#include#includeBlock:Block(bool mine_flag,QWidget* parent) :QLabel(parent) mine_flag_=mine_flag; mark_flag_=false; turn_over_flag_=false
3、; number_=-1; setPixmap(QPixmap(:/images/normal.png);void Block:set_number(int number) number_=number;void Block:turn_over() if(!turn_over_flag_) turn_over_flag_=true; if(mine_flag_) setPixmap(QPixmap(:/images/mine.png); else setPixmap(QPixmap(:/images/mine_+QString(%1).arg(number_)+.png); update();
4、 bool Block:is_mine()const return mine_flag_;bool Block:is_turn_over()const return turn_over_flag_;/*鼠标事件的实现*/void Block:mousePressEvent(QMouseEvent* event) if(event-button()=Qt:LeftButton) if(!turn_over_flag_&!mark_flag_) turn_over_flag_=true; if(mine_flag_=true) setPixmap(QPixmap(:/images/mine.png
5、); update(); emit turn_over(true); else setPixmap(QPixmap(:/images/mine_+QString(%1).arg(number_)+.png); update(); emit turn_over(false); else if(event-button()=Qt:RightButton) if(!turn_over_flag_) if(!mark_flag_) mark_flag_=true; setPixmap(QPixmap(:/images/flag.png); else mark_flag_=false; setPixma
6、p(QPixmap(:/images/normal.png); update(); QLabel:mousePressEvent(event);#ifndef BLOCK_AREA_H_#define BLOCK_AREA_H_#includeblock.h#includeclass QEvent;class QGridLayout;class QObject;class BlockArea:public QWidget Q_OBJECTpublic: BlockArea(int row,int column,int mine_number,QWidget* parent=0); void s
7、et_block_area(int row,int column,int mine_number,int init_flag=false);signals: void game_over(bool is_win);protected: bool eventFilter(QObject* watched, QEvent* event); private slots: void slot_turn_over(bool is_mine); private: int calculate_mines(int x,int y)const; /璁畻浠 ,y涓轰腑蹇幂殑涔濆 镙煎唴镄勯浄鏁 void try_
8、to_turn_over_more_blocks(int x,int y); private: QGridLayout* mainLayout; int row_; int column_; int total_block_number_; int total_mine_number_; int turn_over_block_number_; bool game_over_flag_;#endif/*block_area.h*/#includeblock_area.h#include#include#include#include#include#include#include#includ
9、e#include/*雷的随机布置*/ptrdiff_t random(ptrdiff_t i) return qrand()%i;ptrdiff_t (*p_random)(ptrdiff_t)=random;BlockArea:BlockArea(int row,int column,int mine_number,QWidget* parent) :QWidget(parent) set_block_area(row,column,mine_number,true);void BlockArea:set_block_area(int row,int column,int mine_num
10、ber,int init_flag) if(!init_flag) for(int i=0;irow_;i+) for(int j=0;jcolumn_;j+) delete static_cast(mainLayout-itemAtPosition(i,j)-widget(); delete mainLayout; row_=row; column_=column; total_block_number_=row_*column_; total_mine_number_=mine_number; turn_over_block_number_=0; game_over_flag_=false
11、; bool mine_flagtotal_block_number_; for(int i=0;itotal_mine_number_;i+) mine_flagi=true; for(int i=total_mine_number_;itotal_block_number_;i+) mine_flagi=false; QTime time; time=QTime:currentTime(); qsrand(time.msec()+time.second()*1000); std:random_shuffle(mine_flag,mine_flag+total_block_number_,p
12、_random); mainLayout=new QGridLayout(this); for(int i=0;irow_;i+) for(int j=0;jaddWidget(new Block(mine_flagi*column_+j),i,j); for(int i=0;irow_;i+) for(int j=0;jcolumn_;j+) Block* current_block=static_cast(mainLayout-itemAtPosition(i,j)-widget(); current_block-set_number(calculate_mines(i,j); conne
13、ct(current_block,SIGNAL(turn_over(bool),this,SLOT(slot_turn_over(bool); current_block-installEventFilter(this); bool BlockArea:eventFilter(QObject* watched, QEvent* event) if(game_over_flag_) if(event-type()=QEvent:MouseButtonPress) return true; return QWidget:eventFilter(watched,event);void BlockAr
14、ea:slot_turn_over(bool is_mine) if(is_mine) game_over_flag_=true; emit game_over(false); else +turn_over_block_number_; if(turn_over_block_number_=total_block_number_-total_mine_number_) game_over_flag_=true; emit game_over(true); else for(int i=0;irow_;i+) for(int j=0;jcolumn_;j+) if(sender()=stati
15、c_cast(mainLayout-itemAtPosition(i,j)-widget() try_to_turn_over_more_blocks(i,j); int BlockArea:calculate_mines(int x,int y)const int number=0; for(int i=0;i3;i+) for(int j=0;j=0) & (x-1+i=0) & (y-1+jcolumn_) ) if(static_cast(mainLayout-itemAtPosition(x-1+i,y-1+j)-widget()-is_mine() +number; return
16、number;void BlockArea:try_to_turn_over_more_blocks(int x,int y) QQueueQPair queue; QPair pair; queue.enqueue(qMakePair(x,y); while(!queue.isEmpty() pair=queue.head(); queue.dequeue(); if(calculate_mines(pair.first,pair.second)=0) for(int i=0;i3;i+) for(int j=0;j=0) & (pair.first-1+i=0) & (pair.secon
17、d-1+jcolumn_) ) if(!static_cast(mainLayout-itemAtPosition(pair.first-1+i,pair.second-1+j)-widget()-is_turn_over() static_cast(mainLayout-itemAtPosition(pair.first-1+i,pair.second-1+j)-widget()-turn_over(); +turn_over_block_number_; queue.enqueue(qMakePair(pair.first-1+i,pair.second-1+j); if(turn_ove
18、r_block_number_=total_block_number_-total_mine_number_) game_over_flag_=true; emit game_over(true); #ifndef MAIN_WINDOW_H_#define MAIN_WINDOW_H_#includeblock_area.h#include#include#includeclass QAction;class QActionGroup;class QCloseEvent;class QMenu;class QToolBar;class QWidget;class MainWindow:pub
19、lic QMainWindow Q_OBJECTpublic: MainWindow(QWidget* parent=0);protected: void closeEvent(QCloseEvent* event); private slots: void slot_new_game(); void slot_rank(); void slot_show_game_toolBar(bool show); void slot_show_statusBar(bool show); void slot_standard(QAction* standard_action); void slot_ab
20、out_game(); void slot_game_over(bool is_win); void slot_timer();private: void read_settings(); void write_settings(); void create_actions(); void create_menus(); void create_game_toolBar(); void create_statusBar();private: BlockArea* area_; int row_; int column_; int mine_number_; int easy_record_ti
21、me_; int middle_record_time_; int hard_record_time_; QString easy_record_name_; QString middle_record_name_; QString hard_record_name_; int current_standard_; QMenu* game_menu; QMenu* setting_menu; QMenu* help_menu; QToolBar* game_toolBar; QAction* new_game_action; QAction* rank_action; QAction* exi
22、t_action; QAction* show_game_toolBar_action; QAction* show_statusBar_action; QAction* easy_standard_action; QAction* middle_standard_action; QAction* hard_standard_action; QAction* custom_standard_action; QActionGroup* standard_actionGroup; QAction* about_game_action; QAction* about_qt_action; QLabe
23、l* time_label; QTime time; QTimer timer;#endif#includemain_window.h#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include/*关于游戏介绍的信息*/const QString g_software_name=Mine Sweeper;const QString g_s
24、oftware_version=1.2;const QString g_software_author=CHANGHUIZHEN;/*关于排行榜的信息*/const int g_no_record_time=9999;const QString g_no_record_name=;MainWindow:MainWindow(QWidget* parent) :QMainWindow(parent) area_=new BlockArea(9,9,10);/一般设置 connect(area_,SIGNAL(game_over(bool),this,SLOT(slot_game_over(boo
25、l); setCentralWidget(area_); create_actions(); create_menus(); create_game_toolBar(); create_statusBar(); QCoreApplication:setOrganizationName(g_software_author); QCoreApplication:setApplicationName(g_software_name); read_settings(); layout()-setSizeConstraint(QLayout:SetFixedSize); setWindowTitle(g
26、_software_name); setWindowIcon(QIcon(:/game.png); /*等待最长时间,到时间就会提示游戏失败*/ timer.start(1000); connect(&timer,SIGNAL(timeout(),this,SLOT(slot_timer(); slot_new_game();void MainWindow:closeEvent(QCloseEvent*) write_settings();void MainWindow:slot_new_game() area_-set_block_area(row_,column_,mine_number_
27、); time_label-setText(0); time.restart(); timer.start();void MainWindow:slot_rank() QDialog dialog; dialog.setWindowTitle(tr(rank); QGridLayout* up_layout=new QGridLayout; up_layout-addWidget(new QLabel(tr(Standard),0,0); up_layout-addWidget(new QLabel(tr(Time),0,1); up_layout-addWidget(new QLabel(tr(Name),0,2); up_layout-a
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1