C++ QT中国象棋项目讲解(四) 简单的人机对战
生活随笔
收集整理的這篇文章主要介紹了
C++ QT中国象棋项目讲解(四) 简单的人机对战
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
人機對戰實戰用SingleGame.h
#ifndef SINGLEGAME_H #define SINGLEGAME_H#include "Board.h" #include "Step.h"class SingleGame : public Board { public:SingleGame();void click(int id, int row,int col); // 重寫點擊函數void getAllPossibleMove(QVector<Step *> &steps); // 獲取所有可能的結果保存Step* getBestMove(); // 根據規則獲取最優的移動void fakeMove(Step* step);void unfakeMove(Step* step);int calcScore();};#endif // SINGLEGAME_HSingleGame類繼承自Board類
click為重寫虛函數
此時主函數變為
main.cpp
#include <QApplication> #include "SingleGame.h"int main(int argc, char *argv[]) {QApplication a(argc, argv);SingleGame w;w.show();return a.exec(); }重寫click函數如下
void SingleGame::click(int id,int row,int col) {if(!this->_bRedTurn) // 如果不是紅方走,返回return;Board::click(id,row,col); // 紅方走棋if(!this->_bRedTurn) // 此時輪到黑方走棋{Step* step=getBestMove(); // 尋找最優的走法moveStone(step->_moveid,step->_killid,step->_rowTo,step->_colTo);} }這里獲取走法的算法如下:
首先枚舉出所有可以走的方案,存下來,遍歷所有可以走的方案,然后模擬一步走棋,計算當前局面的分數,注意回溯操作,選擇出局面分數最高的走法,走棋
Step* SingleGame::getBestMove() {QVector<Step *> steps;//看看有哪些步驟可以走getAllPossibleMove(steps);int maxScore=-100000;Step* ret;for(QVector<Step*>::iterator it=steps.begin();it!=steps.end();++it){Step* step=*it;//試著走一下fakeMove(step);//評估局面分int score=calcScore();//再走回來unfakeMove(step);//取最高的分數if(score>maxScore){maxScore=score;ret=step;}}return ret; }枚舉所有可能的走法,保存下來
void SingleGame::getAllPossibleMove(QVector<Step *>& steps) {//遍歷所有黑方棋子for(int i=16;i<32;i++){//如果棋子已死則直接跳過if(_s[i]._dead) continue;//遍歷所有行坐標for(int row=0;row<=9;row++){//遍歷所有列坐標for(int col=0;col<=8;col++){//獲取想要殺死的棋子的idint killid=this->getStoneId(row,col);//若想要殺死的棋子與行走的棋子顏色相同則跳過if(sameColor(killid,i)) continue;//判斷某一棋子能不能行走if(canMove(i,killid,row,col)){//將可以行走的“步”存放到steps中saveStep(i,killid,row,col,steps);}}}} }這里構造一Step結構體/類來保存走棋的過程
#ifndef STEP_H #define STEP_H#include <QObject>class Step : public QObject {Q_OBJECT public:explicit Step(QObject *parent = 0);~Step();//行走棋子的idint _moveid;//殺掉棋子的idint _killid;//起始行坐標int _rowFrom;//起始列坐標int _colFrom;//目標位置行坐標int _rowTo;//目標位置列坐標int _colTo;signals:public slots: };#endif // STEP_H保存當前一步走棋的函數
void Board::saveStep(int id,int killid,int row,int col,QVector<Step *>& steps) {Step *pStep = new Step();pStep->_moveid = id;pStep->_killid = killid;pStep->_rowFrom = _s[id]._row;pStep->_colFrom = _s[id]._col;pStep->_rowTo = row;pStep->_colTo = col;steps.push_back(pStep);}?這里fakeMove函數和unfakeMove函數如下
void SingleGame::fakeMove(Step* step) {killStone(step->_killid);moveStone(step->_moveid,step->_killid,step->_rowTo,step->_colTo); }void SingleGame::unfakeMove(Step* step) {reliveStone(step->_killid);moveStone(step->_moveid,step->_killid,step->_rowFrom,step->_colFrom); }這里killStone和reliveStone只是假想移動
void Board::killStone(int id) {if(id == -1)return;_s[id]._dead = true; }void Board::reliveStone(int id) {if(id == -1)return;_s[id]._dead = false; }評估局面分函數,紅棋和黑棋單獨計算,然后兩者相減
int SingleGame::calcScore() {//枚舉的 車=0 馬=1 炮=2 兵=3 將=4 士=5 相=6static int chessScore[]={100,50,50,20,1500,10,10};int redTotalScore=0;int blackTotalScore=0;//計算紅棋總分for(int i=0;i<16;i++){//如果棋子已死則跳過累加if(_s[i]._dead)continue;redTotalScore+=chessScore[_s[i]._type];}//計算黑棋總分for(int i=16;i<32;i++){//如果棋子已死則跳過累加if(_s[i]._dead)continue;blackTotalScore+=chessScore[_s[i]._type];}//返回黑棋總分-紅棋總分return blackTotalScore-redTotalScore; }?
總結
以上是生活随笔為你收集整理的C++ QT中国象棋项目讲解(四) 简单的人机对战的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zircon ddk快速入门
- 下一篇: UE4/UE5 虚幻引擎,Light光照