資源簡介
親測通過,這是一個單機人機、人人版本的五子棋源碼程序。我的文章參考:https://blog.csdn.net/u013457167/article/details/83859312,

代碼片段和文件信息
#include?
#include?
#include
#include?“GameModel.h“
GameModel::GameModel()
{
}
void?GameModel::startGame(GameType?type)
{
????gameType?=?type;
????//?初始棋盤
????gameMapVec.clear();
????for?(int?i?=?0;?i?????{
????????std::vector?lineBoard;
????????for?(int?j?=?0;?j?????????????lineBoard.push_back(0);
????????gameMapVec.push_back(lineBoard);
????}
????//?如果是AI模式,需要初始化評分數組
????if?(gameType?==?BOT)
????{
????????scoreMapVec.clear();
????????for?(int?i?=?0;?i?????????{
????????????std::vector?lineScores;
????????????for?(int?j?=?0;?j?????????????????lineScores.push_back(0);
????????????scoreMapVec.push_back(lineScores);
????????}
????}
????//?己方下為true對方下位false
????playerFlag?=?true;
}
void?GameModel::updateGameMap(int?row?int?col)
{
????if?(playerFlag)
????????gameMapVec[row][col]?=?WHITECHESS;
????else
????????gameMapVec[row][col]?=?BLACKCHESS;
????//?換手
????playerFlag?=?!playerFlag;
}
void?GameModel::actionByPerson(int?row?int?col)
{
????updateGameMap(row?col);
}
void?GameModel::actionByAI(int?&clickRow?int?&clickCol)
{
????//?計算評分
????calculateScore();
????//?從評分中找出最大分數的位置
????int?maxScore?=?0;
????std::vector>?maxPoints;
????for?(int?row?=?1;?row?????????for?(int?col?=?1;?col?????????{
????????????//?前提是這個坐標是空的
????????????if?(gameMapVec[row][col]?==?0)
????????????{
????????????????if?(scoreMapVec[row][col]?>?maxScore)??????????//?找最大的數和坐標
????????????????{
????????????????????maxPoints.clear();
????????????????????maxScore?=?scoreMapVec[row][col];
????????????????????maxPoints.push_back(std::make_pair(row?col));
????????????????}
????????????????else?if?(scoreMapVec[row][col]?==?maxScore)?????//?如果有多個最大的數,都存起來
????????????????????maxPoints.push_back(std::make_pair(row?col));
????????????}
????????}
????//?隨機落子,如果有多個點的話
????srand((unsigned)time(0));
????int?index?=?rand()?%?maxPoints.size();
????std::pair?pointPair?=?maxPoints.at(index);
????clickRow?=?pointPair.first;?//?記錄落子點
????clickCol?=?pointPair.second;
????updateGameMap(clickRow?clickCol);
}
//?最關鍵的計算評分函數
void?GameModel::calculateScore()
{
????//?統計玩家或者電腦連成的子
????int?personNum?=?0;?//?玩家連成子的個數
????int?botNum?=?0;?//?AI連成子的個數
????int?emptyNum?=?0;?//?各方向空白位的個數
????//?清空評分數組
????scoreMapVec.clear();
????for?(int?i?=?0;?i?????{
????????std::vector?lineScores;
????????for?(int?j?=?0;?j?????????????lineScores.push_back(0);
????????scoreMapVec.push_back(lineScores);
????}
????//?計分(此處是完全遍歷,其實可以用bfs或者dfs加減枝降低復雜度,通過調整權重值,調整AI智能程度以及攻守風格)
????for?(int?row?=?0;?row?????????for?(int?col?=?0;?col?????????{
????????????//?空白點就算
????????????if?(row?>?0?&&?col?>?0?&&
?????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-11-07?16:03??.git\
?????目錄???????????0??2016-12-24?09:44??.git\branches\
?????文件???????????7??2016-12-25?17:38??.git\COMMIT_EDITMSG
?????文件?????????331??2016-12-25?17:38??.git\config
?????文件??????????73??2016-12-24?09:44??.git\desc
?????文件??????????99??2016-12-25?17:38??.git\FETCH_HEAD
?????文件??????????23??2016-12-24?09:44??.git\HEAD
?????目錄???????????0??2018-11-07?16:03??.git\hooks\
?????文件?????????452??2016-12-24?09:44??.git\hooks\applypatch-msg.sample
?????文件?????????896??2016-12-24?09:44??.git\hooks\commit-msg.sample
?????文件?????????189??2016-12-24?09:44??.git\hooks\post-update.sample
?????文件?????????398??2016-12-24?09:44??.git\hooks\pre-applypatch.sample
?????文件????????1642??2016-12-24?09:44??.git\hooks\pre-commit.sample
?????文件????????1352??2016-12-24?09:44??.git\hooks\pre-push.sample
?????文件????????4951??2016-12-24?09:44??.git\hooks\pre-reba
?????文件????????1239??2016-12-24?09:44??.git\hooks\prepare-commit-msg.sample
?????文件????????3611??2016-12-24?09:44??.git\hooks\update.sample
?????文件????????1088??2016-12-25?17:38??.git\index
?????目錄???????????0??2018-11-07?16:03??.git\info\
?????文件?????????240??2016-12-24?09:44??.git\info\exclude
?????目錄???????????0??2018-11-07?16:03??.git\logs\
?????文件????????1047??2016-12-25?17:38??.git\logs\HEAD
?????目錄???????????0??2018-11-07?16:03??.git\logs\refs\
?????目錄???????????0??2018-11-07?16:03??.git\logs\refs\heads\
?????文件????????1047??2016-12-25?17:38??.git\logs\refs\heads\master
?????目錄???????????0??2018-11-07?16:03??.git\logs\refs\remotes\
?????目錄???????????0??2018-11-07?16:03??.git\logs\refs\remotes\origin\
?????文件?????????185??2016-12-24?09:44??.git\logs\refs\remotes\origin\HEAD
?????文件?????????876??2016-12-25?17:38??.git\logs\refs\remotes\origin\master
?????目錄???????????0??2018-11-07?16:03??.git\ob
?????目錄???????????0??2018-11-07?16:03??.git\ob
............此處省略131個文件信息
- 上一篇:基于Zemax的手機攝像鏡頭光學設計.doc
- 下一篇:FDTD算法一維TE波傳播
評論
共有 條評論