COCOS2DX "single bucket landlord" source anatomy of eight computer players play cards and cards (end)

Source: Internet
Author: User

The previous article on the players hands of the cards to analyze the classification, the following should realize the computer player cards and follow the strategy of the card. First we look at the strategy of the card, the code is as follows:

void Gamescene::update (float delta) {    switch (m_istate)    {case        0:            sendpk ();            break;        Case 1:            Schedule (Schedule_selector (Gamescene::call), 1);            break;        Case 2:            scheduleonce (Schedule_selector (Gamescene::outcard), 0.5);            break;        Case 3:            Isshengli ();            break;        Default: Break            ;    }}

The function is explained first, and the function is a loop that is called once per frame. Let's take a look at the M_istate note in the header file:

int m_istate;//current status, 0: Licensing status 1: Call Landlord Status 2: Card status 3: Result status

Obviously, the card and the tag strategy is in the state 2, the function delay 0.5 seconds out of the card. Let's take a look at the strategy of the Outcard function:

void Gamescene::outcard (float delta) {switch (m_ioutcard%3) {case 0:m_chupaimenu->setvisible (            true);//Display the card menu, including "not out", "card" M_typetem = Paiduanpaixing ();//Get the player out of the card type, this function in the COCOS2DX "single bucket landlord" source anatomy of the six players (people) to explain the card.                if (!M_NPCONE->GETISOUTPK () &&!M_NPCTWO->GETISOUTPK ())//If two computer players do not play cards, set the "not out" button is not point, the reaction is then.            ((Ccmenuitemfont *) M_chupaimenu->getchildbytag (0))->setenabled (false);            Else ((Ccmenuitemfont *) M_chupaimenu->getchildbytag (0))->setenabled (true);                 Cards if (!M_NPCONE->GETISOUTPK () &&!M_NPCTWO->GETISOUTPK ()) {//Clear All Out cards CLEAROUTPK ();//The following sticker code if (M_typetem! = Error_card)//error_card is the wrong card type (CC                Menuitemfont *) M_chupaimenu->getchildbytag (1))->setenabled (true);     Else ((Ccmenuitemfont *) M_chupaimenu->getchildbytag (1))->setenabled (false);       } else//tag {if (M_arrplayerout->count ()! = 0) { poker* pk = (poker*) m_arrgenpk->objectatindex (0);//cards to follow poker* PK1 = (poker*) m_arrplayerout- >objectatindex (0);//Player out of the card if (M_typetem = = M_type && pk1->getnum () >pk->getnum () | | ( M_typetem==bomb_card && M_type!=bomb_card))//m_type is the card type (Ccmenuitemfont *) m_chupaimenu-                    >getchildbytag (1))->setenabled (true);                Else ((Ccmenuitemfont *) M_chupaimenu->getchildbytag (1))->setenabled (false);            } else ((Ccmenuitemfont *) M_chupaimenu->getchildbytag (1))->setenabled (false);        } break;            Case 1:m_chupaimenu->setvisible (FALSE);              if (!M_PLAYER->GETISOUTPK () &&!M_NPCONE->GETISOUTPK ()) {  Clear all cards out of CLEAROUTPK ();            Npcoutpoker (m_npctwo,m_arrgenpk,m_npctwoout);//Computer card strategy, the function is explained below.            } else Npcgenpoker (M_NPCTWO,M_ARRGENPK, m_npctwoout);//Computer tag strategy, function explained below.            Playeroutpaixu (M_ARRGENPK);//to the card to be followed, the function in the COCOS2DX "single bucket landlord" source anatomy of the six players (people) of the cards are explained. Playeroutpaixu (M_NPCTWOOUT->GETARRPK ());//The computer players out of the cards to sort M_npctwoout->updatepkweizhi ();//Update location M_            Npctwo->updatepkweizhi ();//Ibid. ++m_ioutcard;                if (Isoutpkfinish ())//Determine if the game is over, explain below.            M_istate = 3;        Break Case 2:if (!M_PLAYER->GETISOUTPK () &&!M_NPCTWO->GETISOUTPK ()) {//Clear All                Out of the card clearoutpk ();            Npcoutpoker (m_npcone,m_arrgenpk,m_npconeout);            } else Npcgenpoker (m_npcone,m_arrgenpk,m_npconeout);            Playeroutpaixu (M_ARRGENPK); Playeroutpaixu (M_NPCTWOOUT->GETARRPK ());            M_npconeout->updatepkweizhi ();            M_npcone->updatepkweizhi ();            ++m_ioutcard;            if (Isoutpkfinish ()) m_istate = 3;        Break    Default:break; }}

First introduce this state machine, we see the file on the definition of m_ioutcard variable: int m_ioutcard;//on who cards, 0 for players to play cards and follow the strategy of the card, 1 and 2 for the computer player cards and follow the card strategy. Their meaning has been added to the code in the comments.

In the above code you must have found some puzzling functions (CLEAROUTPK (), Npcoutpoker (M_npctwo,m_arrgenpk,m_npctwoout), Npcgenpoker (m_npctwo,m_ ARRGENPK, M_npctwoout), Isoutpkfinish ()), explained below one by one:

Code for CLEAROUTPK ():

void Gamescene::clearoutpk () {    ccobject* object;
Clear players out of cards
    Ccarray_foreach (M_PLAYEROUT->GETARRPK (), object) {        poker* pk = (poker*) object;        Pk->setvisible (FALSE);    }    M_PLAYEROUT->GETARRPK ()->removeallobjects ();
Clear the computer player out of the card    Ccarray_foreach (M_NPCTWOOUT->GETARRPK (), object) {        poker* pk = (poker*) object;        Pk->setvisible (FALSE);    }    M_NPCTWOOUT->GETARRPK ()->removeallobjects ();
Ibid    . Ccarray_foreach (M_NPCONEOUT->GETARRPK (), object) {        poker* pk = (poker*) object;        Pk->setvisible (FALSE);    }    M_NPCONEOUT->GETARRPK ()->removeallobjects ();    This->getchildbytag (Npconebuchu)->setvisible (false);    This->getchildbytag (Npctwobuchu)->setvisible (false);}
Npcoutpoker (m_npctwo,m_arrgenpk,m_npctwoout) computer card strategy:

Computer Card strategy I'm just making a simple judgment, playing the card with the lowest card value in the first place.

void Gamescene::npcoutpoker (player* npc,ccarray* out,player* out1) {//hides the last card out of ccobject* object;        Ccarray_foreach (OUT1-&GT;GETARRPK (), object) {//OUT1 is the last card out poker* pk = (poker*) object;    Pk->setvisible (FALSE);    } out1->getarrpk ()->removeallobjects ();    A card with the lowest card value, that is, the first position of the card type paixing px = Npc->m_vecpx.front ();    Out->removeallobjects ();        Three rules for the licensing if (Px.type = = Three_card) {stable_sort (Npc->m_vecpx.begin (), Npc->m_vecpx.end (), isShorter1);        M_type = Three_card;        With single for (Std::vector<paixing>::iterator Iter=npc->m_vecpx.begin (); Iter!=npc->m_vecpx.end (); ++iter)            {//Unless only two hands are left, you cannot bring the King and 2 poker* pk = Iter->vec.front ();            if (Pk->getnum () >= Er && npc->m_vecpx.size ()! = 1) break;                if (Iter->type = = Single_card) {OUT1-&GT;GETARRPK ()->addobject (Iter->vec.front ()); Out->addObject (Iter->vec.front ());                NPC-&GT;GETARRPK ()->removeobject (Iter->vec.front ());                Npc->m_vecpx.erase (ITER);                M_type = Three_one_card;            Break }}//With Double if (OUT1-&GT;GETARRPK ()->count () = = 0) {for (STD::VECTOR&LT;PAIXING&GT;:                : Iterator Iter=npc->m_vecpx.begin (); Iter!=npc->m_vecpx.end (); ++iter) {//Unless only two hands are left, you cannot take King and 2                poker* pk = Iter->vec.front ();                if (Pk->getnum () >= Er && npc->m_vecpx.size ()! = 1) break; if (Iter->type = = Double_card) {for (Std::vector<poker*>::iterator it=iter->v Ec.begin (); It!=iter->vec.end (); ++it) {OUT1-&GT;GETARRPK ()->addobject (*it)                        ;                        Out->addobject (*it);     NPC-&GT;GETARRPK ()->removeobject (*it);               } npc->m_vecpx.erase (ITER);                    M_type = Three_two_card;                Break }}}}//Three shun the card principle if (Px.type = = Aircraft_card) {//have enough single stable_sort with single (Npc->m_v        Ecpx.begin (), Npc->m_vecpx.end (), isShorter1);        M_type = Aircraft_card;            if (Getnpcpxnum (Npc,single_card) >= px.vec.size ()/3) {int num=0; For (Std::vector<paixing>::iterator It=npc->m_vecpx.begin (); It!=npc->m_vecpx.end () &&num<            Px.vec.size ()/3;)                    {if (It->type = = Single_card) {++num;                    OUT1-&GT;GETARRPK ()->addobject (It->vec.front ());                    Out->addobject (It->vec.front ());                    NPC-&GT;GETARRPK ()->removeobject (It->vec.front ());                    it = npc->m_vecpx.erase (it);     M_type = Aircraft_single_card;           } else ++it; }}//have enough double if with double if (Getnpcpxnum (Npc,double_card) >= px.vec.size ()/3 && OUT1-&GT;GETARRPK ()-&            Gt;count () = = 0) {int num=0; For (Std::vector<paixing>::iterator It=npc->m_vecpx.begin (); It!=npc->m_vecpx.end () &&num<            Px.vec.size ()/3;)                    {if (It->type = = Double_card) {++num;                        For (Std::vector<poker*>::iterator Ite=it->vec.begin (); Ite!=it->vec.end (); ++ite) {                        OUT1-&GT;GETARRPK ()->addobject (*ite);                        Out->addobject (*ite);                        NPC-&GT;GETARRPK ()->removeobject (*ite);                    M_type = Aircraft_dobule_card;                } it = Npc->m_vecpx.erase (it);            } else ++it;      }  }}//The card principle, direct out, do not do processing if (Px.type = = Connect_card) {m_type = Connect_card;    }//Double shun the card principle, direct out, do not do processing if (Px.type = = Company_card) {m_type = Company_card; }//pair and List card principle if (Px.type = = Double_card | | px.type = = single_card) {int threenum = Getnpcpxnum (npc,three_card        ) +getnpcpxnum (Npc,aircraft_card);        int chibangnum = Getnpcpxnum (npc,double_card) +getnpcpxnum (Npc,single_card); All three <= all pairs + all cards-2, out of the pair, otherwise out of three bands to if (Threenum <= chiBangNum-2 | | threenum = 0) {if (Px.type =            = double_card) M_type = Double_card;        if (Px.type = = single_card) M_type = Single_card;            } else {paixing px = Npc->m_vecpx.front ();            Std::vector<paixing>::iterator dle = Npc->m_vecpx.begin ();            Npc->m_vecpx.erase (DLE);            Npc->m_vecpx.push_back (px);            Npcoutpoker (NPC,OUT,OUT1);        Return }} for (std::Vector<poker*>::iterator Iter=px.vec.begin (); Iter!=px.vec.end ();        ++iter) {OUT1-&GT;GETARRPK ()->addobject (*iter);        Out->addobject (*iter);        NPC-&GT;GETARRPK ()->removeobject (*iter);    NPC-&GT;SETISOUTPK (TRUE);    } m_lastout = NPC; Remove px for (std::vector<paixing>::iterator it=npc->m_vecpx.begin () from NPC-&GT;M_VECPX; it!=npc->m_ Vecpx.end (); ++it) {if (It->type = = Px.type && it->vec.front ()->getnum () = = Px.vec.front ()->g            Etnum ()) {npc->m_vecpx.erase (IT);        Break }    }}

Npcgenpoker (M_NPCTWO,M_ARRGENPK, M_npctwoout), Isoutpkfinish ()) tag strategy:

void Gamescene::npcgenpoker (player* npc,ccarray* out, player* out1) {//Hide last card out if (M_ischibang) {Ccob        Ject* object;            Ccarray_foreach (OUT1-&GT;GETARRPK (), object) {poker* pk = (poker*) object;        Pk->setvisible (FALSE);    } out1->getarrpk ()->removeallobjects ();                                                      }/************************************************************************//* Find out the corresponding card-type cards *//************************************************************************/for (Std::vec Tor<paixing>::iterator Iter=npc->m_vecpx.begin (); Iter!=npc->m_vecpx.end ();  ++iter) {if (M_type = = Iter->type) {//Determine aircraft, connecting cards if (M_type = = Aircraft_card | | M_type = = Connect_card | |            M_type = = Company_card) if (Out->count ()! = Iter->vec.size ()) continue;          poker* pk = (poker*) out->objectatindex (Out->count ()-1);  poker* PK1 = Iter->vec.front ();                If the other party is one of the more than 2 cards not out if (!npc->getisdizhu () &&!m_lastout->getisdizhu ()) {  if (Pk1->getnum () >=er | | m_type = = bomb_card) {//pass if (NPC = =                    M_npcone) This->getchildbytag (Npconebuchu)->setvisible (true);                    if (NPC = = M_npctwo) This->getchildbytag (Npctwobuchu)->setvisible (true);                    NPC-&GT;SETISOUTPK (FALSE);                Return }} if (Pk1->getnum () > Pk->getnum ()) {out->removeallobjects ()                ; for (Std::vector<poker*>::iterator it = Iter->vec.begin (); It!=iter->vec.end (); ++it) {out1-                    &GT;GETARRPK ()->addobject (*it);                    NPC-&GT;GETARRPK ()->removeobject (*it);         Out->addobject (*it);       } npc->m_vecpx.erase (ITER);                NPC-&GT;SETISOUTPK (TRUE);                M_lastout = NPC;            Return    }}}//Three with one or three with two if (Sandaiyiorer (NPC,OUT,OUT1)) return;    Four with single or four-band dual//aircraft with single or dual if (Feijidaichibang (NPC,OUT,OUT1)) return;                                                  /************************************************************************//* If there's a hand left in the bomb *//************************************************************************/if (npc->m_vecpx.s ize () = = 2) {for (Std::vector<paixing>::iterator iter=npc->m_vecpx.begin (); Iter!=npc->m_vecpx.end ( ); ++iter) {if (Iter->type = = Bomb_card && m_type! = Bomb_card) {out-                >removeallobjects (); for (Std::vector<poker*>::iterator it = Iter->vec.begin (); It!=iter->vec.end (); ++it) {out1- &GT;GETARRPK ()->addobject (*it);                    NPC-&GT;GETARRPK ()->removeobject (*it);                Out->addobject (*it);                } npc->m_vecpx.erase (ITER);                M_lastout = NPC;            Return                                        }}}/************************************************************************//* If the card is a party *//************************************************************************/if (!NPC ->getisdizhu () &&!m_lastout->getisdizhu ()) {//pass if (NPC = = M_npcone) THIS-&G        T;getchildbytag (Npconebuchu)->setvisible (true);        if (NPC = = M_npctwo) This->getchildbytag (Npctwobuchu)->setvisible (true);        NPC-&GT;SETISOUTPK (FALSE);    Return                                                        }/************************************************************************//* Split sheet card with */    /********************************************/if (Npcchaidan (NPC,OUT,OUT1)) return;                                                        /************************************************************************//* Split Shuang Pai with *//************************************************************************/if (Npcchaidui (npc,o    UT,OUT1)) return;                                                        /************************************************************************//* Split three cards with *//************************************************************************/if (Npcchaisan (NPC,    OUT,OUT1)) return;                                                        /************************************************************************//* Remove the aircraft card with *//************************************************************************/if (Npcchaifeiji (NP    C,OUT,OUT1)) return;               /************************************************************************//* Connect with the card                                         */    /************************************************************************/    if (Npcchailianpai (NPC,OUT,OUT1)) return;                                                        /************************************************************************//* Split double shun with *//************************************************************************/if (Npcchaishuangshu    N (NPC,OUT,OUT1)) return;        Fried for (std::vector<paixing>::iterator iter=npc->m_vecpx.begin (); Iter!=npc->m_vecpx.end (); ++iter) {            if (Iter->type = = Bomb_card) {//If the card is not out of the bomb to fry, otherwise the size of the bombing if (m_type! = Bomb_card)                {out->removeallobjects (); for (Std::vector<poker*>::iterator it = Iter->vec.begin (); It!=iter->vec.end (); ++it) {out1-                    &GT;GETARRPK ()->addobject (*it);                NPC-&GT;GETARRPK ()->removeobject (*it);    Out->addobject (*it);                } npc->m_vecpx.erase (ITER);                M_type = Bomb_card;                NPC-&GT;SETISOUTPK (TRUE);                M_lastout = NPC;            Return                }else {poker* pk = (poker*) out->objectatindex (0);                poker* PK1 = Iter->vec.front ();                    if (Pk1->getnum () >pk->getnum ()) {out->removeallobjects ();                        for (Std::vector<poker*>::iterator it = Iter->vec.begin (); It!=iter->vec.end (); ++it) {                        OUT1-&GT;GETARRPK ()->addobject (*it);                        NPC-&GT;GETARRPK ()->removeobject (*it);                    Out->addobject (*it);                    } npc->m_vecpx.erase (ITER);                    M_type = Bomb_card;                    NPC-&GT;SETISOUTPK (TRUE);                    M_lastout = NPC;             Return   }}}}//pass if (NPC = = M_npcone) {This->getchildbytag (npcone    Buchu)->setvisible (true);    } if (NPC = = M_npctwo) {this->getchildbytag (Npctwobuchu)->setvisible (true); } NPC-&GT;SETISOUTPK (false);}

One of the code is not analyzed, please yourself to the first three chapters to download the source reading. This is the end of the article! Thank you for your support!!!

COCOS2DX "single bucket landlord" source anatomy of eight computer players play cards and cards (end)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.