The first implementation is the implementation of the algorithm.
Demand: Touch the bar Hu, can not eat, can not listen, can only self-touch Hu, which Laizi can do any card but not touch and bar.
When you write, you don't play Mahjong or the boss teaches. ^_^
The most troublesome is the HU card algorithm. Before the search is not included Laizi normal Hu card, with a% of 3 more than 2, of which the remainder of 2 is the meaning of the remaining.
But a laizi can't be used that way. Had to write one by himself.
One has 136 cards, million, cakes, strips, East White 34 kinds of cards.
There are four Laizi are directly on the HU card, the worst case is that there are 3 Laizi, but if you go through the logic to judge the 34x34x34 nearly 40,000 times.
Think about if you can, the worst case is in the last Judgment can be Hu, that before the nearly 40,000 times the judgment is wasted.
Here is a change of thinking, is the purpose of the need to take the Cheng Ho card required number of Laizi, rather than blindly traverse and judge the Hu card.
The correctness of the algorithm: if you want to be a HU card is bound to be three flutter one will (normal Hu card). Where flutter refers to the straight or triple card (such as a cake of two cakes and three cakes). Will refer to the two re-cards.
Four different situations:
1. If the "million" in the "Cake" "Bar" "Wind" (including the white) must be the whole flutter.
2. If the "cake" in the "Million" "Bar" "Wind" (including the white) must be the whole flutter.
3. If the "bar" in the "Million" "Cake" "Wind" (including the white) must be the whole flutter.
4. If the "wind" inside (including the white) then "million" "Cake" "bar" must be the whole flutter.
If the current number of Laizi is curhunnum.
Now get the "million" "Cake" "Bar" "Wind" each becomes the number of Laizi required for the whole topology, if it is a situation.
Needhunnum= "cake" need to Laizi number + "bar" need to Laizi number + "Wind" need to laizi number;
If Hadhunnum = Needhunnum-curhunnum, if the hadhunnum<0 demand is more than the possession of the judgment.
Otherwise, it will be necessary to determine the number of "million" to become an entire flutter.
Case 234 and so on.
There are three main functions:
1. Test the HU Card
Issued by the card bool Player::testhupai (pai* Pai) {for (int i = Mjpai_hun; I <= mjpai_fengzfb; i++) {mypaicopy[i].clear ();} Vector<pai*>::iterator iter;for (int i = Mjpai_hun; I <= mjpai_fengzfb; i++) {for (iter = M_mypaivec[i].begin (); I ter! = M_mypaivec[i].end (); iter++) {mypaicopy[i].push_back (*iter);}} Insert Insertpai (Pai, mypaicopy), int curhunnum = Mypaicopy[mjpai_hun].size (), if (curhunnum>3) {Cclog ("four hun!!"); return true;} Addideverypai (mypaicopy); int needhunnum = 0;int Jiangneednum = 0;needminhunnum = Mjpai_hunmax;getneedhunnumtobepu ( Mypaicopy[mjpai_wan], 0); int wantopuneednum = Needminhunnum;needminhunnum = Mjpai_hunmax;getneedhunnumtobepu ( Mypaicopy[mjpai_bing], 0); int bingtopuneednum = Needminhunnum;needminhunnum = Mjpai_hunmax;getneedhunnumtobepu ( Mypaicopy[mjpai_tiao], 0); int tiaotopuneednum = Needminhunnum;needminhunnum = Mjpai_hunmax;getneedhunnumtobepu ( MYPAICOPY[MJPAI_FENGZFB], 0); int fengtopuneednum = Needminhunnum; Cclog ("wantopuneednum:%d bingtopuneednum:%d tiaotopuneednum:%D fengtopuneednum:%d ", Wantopuneednum, Bingtopuneednum, Tiaotopuneednum, fengtopuneednum);//will be in 万中// If the required mix is less than or equal to the current then the calculation will be in the number of mixes that will be required in million Needhunnum = Bingtopuneednum + tiaotopuneednum + fengtopuneednum;if (needhunnum <= Curhunnum) {Cclog ("Jiang in wan:%d%d", Needhunnum, curhunnum); int vecsize = Mypaicopy[mjpai_wan].size (); int hasnum = CurH Unnum-needhunnum;m_hupaivec.clear ();//Do not pack the mix for (iter = Mypaicopy[mjpai_wan].begin (); ITER! = Mypaicopy[mjpai_wan].end (); iter++) {m_hupaivec.push_back (*iter);} BOOL Ishu = Veccanhu (Hasnum); if (Ishu) return true;} Will be in the pie Needhunnum = wantopuneednum + tiaotopuneednum + fengtopuneednum;if (needhunnum <= curhunnum) {CCLOG ("Jiang in bin g:%d%d ", Needhunnum, curhunnum); int vecsize = Mypaicopy[mjpai_bing].size (); int hasnum = Curhunnum-needhunnum;m_hupaive C.clear ();//Do not pack the mix for (iter = Mypaicopy[mjpai_bing].begin (); ITER! = Mypaicopy[mjpai_bing].end (); iter++) {m_ Hupaivec.push_back (*iter);} BOOL Ishu = Veccanhu (Hasnum); if (Ishu) return true;} will be needhunnum in bar = WantopunEednum + bingtopuneednum + fengtopuneednum;if (needhunnum <= curhunnum) {cclog ("Jiang in tiao:%d%d", NeedHunNum, CurHun Num), int vecsize = Mypaicopy[mjpai_tiao].size (), int hasnum = Curhunnum-needhunnum;m_hupaivec.clear (),//No mix for (iter = Mypaicopy[mjpai_tiao].begin (); Iter! = Mypaicopy[mjpai_tiao].end (); iter++) {m_hupaivec.push_back (*iter);} BOOL Ishu = Veccanhu (Hasnum); if (Ishu) return true;} Will be in the wind Needhunnum = wantopuneednum + bingtopuneednum + tiaotopuneednum;if (needhunnum <= curhunnum) {CCLOG ("Jiang in Fen g:%d%d ", Needhunnum, curhunnum); int vecsize = Mypaicopy[mjpai_fengzfb].size (); int hasnum = Curhunnum-needhunnum;m_hupa Ivec.clear ()///Not mixed (Laizi) card for (iter = Mypaicopy[mjpai_fengzfb].begin (); ITER! = Mypaicopy[mjpai_fengzfb].end (); iter++) { M_hupaivec.push_back (*iter);} BOOL Ishu = Veccanhu (Hasnum); if (Ishu) return true;} return false;}2. The number of Laizi to be required for the entire topology:
void Player::getneedhunnumtobepu (Vector<pai*>typevec, int neednum) {if (Needminhunnum = = 0) return;if (needNum >= needminhunnum) Return;int vsize = Typevec.size (); if (vsize = = 0) {needminhunnum = MIN (Neednum, needminhunnum); return ;} else if (vsize = = 1) {Needminhunnum = MIN (Neednum + 2, needminhunnum); return;} else if (vsize = = 2) {pai* P1 = typevec.at (0); pai* P2 = typevec.at (1);//If the latter is east in the white can not be the appearance of a shun card if (p2->gettype () = = MJPAI_FENGZFB) {if (p1->getvalue () = = P2->g Etvalue ()) Needminhunnum = MIN (needminhunnum, Neednum + 1); Vector<pai*>::iterator iter;for (iter = TypeVec.begin () ; Iter! = Typevec.end (); iter++) {cclog ("3pai:%d:type:%d value:%d", Needminhunnum, (*iter)->gettype (), (*iter)->getvalue ());} return;} else if (P2->getvalue ()-P1->getvalue () < 3) {Needminhunnum = MIN (needminhunnum, Neednum + 1);} return;} Greater than or equal to 3 cards pai* p1 = typevec.at (0); pai* P2 = typevec.at (1); pai* p3 = typevec.at (2);//First self-flutter if (Neednum + 2<needminhunnum) {Typevec = Removevector (P1, Typevec); Getneedhunnumtobepu (Typevec, Neednum + 2); Typevec = Insertvector (P1, Typevec);} The first one with the other is a flutter if (Neednum + 1<needminhunnum) {//p1 is the wind if (p1->gettype () = = MJPAI_FENGZFB) {if (p1->getvalue () = = P2->getvalue ()) {Typevec = Removevector (P1, typevec); Typevec = Removevector (P2, Typevec); Getneedhunnumtobepu ( Typevec, Neednum + 1); Typevec = Insertvector (P2, typevec); Typevec = Insertvector (P1, Typevec);}} else{for (unsigned int i = 1; i < typevec.size (); i++) {if (Neednum + 1 >= needminhunnum) break;p2 = typevec[i];//455 567 here can be combined may be 45 46 otherwise 45 45 45 46//If the current value is not equal to the next value then and the next combination avoids repeating if (i + 1! = Typevec.size ()) {p3 = typevec[i + 1];if (p3 ->getvalue () = = P2->getvalue ()) continue;;} if (P2->getvalue ()-P1->getvalue () < 3) {Typevec = Removevector (P1, typevec); Typevec = Removevector (P2, TypeVec) ; Getneedhunnumtobepu (Typevec, Neednum + 1); Typevec = Insertvector (P2, typevec); Typevec = Insertvector (P1, typeVec);} else break;}}} The first and the other two a flutter//back interval two sheets not the same as the previous one 222234//possibilityFor 222 234for (unsigned int i = 1; i < typevec.size (); i++) {if (neednum >= needminhunnum) break;p2 = Typevec[i];if (i + 2 < Typevec.size ()) {if (Typevec[i + 2]->getvalue () = = P2->getvalue ()) continue;} for (unsigned int j = i + 1; j < Typevec.size (); j + +) {if (neednum >= needminhunnum) break;p3 = typevec[j];if (P1 = = P3) {Cclog ("cao!!");} if (j + 1 < Typevec.size ()) {if (p3->getvalue () = = Typevec[j + 1]->getvalue ()) continue;} if (Test3combine (P1, p2, p3)) {Typevec = Removevector (P1, typevec); Typevec = Removevector (P2, typevec); Typevec = Removevec Tor (P3, Typevec); Getneedhunnumtobepu (Typevec, neednum); Typevec = Insertvector (p3, typevec); Typevec = Insertvector (P2, TYPEVEC); Typevec = Insertvector (P1, Typevec);} 4556}}}3. Determine whether a type can become an integral flutter one:
BOOL Player::veccanhu (int hunnum) {cclog ("Veccanhu"); int husize = M_hupaivec.size ();D ebugplayer (HUVEC); if (Husize < = 0) {if (Hunnum >= 2) return True;return false;} pai* Firstpai = M_hupaivec.front (); int ktype = Firstpai->gettype (); Vector<pai*>::iterator Iter;vector<Pai *> hupaicopy;for (iter = M_hupaivec.begin (); ITER! = M_hupaivec.end (); iter++) Hupaicopy.push_back (*iter); for (iter = h Upaicopy.begin (); Iter! = Hupaicopy.end (); iter++) {if (iter = = Hupaicopy.end ()-1) {Cclog ("last iterator!!"); if (Hunnum > 0) {hunnum = Hunnum-1;removehupai (*iter); needminhunnum = Mjpai_hunmax;getneedhunnumtobepu (m_HuPaiVec, 0 if (needminhunnum <= hunnum) {cclog ("t:%d v:%d", (*iter)->gettype (), (*iter)->getvalue ()); return true;} Hunnum = Hunnum + 1;inserthupai (*iter);}} Else{if (iter + 2 = = Hupaicopy.end ()) | | ((*iter)->getvalue ()! = (* (iter + 2))->getvalue ())) {Cclog ("other iterator!!"); if (Test2combine (*iter, * (iter + 1))) {Cclog ("%d%d", (*iter)->gettype (), (*iter)-≫getvalue ()); Removehupai (*iter); Removehupai (* (iter + 1)); needminhunnum = Mjpai_hunmax;getneedhunnumtobepu (m_ Hupaivec, 0); if (Needminhunnum <= hunnum) {cclog ("t:%d v:%d", (*iter)->gettype (), (*iter)->getvalue ()); return true;} Inserthupai (*iter); Inserthupai (* (iter + 1));}} if (hunnum>0 && (*iter)->getvalue ()! = (* (iter + 1))->getvalue ()) {Cclog ("%d%d", (*iter)->gettype () , (*iter)->getvalue ()) Hunnum = Hunnum-1;removehupai (*iter); needminhunnum = Mjpai_hunmax;getneedhunnumtobepu (m_ Hupaivec, 0); if (Needminhunnum <= hunnum) {cclog ("t:%d v:%d", (*iter)->gettype (), (*iter)->getvalue ()); return true;} Hunnum = Hunnum + 1;inserthupai (*iter);}}} return false;}One of the Veccanhu functions can be further enhanced pruning, such as changing the laizi when adding more than 3 of the idea of 2.
Mahjong is almost finished, learned a lot of things. Come on, you old man!
Laizi Mahjong Game Algorithm implementation