資源簡介
QT 掃雷程序源碼,QT Create 5.11編寫,防系統掃雷程序,高,中,低級。

代碼片段和文件信息
#include?
#include?
#include?“game_model.h“
GameModel::GameModel()
????:?mRow(kRow)
??????mCol(kCol)
??????totalMineNumber(kMineCount)
??????timerSeconds(kTime)
??????gameState(PLAYING)
{
????//?在構造函數里面不做具體初始化,因為構造函數的調用時間不確定
}
GameModel::~GameModel()
{
}
void?GameModel::createGame(int?row?int?col?int?mineCount?GameLevel?level)
{
????//?先清空已經有的游戲地圖
????gameMap.clear();
????//?設置成員變量
????mRow?=?row;
????mCol?=?col;
????totalMineNumber?=?mineCount;
????curMineNumber?=?mineCount;
????gameState?=?PLAYING;
????gameLevel?=?level;
????timerSeconds?=?0;
????//?初始化雷方塊
????for(int?i?=?0;?i?????{
????????//添加每行的block
????????std::vector?lineBlocks;
????????for(int?j?=?0;?j?????????{
????????????MineBlock?mineBlock;
????????????mineBlock.curState?=?UN_DIG;?//?默認都是未挖掘
????????????mineBlock.valueFlag?=?0;?//?默認都是0
????????????lineBlocks.push_back(mineBlock);
????????}
????????gameMap.push_back(lineBlocks);
????}
????//?隨機布雷
????srand((unsigned?int)time(0));
????int?k?=?totalMineNumber;
????while(k?>?0)
????{
????????//?埋雷并防止重疊
????????int?pRow?=?rand()?%?mRow;
????????int?pCol?=?rand()?%?mCol;
????????if(gameMap[pRow][pCol].valueFlag?!=?-1)
????????{
?????????????gameMap[pRow][pCol].valueFlag?=?-1;
?????????????k--;?//?如果原來就有雷重新循環
????????}
????}
????//?計算雷周圍的方塊數字
????for(int?i?=?0;?i?????{
????????for(int?j?=?0;?j?????????{
????????????//?周圍八個方塊(排除自己,在地圖范圍內)的數字根據雷的數目疊加
????????????//?y為行偏移量,x為列偏移量
????????????//?前提條件是本方塊不是雷
????????????if(gameMap[i][j].valueFlag?!=?-1)
????????????{
????????????????for(int?y?=?-1;?y?<=?1;?y++)
????????????????{
????????????????????for(int?x?=?-1;?x?<=?1;?x++)
????????????????????{
????????????????????????if(i?+?y?>=?0
????????????????????????&&?i?+?y?????????????????????????&&?j?+?x?>=?0
????????????????????????&&?j?+?x?????????????????????????&&?gameMap[i?+?y][j?+?x].valueFlag?==?-1
????????????????????????&&?!(x?==?0?&&?y?==?0))
????????????????????????{
????????????????????????????//?方塊數字加1
????????????????????????????gameMap[i][j].valueFlag++;
????????????????????????}
????????????????????}
????????????????}
????????????}
????????}
????}
}
void?GameModel::restartGame()
{
????createGame(mRow?mCol?totalMineNumber?gameLevel);
}
void?GameModel::digMine(int?m?int?n)
{
????//?正常方塊且沒有被翻開過,標記為已挖
????if(gameMap[m][n].valueFlag?>?0
?????&&?gameMap[m][n].curState?==?UN_DIG)
????{
????????gameMap[m][n].curState?=?DIGGED;
????}
????//?遇到空白塊(數字0)就遞歸挖雷,如果踩雷就爆掉游戲結束
????if(gameMap[m][n].valueFlag?==?0
?????&&?gameMap[m][n].curState?==?UN_DIG)
????{
????????gameMap[m][n].curState?=?DIGGED;
????????for(int?y?=?-1;?y?<=?1;?y++)
????????{
????????????for(int?x?=?-1;?x?<=?1;?x++)
????????????{
????????????????if(m?+?y?>=?0
????????????????&&?m?+?y?????????????????&&?n?+?x?>=?0
????????????????&&?n?+?x?????????????????&&?!(x?==?0?&&?y?==?0))
????????????????{
????????????????????digMine(m?+?y?n?+?x);
???????????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2016-07-11?10:49??QtMineSweep-master\
?????文件?????????453??2016-07-11?10:49??QtMineSweep-master\QtMineSweep.pro
?????文件???????23882??2016-07-11?10:49??QtMineSweep-master\QtMineSweep.pro.user
?????文件?????????568??2016-07-11?10:49??QtMineSweep-master\README.md
?????文件????????5402??2016-07-11?10:49??QtMineSweep-master\game_model.cpp
?????文件????????1663??2016-07-11?10:49??QtMineSweep-master\game_model.h
?????文件?????????182??2016-07-11?10:49??QtMineSweep-master\main.cpp
?????文件????????8657??2016-07-11?10:49??QtMineSweep-master\main_game_window.cpp
?????文件?????????954??2016-07-11?10:49??QtMineSweep-master\main_game_window.h
?????文件????????1747??2016-07-11?10:49??QtMineSweep-master\maingamewindow.ui
?????目錄???????????0??2016-07-11?10:49??QtMineSweep-master\pic\
?????文件????????8997??2016-07-11?10:49??QtMineSweep-master\pic\1.PNG
?????文件???????10341??2016-07-11?10:49??QtMineSweep-master\pic\2.PNG
?????文件???????11912??2016-07-11?10:49??QtMineSweep-master\pic\3.PNG
?????文件???????11727??2016-07-11?10:49??QtMineSweep-master\pic\4.PNG
?????文件????????8342??2016-07-11?10:49??QtMineSweep-master\pic\5.PNG
?????文件???????24510??2016-07-11?10:49??QtMineSweep-master\pic\game.gif
?????目錄???????????0??2016-07-11?10:49??QtMineSweep-master\res\
?????文件???????15654??2016-07-11?10:49??QtMineSweep-master\res\blocks.bmp
?????文件????????6966??2016-07-11?10:49??QtMineSweep-master\res\faces.bmp
?????文件???????32054??2016-07-11?10:49??QtMineSweep-master\res\fr
?????文件???????16854??2016-07-11?10:49??QtMineSweep-master\res\timenumber.bmp
?????文件?????????203??2016-07-11?10:49??QtMineSweep-master\resource.qrc
- 上一篇:sln轉換為dsw
- 下一篇:canvas行走的小人
評論
共有 條評論