1、/*block.h*/#ifndef BLOCK_H_#define BLOCK_H_#includeclass QWidget;class Block:public QLabelQ_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);protected:void mous
2、ePressEvent(QMouseEvent* event);private:bool mine_flag_;bool mark_flag_;bool turn_over_flag_;int number_;#endif#include“block.h“#include#include#include#includeBlock:Block(bool mine_flag,QWidget* parent):QLabel(parent)mine_flag_=mine_flag;mark_flag_=false;turn_over_flag_=false;number_=-1;setPixmap(Q
3、Pixmap(“:/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“);elsesetPixmap(QPixmap(“:/images/mine_“+QString(“%1“).arg(number_)+“.png“);update();bool Block:is_mine()con
4、streturn mine_flag_;bool Block:is_turn_over()constreturn turn_over_flag_;/*鼠标事件的实现*/void Block:mousePressEvent(QMouseEvent* event)if(event-button()=Qt:LeftButton)if(!turn_over_flag_if(mine_flag_=true)setPixmap(QPixmap(“:/images/mine.png“);update();emit turn_over(true);elsesetPixmap(QPixmap(“:/images
5、/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“);elsemark_flag_=false;setPixmap(QPixmap(“:/images/normal.png“);update();QLabel:mousePressEvent(even
6、t);#ifndef BLOCK_AREA_H_#define BLOCK_AREA_H_#include“block.h“#includeclass QEvent;class QGridLayout;class QObject;class BlockArea:public QWidgetQ_OBJECTpublic:BlockArea(int row,int column,int mine_number,QWidget* parent=0);void set_block_area(int row,int column,int mine_number,int init_flag=false);
7、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_to_turn_over_more_blocks(int x,int y);private:QGridLayout* mainLayout;int row_
8、;int column_;int total_block_number_;int total_mine_number_;int turn_over_block_number_;bool game_over_flag_;#endif/*block_area.h*/#include“block_area.h“#include#include#include#include#include#include#include#include#include/*雷的随机布置*/ptrdiff_t random(ptrdiff_t i) return qrand()%i;ptrdiff_t (*p_rand
9、om)(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_number,int init_flag)if(!init_flag)for(int i=0;i(mainLayout-itemAtPosition(i,j)-widget();
10、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;bool mine_flagtotal_block_number_;for(int i=0;iaddWidget(new Block(mine_flagi*column_+j),i,j);for(int i=0;i(mainLayout-itemAtPosition(i,j)-widget(
11、);current_block-set_number(calculate_mines(i,j);connect(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
12、QWidget:eventFilter(watched,event);void BlockArea: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);elsefor(int i=0;i(mainLayout-it
13、emAtPosition(i,j)-widget()try_to_turn_over_more_blocks(i,j);int BlockArea:calculate_mines(int x,int y)constint number=0;for(int i=0;i=0) return number;void BlockArea:try_to_turn_over_more_blocks(int x,int y)QQueue queue;QPair pair;queue.enqueue(qMakePair(x,y);while(!queue.isEmpty()pair=queue.head();
14、queue.dequeue();if(calculate_mines(pair.first,pair.second)=0)for(int i=0;i=0) +turn_over_block_number_;queue.enqueue(qMakePair(pair.first-1+i,pair.second-1+j);if(turn_over_block_number_=total_block_number_-total_mine_number_)game_over_flag_=true;emit game_over(true);#ifndef MAIN_WINDOW_H_#define MAI
15、N_WINDOW_H_#include“block_area.h“#include#include#includeclass QAction;class QActionGroup;class QCloseEvent;class QMenu;class QToolBar;class QWidget;class MainWindow:public QMainWindowQ_OBJECTpublic:MainWindow(QWidget* parent=0);protected:void closeEvent(QCloseEvent* event);private slots:void slot_n
16、ew_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_about_game();void slot_game_over(bool is_win);void slot_timer();private:void read_settings();void write_settings();void create_actions();void c
17、reate_menus();void create_game_toolBar();void create_statusBar();private:BlockArea* area_;int row_;int column_;int mine_number_;int easy_record_time_;int middle_record_time_;int hard_record_time_;QString easy_record_name_;QString middle_record_name_;QString hard_record_name_;int current_standard_;QM
18、enu* game_menu;QMenu* setting_menu;QMenu* help_menu;QToolBar* game_toolBar;QAction* new_game_action;QAction* rank_action;QAction* exit_action;QAction* show_game_toolBar_action;QAction* show_statusBar_action;QAction* easy_standard_action;QAction* middle_standard_action;QAction* hard_standard_action;Q
19、Action* custom_standard_action;QActionGroup* standard_actionGroup;QAction* about_game_action;QAction* about_qt_action;QLabel* time_label;QTime time;QTimer timer;#endif#include“main_window.h“#include#include#include#include#include#include#include#include#include#include#include#include#include#inclu
20、de#include#include#include#include#include#include/*关于游戏介绍的信息*/const QString g_software_name=“Mine Sweeper“;const QString g_software_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
21、* parent):QMainWindow(parent)area_=new BlockArea(9,9,10);/一般设置connect(area_,SIGNAL(game_over(bool),this,SLOT(slot_game_over(bool);setCentralWidget(area_);create_actions();create_menus();create_game_toolBar();create_statusBar();QCoreApplication:setOrganizationName(g_software_author);QCoreApplication:
22、setApplicationName(g_software_name);read_settings();layout()-setSizeConstraint(QLayout:SetFixedSize);setWindowTitle(g_software_name);setWindowIcon(QIcon(“:/game.png“);/*等待最长时间,到时间就会提示游戏失败*/timer.start(1000);connect(slot_new_game();void MainWindow:closeEvent(QCloseEvent*)write_settings();void MainWin
23、dow:slot_new_game()area_-set_block_area(row_,column_,mine_number_);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-addWi
24、dget(new QLabel(tr(“Time“),0,1);up_layout-addWidget(new QLabel(tr(“Name“),0,2);up_layout-addWidget(new QLabel(tr(“Easy“),1,0);up_layout-addWidget(new QLabel(QString(“%1“).arg(easy_record_time_),1,1);up_layout-addWidget(new QLabel(easy_record_name_),1,2);up_layout-addWidget(new QLabel(tr(“Middle“),2,
25、0);up_layout-addWidget(new QLabel(QString(“%1“).arg(middle_record_time_),2,1);up_layout-addWidget(new QLabel(middle_record_name_),2,2);up_layout-addWidget(new QLabel(tr(“Hard“),3,0);up_layout-addWidget(new QLabel(QString(“%1“).arg(hard_record_time_),3,1);up_layout-addWidget(new QLabel(hard_record_na
26、me_),3,2);QPushButton* recount_button=new QPushButton(tr(“recount“);QPushButton* close_button=new QPushButton(tr(“close“);close_button-setDefault(true);connect(recount_button,SIGNAL(clicked(),connect(close_button,SIGNAL(clicked(),QHBoxLayout* bottom_layout=new QHBoxLayout;bottom_layout-addStretch();
27、bottom_layout-addWidget(recount_button);bottom_layout-addWidget(close_button);QVBoxLayout* main_layout=new QVBoxLayout(main_layout-addLayout(up_layout);main_layout-addLayout(bottom_layout);if(dialog.exec()=QDialog:Accepted)easy_record_time_=middle_record_time_=hard_record_time_=g_no_record_time;easy
28、_record_name_=middle_record_name_=hard_record_name_=g_no_record_name;void MainWindow:slot_show_game_toolBar(bool show)if(show)game_toolBar-show();elsegame_toolBar-hide();void MainWindow:slot_show_statusBar(bool show)if(show)statusBar()-show();elsestatusBar()-hide();/*游戏的设置容易、中等、困难及自定义*/void MainWind
29、ow:slot_standard(QAction* standard_action)if(standard_action=easy_standard_action)current_standard_=0;row_=9;column_=9;mine_number_=10;else if(standard_action=middle_standard_action)current_standard_=1;row_=16;column_=16;mine_number_=40;else if(standard_action=hard_standard_action)current_standard_=
30、2;row_=16;column_=30;mine_number_=99;else if(standard_action=custom_standard_action)QDialog dialog;dialog.setWindowTitle(tr(“set standard“);QSpinBox* row_spinBox=new QSpinBox;row_spinBox-setRange(5,50);row_spinBox-setValue(row_);QSpinBox* column_spinBox=new QSpinBox;column_spinBox-setRange(5,50);col
31、umn_spinBox-setValue(column_);QSpinBox* mine_spinBox=new QSpinBox;mine_spinBox-setValue(mine_number_);QHBoxLayout* up_layout=new QHBoxLayout;up_layout-addWidget(row_spinBox);up_layout-addWidget(column_spinBox);up_layout-addWidget(mine_spinBox);QDialogButtonBox* dialog_buttonBox=new QDialogButtonBox;
32、dialog_buttonBox-addButton(QDialogButtonBox:Ok);dialog_buttonBox-addButton(QDialogButtonBox:Cancel);connect(dialog_buttonBox,SIGNAL(accepted(),connect(dialog_buttonBox,SIGNAL(rejected(),QHBoxLayout* bottom_layout=new QHBoxLayout;bottom_layout-addStretch();bottom_layout-addWidget(dialog_buttonBox);QV
33、BoxLayout* main_layout=new QVBoxLayout(main_layout-addLayout(up_layout);main_layout-addLayout(bottom_layout);if(dialog.exec()=QDialog:Accepted)if(row_spinBox-value()*column_spinBox-value()mine_spinBox-value()current_standard_=3;row_=row_spinBox-value();column_=column_spinBox-value();mine_number_=min
34、e_spinBox-value();slot_new_game();/*实现帮助菜单中的关于游戏,及功能*/void MainWindow:slot_about_game()QString introduction(“+tr(“About Mine Sweepr“)+“+“+tr(“This game is played by revealing squares of the grid, typically by clicking them with a mouse. If a square containing a mine is revealed, the player loses the
35、 game. Otherwise, a digit is revealed in the square, indicating the number of adjacent squares (out of the possible eight) that contain mines.if this number is zero then the square appears blank, and the surrounding squares are automatically also revealed. By using logic, the player can in many inst
36、ances use this information to deduce that certain other squares are mine-free, in which case they may be safely revealed, or mine-filled, in which they can be marked as such (which is effected by right-clicking the square and indicated by a flag graphic).“)+“+“+tr(“This program is free software; you
37、 can redistribute it and/or modify.it under the terms of the GNU General Public License as published by the Software Foundation; either version 3 of the License, or (at your option) any later version.“)+“+“+tr(“Please see “)+“http:/www.gnu.org/licenses/gpl-3.0.html“+tr(“ for an overview of GPLv3 lic
38、ensing“)+“+“+tr(“Version:“)+g_software_version+“+“+tr(“Author: “)+g_software_author+“);QMessageBox messageBox(QMessageBox:Information,tr(“About Mine Sweeper“),introduction,QMessageBox:Ok);messageBox.exec();/*游戏的判断,及所给出的提示做出判断*/void MainWindow:slot_game_over(bool is_win)timer.stop();QString name;if(i
39、s_win)switch(current_standard_)case 0:if(time_label-text().toInt()text().toInt();easy_record_name_=name;elseQMessageBox:information(this,tr(“Result“),tr(“You win“);break;case 1:if(time_label-text().toInt()text().toInt();middle_record_name_=name;elseQMessageBox:information(this,tr(“Result“),tr(“You w
40、in“);break;case 2:if(time_label-text().toInt()text().toInt();hard_record_name_=name;elseQMessageBox:information(this,tr(“Result“),tr(“You win“);break;default:QMessageBox:information(this,tr(“Result“),tr(“You win“);elseQMessageBox:information(this,tr(“Result“),tr(“You lose“);/*定时器的设置*/void MainWindow
41、:slot_timer()time_label-setText(QString(“%1“).arg(time.elapsed()/1000);/*关于菜单栏的设置是否显示游戏工具栏和状态栏*/void MainWindow:read_settings()QSettings settings;settings.beginGroup(“MainWindow“);resize(settings.value(“size“).toSize();move(settings.value(“pos“).toPoint();bool show_game_toolBar=settings.value(“showG
42、ameToolBar“).toBool();show_game_toolBar_action-setChecked(show_game_toolBar);slot_show_game_toolBar(show_game_toolBar);bool show_statusBar=settings.value(“showStatusBar“).toBool();show_statusBar_action-setChecked(show_statusBar);slot_show_statusBar(show_statusBar); settings.endGroup();settings.begin
43、Group(“GameSetting“);current_standard_=settings.value(“current_standard“).toInt();switch(current_standard_)case 0:easy_standard_action-setChecked(true);break;case 1:middle_standard_action-setChecked(true);break;case 2:hard_standard_action-setChecked(true);break;case 3:custom_standard_action-setCheck
44、ed(true);break;default:;row_=settings.value(“row“).toInt()=0?9:settings.value(“row“).toInt();column_=settings.value(“column“).toInt()=0?9:settings.value(“column“).toInt();mine_number_=settings.value(“mine_number“).toInt()=0?10:settings.value(“mine_number“).toInt();settings.endGroup();settings.beginG
45、roup(“Rank“);easy_record_time_=settings.value(“easy_record_time“).toInt()=0?g_no_record_time:settings.value(“easy_record_time“).toInt();middle_record_time_=settings.value(“middle_record_time“).toInt()=0?g_no_record_time:settings.value(“middle_record_time“).toInt();hard_record_time_=settings.value(“h
46、ard_record_time“).toInt()=0?g_no_record_time:settings.value(“hard_record_time“).toInt();easy_record_name_=settings.value(“easy_record_name“).toString()=“?g_no_record_name:settings.value(“easy_record_name“).toString();middle_record_name_=settings.value(“middle_record_name“).toString()=“?g_no_record_n
47、ame:settings.value(“middle_record_name“).toString();hard_record_name_=settings.value(“hard_record_name“).toString()=“?g_no_record_name:settings.value(“hard_record_name“).toString();settings.endGroup();void MainWindow:write_settings()QSettings settings;settings.beginGroup(“MainWindow“);settings.setVa
48、lue(“size“,size();settings.setValue(“pos“, pos();settings.setValue(“showGameToolBar“,show_game_toolBar_action-isChecked();settings.setValue(“showStatusBar“,show_statusBar_action-isChecked();settings.endGroup();settings.beginGroup(“GameSetting“);settings.setValue(“current_standard“,current_standard_)
49、;settings.setValue(“row“,row_);settings.setValue(“column“,column_);settings.setValue(“mine_number“,mine_number_);settings.endGroup();settings.beginGroup(“Rank“);settings.setValue(“easy_record_time“,easy_record_time_);settings.setValue(“middle_record_time“,middle_record_time_);settings.setValue(“hard_record_time“,hard_record_time_