zoukankan      html  css  js  c++  java
  • 象棋人工智能的实现

    为了实现人机对战功能,必须实现象棋的人工智能,将象棋的每个棋子都赋予一定的权重,每走一步都计算分值,选择得分最高的一步,这是象棋人工智能的基本思想。

        #ifndef AI_H
        #define AI_H
        #include "cocos2d.h"
        USING_NS_CC;
        class SceneGame;
    
        class Step : public CCObject
        {
        public:
            int _moveid;
            int _killid;
            int _xFrom;
            int _yFrom;
            int _xTo;
            int _yTo;
            static Step* create(int moveid, int killid, int xFrom, int yFrom, int xTo, int yTo)
            {
                Step* step = new Step;
                step->_killid = killid;
                step->_moveid = moveid;
                step->_xFrom = xFrom;
                step->_xTo = xTo;
                step->_yFrom = yFrom;
                step->_yTo = yTo;
                step->autorelease();
                return step;
            }
        };
        class AI
        {
        public:
            AI(SceneGame *game);
    
            SceneGame *_game;
            Step *GenOneMove(int level=2);
            int getScore();
    
            static int _score[7];
            CCArray *getAllPossibleMove();
            void getAllPossibleMove(int idx,CCArray *arr);
    
            int getMinValue(int level,int maxScore);
            int getMaxValue(int level,int minScore);
            Step *_step;
        };
    
        #endif // AI_H
    

    得分表

    int AI::_score[7]=
    {
        1000,
        10,
        10,
        100,
        50,
        50,
        20
    };

    创建一步

    Step *AI::GenOneMove(int level)
    {
    
        int maxScore=-10000;
        Step *ret;
        //find all possible access an calcute the hights score
        CCArray *possibleMOve=getAllPossibleMove();
        CCObject *obj;
        CCARRAY_FOREACH(possibleMOve,obj)
        {
            Step *step=(Step*)obj;
            _game->fakeMove(step);
            int score=getMinValue(level-1,maxScore);
            //int score=getScore();
            _game->unfakeMove(step);
            if(score>maxScore)
            {
                maxScore=score;
                ret=step;
            }
        }
    
        return ret;
    
    }

    最大值最小值算法

    int AI::getMinValue(int level,int maxScore)
    {
        if(level ==0)
        {
            return getScore();
        }
        int minScore=10000;
        CCArray *possibleMOve=getAllPossibleMove();
        CCObject *obj;
        CCARRAY_FOREACH(possibleMOve,obj)
        {
            Step *step=(Step*)obj;
            _game->fakeMove(step);
            int score=getMaxValue(level-1,minScore);
             _game->unfakeMove(step);
            if(score<=maxScore)
            {
                minScore=score;
                return minScore;
            }
    
    
            if(score<minScore)
            {
                minScore=score;
    
            }
    
        }
    
        return minScore;
    }
    int AI::getMaxValue(int level,int minScore)
    {
        if(level ==0)
        {
            return getScore();
        }
        int maxScore=-10000;
        CCArray *possibleMOve=getAllPossibleMove();
        CCObject *obj;
        CCARRAY_FOREACH(possibleMOve,obj)
        {
            Step *step=(Step*)obj;
            _game->fakeMove(step);
            int score=getMinValue(level-1,maxScore);
             _game->unfakeMove(step);
            if(score>=minScore)
            {
                maxScore=score;
                break;
            }
    
            if(score>maxScore)
            {
                maxScore=score;
    
            }
    
        }
    
        return maxScore;
    }
    

    值得注意的是,象棋预先考虑的步骤越多,象棋越智能,但是当象棋考虑到第4步的时候,ubuntu就崩溃了,可以采用智能减枝算法,有效减少计算量。注意,当使用智能减枝时,一定要将假动作回移,不然会引起递归混乱。

  • 相关阅读:
    nowcoderD Xieldy And His Password
    Codeforces681D Gifts by the List
    nowcoder80D applese的生日
    Codeforces961E Tufurama
    Codeforces957 Mahmoud and Ehab and yet another xor task
    nowcoder82E 无向图中的最短距离
    nowcoder82B 区间的连续段
    Codeforces903E Swapping Characters
    Codeforces614C Peter and Snow Blower
    Codeforces614D Skills
  • 原文地址:https://www.cnblogs.com/jjx2013/p/6223762.html
Copyright © 2011-2022 走看看