C++ QT中国象棋项目讲解(六) 人机对战N步搜索

加入搜索层数,取局面的最小值,最大值构成递归 ,这里回溯的作用就体现出来

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=getMinScore(_level-1);
        unfakeMove(step);
        if(score>maxScore)
        {
            maxScore=score;
            ret=step;
        }
    }
    return ret;
}

求最小值

int SingleGame::getMinScore(int level)
{
    if(level==0) return calcScore();
    QVector<Step*> steps;
    getAllPossibleMove(steps);
    int minScore=100000;
    for(QVector<Step*>::iterator it=steps.begin();it!=steps.end();++it)
    {
        Step* step=*it;
        fakeMove(step);
        int score=getMaxScore(level-1);
        unfakeMove(step);
        if(score<minScore)
        {
            minScore=score;
        }
    }
    return minScore;
}

求最大值

int SingleGame::getMaxScore(int level)
{
    if(level==0) return calcScore();
    QVector<Step*> steps;
    getAllPossibleMove(steps);
    int maxScore=-100000;
    for(QVector<Step*>::iterator it=steps.begin();it!=steps.end();++it)
    {
        Step* step=*it;
        fakeMove(step);
        int score=getMinScore(level-1);
        unfakeMove(step);
        if(score>maxScore)
        {
            maxScore=score;
        }
    }
    return maxScore;
}

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中国象棋C++代码 #include "chess_zn.h" QTcpSocket * Chess_ZN::client = new QTcpSocket; QUndoStack * Chess_ZN::undoStack = new QUndoStack(); int Chess_ZN::second = 120; bool Chess_ZN::isTurn = false; Chess_ZN::Chess_ZN(QWidget *parent) : QWidget(parent) { init(); initElse(); } void Chess_ZN::initElse(){ treeitem = 1; timer=new QTimer; portmap=0; isConn = true; start = false; isTimer = false; isSearch = false; connect(timer,SIGNAL(timeout()),this,SLOT(stopWatch())); connect(wigettree[1],SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(getInfo(QTreeWidgetItem*))); connect(wigettree[0],SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(connectToHost_PK(QTreeWidgetItem*))); connect(client,SIGNAL(connected()),this,SLOT(connected())); //连接一旦断开 connect(client,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error(QAbstractSocket::SocketError ))); connect(client,SIGNAL(readyRead()),this,SLOT(readyRead())); peer = new PeerManager(this); peer->setServerPort(10001); items=wigettree[1]->currentItem(); item_pk=wigettree[0]->currentItem(); item_pk_info=wigettree[0]->currentItem(); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[8], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[9], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[10], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),action2[11], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[0], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[1], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[2], SLOT(setEnabled(bool))); connect(undoStack, SIGNAL(canUndoChanged(bool)),button[3], SLOT(setEnabled(bool))); timer->start(1000); createUndoView(); isChoose = true; tableeditor=new TableEditor("users"); } void Chess_ZN::createUndoView() { undoVie

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值