码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2dx《单机斗地主》源码解剖之七 对电脑玩家手中的牌进行分拆

时间:2015-02-16 18:30:32      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:

在电脑玩家跟牌和出牌之前首先对电脑玩家的牌进行拆分:

根据文档需求(见本博客“斗地主规则“)拆分牌按以下顺序,先分析炸弹---飞机---连对---连牌--三带,对子,单张。请看如下代码:

void GameScene::FenChaiNpcPai(Player* npc){
    /************************************************************************/
    /* 1.首先分析出来牌的类型(如:四张,三张,两张,一张)                                              */
    /************************************************************************/
    std::vector<PaiXing> vec;
    PaiXing xing;
    CCArray* arr = CCArray::create();//临时数组
    arr->addObjectsFromArray(npc->getArrPk());
    //提取双鬼
    Poker* pk = (Poker *)arr->objectAtIndex(0);
    Poker* pk1 = (Poker *)arr->objectAtIndex(1);
    if(pk->getHuaSe() == Gui && pk1->getHuaSe() == Gui){
        xing.type = BOMB_CARD;
        xing.vec.push_back(pk);
        xing.vec.push_back(pk1);
        arr->removeObject(pk);
        arr->removeObject(pk1);
        vec.push_back(xing);
    }
    //分析牌型
    for(int i=0; i<arr->count();)
    {
        pk = (Poker*)arr->objectAtIndex(i);
        xing.vec.clear();//清除数组
        //找出与pk相同的牌
        for (int j=i; j<arr->count(); ++j)
        {
            pk1 = (Poker*)arr->objectAtIndex(j);
            if(pk->getNum() == pk1->getNum())
            {
                ++i;
                xing.vec.push_back(pk1);
            }
            else
            {
                break;
            }
        }
        if(xing.vec.size() == 4)
            xing.type = BOMB_CARD;
        if(xing.vec.size() == 3)
            xing.type = THREE_CARD;
        if(xing.vec.size() == 2)
            xing.type = DOUBLE_CARD;
        if(xing.vec.size() == 1)
            xing.type = SINGLE_CARD;
        vec.push_back(xing);
    }
    /************************************************************************/
    /* 2.按优先级(先分析炸弹---飞机---连对---连牌--三带,对子,单张)提取牌型并保存用于出牌或跟牌                               */
    /************************************************************************/
    //提取炸弹
    for(std::vector<PaiXing>::iterator iter = vec.begin(); iter != vec.end(); )
    {
        if(iter->type == BOMB_CARD)
        {
            xing.type = BOMB_CARD;
            xing.vec.clear();
            xing.vec = iter->vec;
            npc->m_vecPX.push_back(xing);//把牌型保存到用户数组中
            iter = vec.erase(iter);
        }
        else
        {
            ++iter;
        }
    }
    //提取飞机
    TiQuFeiJi(npc,THREE_CARD,vec);
    //提取连对
    TiQuLianDui(npc,vec);
    //提取连牌
    TiQuLianPai(npc,vec);
    //剩余的是三带,对子,单张 全部加入npc牌型中
    for(std::vector<PaiXing>::iterator iter = vec.begin(); iter != vec.end();)
    {
        npc->m_vecPX.push_back(*iter);
        iter = vec.erase(iter);
    }
    //排序
    stable_sort(npc->m_vecPX.begin(),npc->m_vecPX.end(),isShorter1);
}
最后按每个牌型的值从小到大进行排序。

提取飞机代码:

void GameScene::TiQuFeiJi(Player* npc,CARD_TYPE type,std::vector<PaiXing> &vec){
    Poker * pk = NULL;
    PaiXing xing;
    for (std::vector<PaiXing>::iterator iter=vec.begin(); iter != vec.end();)
    {
        if(pk == NULL && iter+1 == vec.end())
            break;
        if(pk == NULL && iter->type == type && (iter+1)->type == type)
        {
            Poker* pk1 = iter->vec.front();
            Poker* pk2 = (iter+1)->vec.front();
            if(pk1->getNum()-1 == pk2->getNum())
            {
                pk = pk2;
                xing.type = AIRCRAFT_CARD;
                xing.vec.clear();
                xing.vec = iter->vec;
                iter = vec.erase(iter);
                xing.vec.insert(xing.vec.end(),iter->vec.begin(),iter->vec.end());
                iter = vec.erase(iter);
            }
        }
        if(pk != NULL)
        {
            if(iter == vec.end())
            {
                npc->m_vecPX.push_back(xing);
                break;
            }
            
            Poker* pk1 = iter->vec.front();
            if(iter->type == type && pk->getNum()-1 == pk1->getNum())
            {
                pk = pk1;
                xing.vec.insert(xing.vec.end(),iter->vec.begin(),iter->vec.end());
                iter = vec.erase(iter);
                if(iter == vec.end())
                {
                    npc->m_vecPX.push_back(xing);
                    break;
                }
            }
            else
            {
                npc->m_vecPX.push_back(xing);
                pk = NULL;
            }
        }
        else
        {
            ++iter;
        }
    }
}
提取连对 代码:

void GameScene::TiQuLianDui(Player* npc,std::vector<PaiXing> &vec){
    std::vector<PaiXing> vecTem;//临时数组
    std::vector<PaiXing> vecFan;//存放要重新返还vec里的牌
    Poker* pk = NULL;
    for(std::vector<PaiXing>::iterator iter = vec.begin(); iter != vec.end();)
    {
        //将相连的牌加入临时数组中
        Poker* pk1 = iter->vec.front();
        if((iter->type == THREE_CARD || iter->type == DOUBLE_CARD) && (pk == NULL || (pk->getNum()-1 == pk1->getNum() && pk->getNum() < Er)))
        {
            pk = pk1;
            vecTem.push_back(*iter);
            iter = vec.erase(iter);
        }
        else
        {
            if(pk == NULL)
                ++iter;
            pk = NULL;
            if(vecTem.size() >= 3)
            {
                PaiXing xing;
                xing.type = COMPANY_CARD;
                for (int i=0; i<vecTem.size(); ++i)
                {
                    if(vecTem[i].type == THREE_CARD)
                    {
                        //将多余的一张保存返回数组vecFan中
                        PaiXing xing1;
                        xing1.type = SINGLE_CARD;
                        xing1.vec.push_back(vecTem[i].vec.back());
                        vecTem[i].vec.pop_back();
                        vecFan.push_back(xing1);
                        //将剩余两张保存xing中
                        xing.vec.insert(xing.vec.end(),vecTem[i].vec.begin(),vecTem[i].vec.end());
                    }
                    if(vecTem[i].type == DOUBLE_CARD)
                    {
                        xing.vec.insert(xing.vec.end(),vecTem[i].vec.begin(),vecTem[i].vec.end());
                    }
                }
                vecTem.clear();
                npc->m_vecPX.push_back(xing);
            }
            else if(!vecTem.empty())
            {
                vecFan.insert(vecFan.end(),vecTem.begin(),vecTem.end());
                vecTem.clear();
            }
        }
    }
    if(!vecTem.empty())
    {
        if(vecTem.size() >= 3)
        {
            PaiXing xing;
            xing.type = COMPANY_CARD;
            for (int i=0; i<vecTem.size(); ++i)
            {
                if(vecTem[i].type == THREE_CARD)
                {
                    //将多余的一张保存返回数组vecFan中
                    PaiXing xing1;
                    xing1.type = SINGLE_CARD;
                    xing1.vec.push_back(vecTem[i].vec.back());
                    vecTem[i].vec.pop_back();
                    vecFan.push_back(xing1);
                    //将剩余两张保存xing中
                    xing.vec.insert(xing.vec.end(),vecTem[i].vec.begin(),vecTem[i].vec.end());
                }
                if(vecTem[i].type == DOUBLE_CARD)
                {
                    xing.vec.insert(xing.vec.end(),vecTem[i].vec.begin(),vecTem[i].vec.end());
                }
            }
            vecTem.clear();
            npc->m_vecPX.push_back(xing);
        }
        else if(!vecTem.empty())
        {
            vecFan.insert(vecFan.end(),vecTem.begin(),vecTem.end());
            vecTem.clear();
        }
    }
    //将vecFan返回到vec数组中并从大到小排序
    if(!vecFan.empty())
    {
        vec.insert(vec.end(),vecFan.begin(),vecFan.end());
        stable_sort(vec.begin(),vec.end(),isDaDaoXiao);
    }
}
提取连牌 代码:

void GameScene::TiQuLianPai(Player* npc,std::vector<PaiXing> &vec){
    std::vector<PaiXing> vecTem;//临时数组
    std::vector<PaiXing> vecFan;//存放要重新返还vec里的牌
    Poker* pk = NULL;
    for(std::vector<PaiXing>::iterator iter = vec.begin(); iter != vec.end();)
    {
        //将相连的牌加入临时数组中
        Poker* pk1 = iter->vec.front();
        if((iter->type == THREE_CARD || iter->type == DOUBLE_CARD || iter->type == SINGLE_CARD) && (pk == NULL || (pk->getNum()-1 == pk1->getNum() && pk->getNum() < Er)))
        {
            pk = pk1;
            vecTem.push_back(*iter);
            iter = vec.erase(iter);
        }
        else
        {
            if(pk == NULL)
                ++iter;
            pk = NULL;
            if(vecTem.size() >= 5)
            {
                PaiXing xing;
                xing.type = CONNECT_CARD;
                for (int i=0; i<vecTem.size(); ++i)
                {
                    if(vecTem[i].type == THREE_CARD)
                    {
                        //将多余的两张保存返回数组vecFan中
                        PaiXing xing1;
                        xing1.type = DOUBLE_CARD;
                        xing1.vec.push_back(vecTem[i].vec.back());
                        vecTem[i].vec.pop_back();
                        xing1.vec.push_back(vecTem[i].vec.back());
                        vecTem[i].vec.pop_back();
                        vecFan.push_back(xing1);
                        //将剩余一张保存xing中
                        xing.vec.insert(xing.vec.end(),vecTem[i].vec.begin(),vecTem[i].vec.end());
                    }
                    if(vecTem[i].type == DOUBLE_CARD)
                    {
                        //将多余的一张保存返回数组vecFan中
                        PaiXing xing1;
                        xing1.type = SINGLE_CARD;
                        xing1.vec.push_back(vecTem[i].vec.back());
                        vecTem[i].vec.pop_back();
                        vecFan.push_back(xing1);
                        //将剩余一张保存xing中
                        xing.vec.insert(xing.vec.end(),vecTem[i].vec.begin(),vecTem[i].vec.end());
                    }
                    if(vecTem[i].type == SINGLE_CARD)
                        xing.vec.push_back(vecTem[i].vec.front());
                }
                vecTem.clear();
                npc->m_vecPX.push_back(xing);
            }
            else if(!vecTem.empty())
            {
                vecFan.insert(vecFan.end(),vecTem.begin(),vecTem.end());
                vecTem.clear();
            }
        }
    }
    if(!vecTem.empty())
    {
        if(vecTem.size() >= 5)
        {
            PaiXing xing;
            xing.type = CONNECT_CARD;
            for (int i=0; i<vecTem.size(); ++i)
            {
                if(vecTem[i].type == THREE_CARD)
                {
                    //将多余的两张保存返回数组vecFan中
                    PaiXing xing1;
                    xing1.type = DOUBLE_CARD;
                    xing1.vec.push_back(vecTem[i].vec.back());
                    vecTem[i].vec.pop_back();
                    xing1.vec.push_back(vecTem[i].vec.back());
                    vecTem[i].vec.pop_back();
                    vecFan.push_back(xing1);
                    //将剩余一张保存xing中
                    xing.vec.insert(xing.vec.end(),vecTem[i].vec.begin(),vecTem[i].vec.end());
                }
                if(vecTem[i].type == DOUBLE_CARD)
                {
                    //将多余的一张保存返回数组vecFan中
                    PaiXing xing1;
                    xing1.type = SINGLE_CARD;
                    xing1.vec.push_back(vecTem[i].vec.back());
                    vecTem[i].vec.pop_back();
                    vecFan.push_back(xing1);
                    //将剩余一张保存xing中
                    xing.vec.insert(xing.vec.end(),vecTem[i].vec.begin(),vecTem[i].vec.end());
                }
                if(vecTem[i].type == SINGLE_CARD)
                    xing.vec.push_back(vecTem[i].vec.front());
            }
            vecTem.clear();
            npc->m_vecPX.push_back(xing);
        }
        else if(!vecTem.empty())
        {
            vecFan.insert(vecFan.end(),vecTem.begin(),vecTem.end());
            vecTem.clear();
        }
    }
    //将vecFan返回到vec数组中并从大到小排序
    if(!vecFan.empty())
    {
        vec.insert(vec.end(),vecFan.begin(),vecFan.end());
        stable_sort(vec.begin(),vec.end(),isShorter1);
    }
}



cocos2dx《单机斗地主》源码解剖之七 对电脑玩家手中的牌进行分拆

标签:

原文地址:http://blog.csdn.net/oiy37215/article/details/43852745

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!