版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、/*block.h*/#ifndef BLOCK_H_#define BLOCK_H_#include<QLabel>class 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);prote
2、cted:void mousePressEvent(QMouseEvent* event);private:bool mine_flag_;bool mark_flag_;bool turn_over_flag_;int number_;#endif#include"block.h"#include<QLabel>#include<QMouseEvent>#include<QPixmap>#include<QWidget>Block:Block(bool mine_flag,QWidget* parent):QLabel(pa
3、rent)mine_flag_=mine_flag;mark_flag_=false;turn_over_flag_=false;number_=-1;setPixmap(QPixmap(":/images/normal.png");1 / 22void 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&
4、quot;);elsesetPixmap(QPixmap(":/images/mine_"+QString("%1").arg(number_)+".png");update();bool Block:is_mine()constreturn mine_flag_;bool Block:is_turn_over()constreturn turn_over_flag_;/*鼠標事件的實現(xiàn)*/void Block:mousePressEvent(QMouseEvent* event)if(event->button()=Qt:Le
5、ftButton)if(!turn_over_flag_&&!mark_flag_)turn_over_flag_=true;if(mine_flag_=true)setPixmap(QPixmap(":/images/mine.png");update();emit turn_over(true);elsesetPixmap(QPixmap(":/images/mine_"+QString("%1").arg(number_)+".png");update();emit turn_over(fal
6、se);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(event);#ifndef BLOCK_AREA_H_#define BLOCK_AREA_H_#
7、include"block.h"#include<QWidget>class 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);signals:void game_ove
8、r(bool is_win);protected:booleventFilter(QObject* watched, QEvent* event);private slots:void slot_turn_over(bool is_mine);private:int calculate_mines(int x,int y)const;/璁畻浠,y涓轟腑蹇冪殑涔?jié)彐牸鍐呯殑闆锋晇oid try_to_turn_over_more_blocks(int x,int y);private:QGridLayout* mainLayout;int row_;int column_;int total_b
9、lock_number_;int total_mine_number_;int turn_over_block_number_;bool game_over_flag_;#endif/*block_area.h*/#include"block_area.h"#include<algorithm>#include<QEvent>#include<QGridLayout>#include<QLayout>#include<QMouseEvent>#include<QObject>#include<QQ
10、ueue>#include<QTime>#include<QWidget>/*雷的隨機布置*/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_b
11、lock_area(int row,int column,int mine_number,int init_flag)if(!init_flag)for(int i=0;i<row_;i+)for(int j=0;j<column_;j+)delete static_cast<Block*>(mainLayout->itemAtPosition(i,j)->widget();delete mainLayout;row_=row;column_=column;total_block_number_=row_*column_;total_mine_number_
12、=mine_number;turn_over_block_number_=0;game_over_flag_=false;bool mine_flagtotal_block_number_;for(int i=0;i<total_mine_number_;i+)mine_flagi=true;for(int i=total_mine_number_;i<total_block_number_;i+)mine_flagi=false;QTime time;time=QTime:currentTime();qsrand(time.msec()+time.second()*1000);s
13、td:random_shuffle(mine_flag,mine_flag+total_block_number_,p_random);mainLayout=new QGridLayout(this);for(int i=0;i<row_;i+)for(int j=0;j<column_;j+)mainLayout->addWidget(new Block(mine_flagi*column_+j),i,j);for(int i=0;i<row_;i+)for(int j=0;j<column_;j+)Block* current_block=static_cas
14、t<Block*>(mainLayout->itemAtPosition(i,j)->widget();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);boolBlockArea:eventFilter(QObject* watched, QEvent* event)if(game_o
15、ver_flag_)if(event->type()=QEvent:MouseButtonPress)return true;return 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_)
16、game_over_flag_=true;emit game_over(true);elsefor(int i=0;i<row_;i+)for(int j=0;j<column_;j+)if(sender()=static_cast<Block*>(mainLayout->itemAtPosition(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<3;i+)
17、for(int j=0;j<3;j+)if( (x-1+i>=0) && (x-1+i<row_) && (y-1+j>=0) && (y-1+j<column_) )if(static_cast<Block*>(mainLayout->itemAtPosition(x-1+i,y-1+j)->widget()->is_mine()+number;return number;void BlockArea:try_to_turn_over_more_blocks(int x,int y)Q
18、Queue<QPair<int,int> > queue;QPair<int,int> 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;i<3;i+)for(int j=0;j<3;j+)if( (pair.first-1+i>=0) && (pair.first-1+i<ro
19、w_) && (pair.second-1+j>=0) && (pair.second-1+j<column_) )if(!static_cast<Block*>(mainLayout->itemAtPosition(pair.first-1+i,pair.second-1+j)->widget()->is_turn_over()static_cast<Block*>(mainLayout->itemAtPosition(pair.first-1+i,pair.second-1+j)->widge
20、t()->turn_over();+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 MAIN_WINDOW_H_#include"block_area.h"#include<QMa
21、inWindow>#include<QTime>#include<QTimer>class 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_new
22、_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 cre
23、ate_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_;QMen
24、u* 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;QAc
25、tion* 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<QAction>#include<QActionGroup>#include<QApplication>#include<QDialog>
26、#include<QDialogButtonBox>#include<QHBoxLayout>#include<QIcon>#include<QInputDialog>#include<QLayout>#include<QMainWindow>#include<QMenu>#include<QMenuBar>#include<QMessageBox>#include<QPushButton>#include<QSettings>#include<QSpinB
27、ox>#include<QStatusBar>#include<QToolBar>#include<QVBoxLayout>#include<QWidget>/*關于游戲介紹的信息*/const QString g_software_name="Mine Sweeper"const QString g_software_version="1.2"const QString g_software_author="CHANGHUIZHEN"/*關于排行榜的信息*/const int g
28、_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(bool);setCentralWidget(area_);create_actions();create_menus();create_game_toolBar(
29、);create_statusBar();QCoreApplication:setOrganizationName(g_software_author);QCoreApplication:setApplicationName(g_software_name);read_settings();layout()->setSizeConstraint(QLayout:SetFixedSize);setWindowTitle(g_software_name);setWindowIcon(QIcon(":/game.png"); /*等待最長時間,到時間就會提示游戲失敗*/ti
30、mer.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_);time_label->setText("0");time.restart();timer.start();vo
31、id 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&quo
32、t;),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,0);up_layout->addWidg
33、et(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
34、(new QLabel(hard_record_name_),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(),&dialog,SLOT(accept();connect(close_button,SIGNAL(clic
35、ked(),&dialog,SLOT(reject();QHBoxLayout* bottom_layout=new QHBoxLayout;bottom_layout->addStretch();bottom_layout->addWidget(recount_button);bottom_layout->addWidget(close_button);QVBoxLayout* main_layout=new QVBoxLayout(&dialog);main_layout->addLayout(up_layout);main_layout->a
36、ddLayout(bottom_layout);if(dialog.exec()=QDialog:Accepted)easy_record_time_=middle_record_time_=hard_record_time_=g_no_record_time;easy_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_toolBa
37、r->hide();void MainWindow:slot_show_statusBar(bool show)if(show)statusBar()->show();elsestatusBar()->hide();/*游戲的設置容易、中等、困難及自定義*/void MainWindow:slot_standard(QAction* standard_action)if(standard_action=easy_standard_action)current_standard_=0;row_=9;column_=9;mine_number_=10;else if(standa
38、rd_action=middle_standard_action)current_standard_=1;row_=16;column_=16;mine_number_=40;else if(standard_action=hard_standard_action)current_standard_=2;row_=16;column_=30;mine_number_=99;else if(standard_action=custom_standard_action)QDialog dialog;dialog.setWindowTitle(tr("set standard")
39、;QSpinBox* row_spinBox=new QSpinBox;row_spinBox->setRange(5,50);row_spinBox->setValue(row_);QSpinBox* column_spinBox=new QSpinBox;column_spinBox->setRange(5,50);column_spinBox->setValue(column_);QSpinBox* mine_spinBox=new QSpinBox;mine_spinBox->setValue(mine_number_);QHBoxLayout* up_l
40、ayout=new QHBoxLayout;up_layout->addWidget(row_spinBox);up_layout->addWidget(column_spinBox);up_layout->addWidget(mine_spinBox);QDialogButtonBox* dialog_buttonBox=new QDialogButtonBox;dialog_buttonBox->addButton(QDialogButtonBox:Ok);dialog_buttonBox->addButton(QDialogButtonBox:Cancel)
41、;connect(dialog_buttonBox,SIGNAL(accepted(),&dialog,SLOT(accept();connect(dialog_buttonBox,SIGNAL(rejected(),&dialog,SLOT(reject();QHBoxLayout* bottom_layout=new QHBoxLayout;bottom_layout->addStretch();bottom_layout->addWidget(dialog_buttonBox);QVBoxLayout* main_layout=new QVBoxLayout(
42、&dialog);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_numbe
43、r_=mine_spinBox->value();slot_new_game();/*實現(xiàn)幫助菜單中的關于游戲,及功能*/void MainWindow:slot_about_game()QString introduction("<h2>"+tr("About Mine Sweepr")+"</h2>"+"<p>"+tr("This game is played by revealing squares of the grid, typically by cli
44、cking them with a mouse. If a square containing a mine is revealed, the player loses the 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 surround
45、ing squares are automatically also revealed. By using logic, the player can in many instances 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 t
46、he square and indicated by a flag graphic).")+"</p>"+"<p>"+tr("This program is free software; you 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,
47、 or (at your option) any later version.")+"</p>"+"<p>"+tr("Please see ")+"<a href=/licenses/gpl-3.0.html>/licenses/gpl-3.0.html</a>"+tr(" for an overview of GPLv3 licensing")+"</p
48、>"+"<br>"+tr("Version:")+g_software_version+"</br>"+"<br>"+tr("Author: ")+g_software_author+"</br>");QMessageBox messageBox(QMessageBox:Information,tr("About Mine Sweeper"),introduction,QMessageBox:
49、Ok);messageBox.exec();/*游戲的判斷,及所給出的提示做出判斷*/void MainWindow:slot_game_over(bool is_win)timer.stop();QString name;if(is_win)switch(current_standard_)case 0:if(time_label->text().toInt()<easy_record_time_)name=QInputDialog:getText(this,tr("Please enter your name"),tr("You create a
50、record. Please enter your name");if(!name.isEmpty()easy_record_time_=time_label->text().toInt();easy_record_name_=name;elseQMessageBox:information(this,tr("Result"),tr("You win");break;case 1:if(time_label->text().toInt()<middle_record_time_)name=QInputDialog:getTex
51、t(this,tr("Please enter your name"),tr("You create a record. Please enter your name");if(!name.isEmpty()middle_record_time_=time_label->text().toInt();middle_record_name_=name;elseQMessageBox:information(this,tr("Result"),tr("You win");break;case 2:if(time_
52、label->text().toInt()<hard_record_time_)name=QInputDialog:getText(this,tr("Please enter your name"),tr("You create a record. Please enter your name");if(!name.isEmpty()hard_record_time_=time_label->text().toInt();hard_record_name_=name;elseQMessageBox:information(this,tr
53、("Result"),tr("You win");break;default:QMessageBox:information(this,tr("Result"),tr("You win");elseQMessageBox:information(this,tr("Result"),tr("You lose");/*定時器的設置*/void MainWindow:slot_timer()time_label->setText(QString("%1")
54、.arg(time.elapsed()/1000);/*關于菜單欄的設置是否顯示游戲工具欄和狀態(tài)欄*/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("showGameToolBar
55、").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();s
56、ettings.beginGroup("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)
57、;break;case 3:custom_standard_action->setChecked(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_n
58、umber").toInt()=0?10:settings.value("mine_number").toInt();settings.endGroup();settings.beginGroup("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").toIn
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度環(huán)保技術研發(fā)采購追加合同3篇
- 2025年度新型農(nóng)業(yè)科技場咨詢服務合同范本4篇
- 2025年度住宅窗戶安全性能提升改造合同4篇
- 2024渣土運輸車加盟合同規(guī)范樣本3篇
- 2025年度智能汽車生產(chǎn)線廠房租賃合同3篇
- 2025年度市政綠化帶除草承包服務協(xié)議4篇
- 2025年度智能安防產(chǎn)品全國代理權授權合同范本4篇
- 2025年度水電工程合同履約保證金管理協(xié)議書范本4篇
- 2025年度智能凈水系統(tǒng)安裝與維護服務合同范本4篇
- 2025年度智能家居系統(tǒng)安裝與售后服務合同6篇
- 醫(yī)療糾紛預防和處理條例通用課件
- 廚邦醬油推廣方案
- 乳腺癌診療指南(2024年版)
- 高三數(shù)學寒假作業(yè)1
- 保險產(chǎn)品創(chuàng)新與市場定位培訓課件
- (完整文本版)體檢報告單模版
- 1例左舌鱗癌手術患者的圍手術期護理體會
- (完整)100道兩位數(shù)加減兩位數(shù)口算題(難)
- 鋼結(jié)構牛腿計算
- 2023-2024學年重慶市兩江新區(qū)四上數(shù)學期末質(zhì)量檢測試題含答案
- 體外診斷試劑工作校準品產(chǎn)品校準品互換性(基質(zhì)效應)Excel計算模板
評論
0/150
提交評論