Implement the mahjong Card Algorithm and mahjong Card Algorithm

Source: Internet
Author: User

Implement the mahjong Card Algorithm and mahjong Card Algorithm

The first implementation is the implementation of algorithms.

Requirement: the hacker can only touch the hacker without having to eat or listen. The hacker can play any card but cannot touch the hacker.

I don't know how to play mahjong yet, but I am taught by my boss. Pai_^

The most troublesome is the Hu Pai algorithm. All the cards found previously do not contain normal cards of the sub-accounts. They use more than % 3 2, and the remainder 2 means the remainder.

However, this is not the case if you have a token. I had to write one myself.

There are 136 cards, cards, cakes, bars, 34 cards in the north and west of China.

Four sub-accounts are fake, and the worst case is that there are three sub-accounts. However, if the traversal times, 34X34X34 is nearly 40 thousand times without logic judgment.

Think about it. The worst case is that the last time the card was identified, it would be a waste of nearly 40 thousand previous judgments.

Here, we will change our mindset, that is, the number of sub-accounts required to achieve the card as needed, rather than blindly traversing and then judging the card.

The correctness of the algorithm: If you want to hide a card, you must make a trigger (normal card ). Specifically, it refers to shunzi or three brands (for example, one cake, two cakes, three cakes, or Dongfeng ). It will refer to two duplicate cards.

Four situations:

1. If [ten thousand] is used, the [pie] [bar] [wind] (including sending white) will inevitably be a flat.

2. If [10 thousand] [bar] [wind] (including sending white) in [pie] will inevitably be a whole flutter.

3. If [10 thousand] [pie] [wind] (including the publish white) will inevitably be a flat.

4. if it is going to be included in the [wind] (included in the white), then the [ten thousand] [pie] [bar] will inevitably be a whole flutter.

Assume that the number of shards is curHunNum.

Now we need to get the [ten thousand] [pie] [bar] [wind] into the number of shards required for the whole flutter. If this is the case 1.

NeedHunNum = [pie] becomes the number of shards required for the whole flutter + [bar] becomes the number of shards required for the whole flutter + [wind] becomes the number of shards required for the whole flutter;

If hadHunNum = needHunNum-curHunNum; If hadHunNum <0 requires more than it has, no judgment is made.

Otherwise, you will be able to determine the number required by the total number of RMB.

Case 2, case 4, and so on.

There are three main functions:

1. Test Hu Pai

// Issued card bool Player: testHuPai (Pai * pai) {for (int I = mj1__hun; I <= mj1__fengzfb; I ++) {my1_copy [I]. clear () ;}vector <Pai * >:: iterator iter; for (int I = mj1__hun; I <= mj1__fengzfb; I ++) {for (iter = m_MyPAIVec [I]. begin (); iter! = M_MyPAIVec [I]. end (); iter ++) {mydomaincopy [I]. push_back (* iter) ;}// insert insertPai (pai, my‑copy); int curHunNum = my‑copy [mj1__hun]. size (); if (curHunNum> 3) {CCLOG ("Four Hun !! "); Return true;} addIDEveryPai (my‑copy); int needHunNum = 0; int jiangNeedNum = 0; needMinHunNum = mj1__hunmax; bytes (my‑copy [mj1__wan], 0 ); int wanToPuNeedNum = needMinHunNum; needMinHunNum = bytes; bytes (my‑copy [mj1__bing], 0); int bytes = needMinHunNum; needMinHunNum = bytes; bytes (my‑copy [bytes], 0 ); int tiaoToPuNeedNum = nee DMinHunNum; needMinHunNum = mj1__hunmax; bytes (my‑copy [bytes], 0); int bytes = needMinHunNum; CCLOG ("wanToPuNeedNum: % d Bytes: % d tiaoToPuNeedNum: % d Bytes: % d ", wanToPuNeedNum, bingToPuNeedNum, tiaoToPuNeedNum, fengToPuNeedNum ); // It Will Be In hichina. // if the expected number of obfuscation values is less than or equal to the current number, the number of obfuscation numbers needHunNum = bingToPuNeedNum + tiaoToPuNeedNum + fengToPuNeedNum will be calculated; if (needHunNum <= curHu NNum) {CCLOG ("jiang in wan: % d", needHunNum, curHunNum); int vecSize = my1_copy [mj1__wan]. size (); int hasNum = curHunNum-needHunNum; m_HuPaiVec.clear (); // for (iter = my‑copy [mj1__wan]. begin (); iter! = My‑copy [mj1__wan]. end (); iter ++) {m_HuPaiVec.push_back (* iter);} bool isHu = vecCanHu (hasNum); if (isHu) return true ;} // in the pie, needHunNum = wanToPuNeedNum + bytes + fengToPuNeedNum; if (needHunNum <= curHunNum) {CCLOG ("jiang in bing: % d", needHunNum, curHunNum ); int vecSize = my‑copy [mj1__bing]. size (); int hasNum = curHunNum-needHunNum; m_HuPaiVec.clear (); // for (iter = my‑copy [MJPAI _ BING]. begin (); iter! = My‑copy [mj1__bing]. end (); iter ++) {m_HuPaiVec.push_back (* iter);} bool isHu = vecCanHu (hasNum); if (isHu) return true ;} // needHunNum = wanToPuNeedNum + forward + fengToPuNeedNum; if (needHunNum <= curHunNum) {CCLOG ("jiang in tiao: % d", needHunNum, curHunNum ); int vecSize = my‑copy [mj1__tiao]. size (); int hasNum = curHunNum-needHunNum; m_HuPaiVec.clear (); // for (iter = my‑copy [MJPA I _TIAO]. begin (); iter! = My‑copy [mj1__tiao]. end (); iter ++) {m_HuPaiVec.push_back (* iter);} bool isHu = vecCanHu (hasNum); if (isHu) return true ;} // needHunNum = wanToPuNeedNum + quota + tiaoToPuNeedNum; if (needHunNum <= curHunNum) {CCLOG ("jiang in feng: % d", needHunNum, curHunNum ); int vecSize = my‑copy [mj1__fengzfb]. size (); int hasNum = curHunNum-needHunNum; m_HuPaiVec.clear (); // for (iter = myPaiCo Py [mj1__fengzfb]. begin (); iter! = My‑copy [mj1__fengzfb]. end (); iter ++) {m_HuPaiVec.push_back (* iter);} bool isHu = vecCanHu (hasNum); if (isHu) return true;} return false ;}
2. The number of shards required for the whole throttling:
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 last one is in the east, west, north, and south, it is impossible to make the order if (P2-> getType () = mj1__fengzfb) {if (p1-> getValue () = p2-> getValue () 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 ;}// Pai * p1 = typeVec must be greater than or equal to 3 cards. at (0); Pai * p2 = typeVec. at (1); Pai * p3 = typeVec. at (2); // the first one that throws itself if (needNum + 2 <needMinHunNum) {typeVec = removeVector (p1, typeVec); getNeedHunNumToBe Pu (typeVec, needNum + 2); typeVec = insertVector (p1, typeVec);} // if (needNum + 1 <needMinHunNum) {// p1 is wind if (p1-> getType () = mj1__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]; // 455567 the possible combination here may be 45 46 otherwise 45 45 46 // if the current value is not equal to the next value, combine it with the next one to avoid 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 one and the other two are one. // The interval between the two is not the same as the previous one. 222234 // The probability is 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 = removeVector (p3, typeVec); getNeedHunNumToBePu (typeVec, needNum ); typeVec = insertVector (p3, typeVec); typeVec = insertVector (p2, typeVec); typeVec = insertVector (p1, typeVec);} // 4556 }}}
3. Determine whether a certain type can be a one-stop task:

bool Player::vecCanHu(int hunNum){CCLOG("vecCanHu");int huSize = m_HuPaiVec.size();DebugPlayer(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 = huPaiCopy.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;}
The vecCanHu function can be used to strengthen pruning. For example, the idea of adding % 3 + 2 to the Shard is changed, but the optimization is not continued very quickly during the test.

Mahjong is almost finished and has learned a lot. Come on, cool !!



Mahjong card algorithm C/C ++

The words can only be engraved...
Then you can output all the possibilities...
Simple
1112345678999
Just grab the one with the same color.
The code is not written. Let's take a rough look.
First, classify them into tens of thousands, tubes, entries, and words.
Then let's straighten out the box, cylinder, and bar, and see if there is a forward. If there is a forward
Then we can only judge the moment later.
If not, the following words can be output
The difficulty in the middle is that, sometimes, when the same three cards are carved, the cards will become broken. Therefore, priority must be given to them. Of course, you do not need to calculate the card size here.

Mahjong Card Algorithm

Chinese national standard Mahjong rules

88 times
A card is composed of four wind stamps (bars. No matter the cycle, the door, the three winds, the touch and
Among the two three yuan cards, there are three engraved ones. No arrows
3 green one color consists of 23468 and any card in the text, shun zi, engraved five, Will and card. Not mixed. If there is no card consisting of the word "hair", it can be clear
4 9 Lotus treasure lamp consists of a specific card type consisting of a color ordinal number card consisting of 1112345678999, see any one ordinal number card of the same color to generate and card. Not clear
5, 4, 4
A 6-to-7 pair consists of seven pairs connected by a color sequence card. All-in-One, unattended, single fishing
Thirteen thousand cards consist of one or nine cards with three sequence numbers, and seven types of cards and one of them will constitute a pair of cards. No matter what the five steps are, do not seek human, single fishing

64-fold
8. The card is composed of a sequence card, a sequence card, and a sequence card. Without touch and touch, At the same moment, without words
9. The four-happiness card has a three-letter badge and a badge. Not counted
There are two sides of the Wrigley and the badge. No arrows
The 11-Character Color consists of the engraved sub-cards (bars) and the cards that will be formed. Without touch and
12: 4: 4: 4: Dark bars ). Clear, touch, and
13 one-color dual-dragon club one color of two old pair, 5 for the card. Excluding all, seven, and all

48 times
14 one-color, four-color, one-color, one-color, four-color, four-color, three-section high, general high, four-to-one
15. One color, four colors, one color, four colors, one digit in sequence.

32fan
16. One color, four steps, one color, four colors, one digit in sequence, or two digits in sequence.
17. Three bars
18. Without touch and

24fan
19 seven pairs consist of seven pairs and cards. Independent of seeking, single fishing
147 stars do not rely on the need to have seven leaflets in the east, west, north and south of the white, plus three colors, the number of 258, 369, in the seven sequence card composition of no cards and cards. No matter what the five steps are, do not seek human, single fishing
21 dual-engraved cards consist of 2, 4, 6, and 8 sequence cards. Without touch and interruption
22. No words
23 one color three with Shun and card when there is a color 3 with the same ordinal number of Shun. Three-section high regardless of the color
24 color three-section high and card when there is a color 3 pairs in turn increments by a digit engraved. Excluding all colors, three in the Same Direction
25. It consists of a sequence card of 789. It is used to cut the sub-bars, and the cards that match the cards. Wordless
26. The number card consists of a sequence card of 456. Disallow
27 The sum of the cards that are composed of a sequence card of 123. Wordless

16 times
28 when Qinglong and card are connected, there is a sequential number card with a color of 1-9
29 three-color dual-dragon club two colors, two old and young deputy, another color 5 as the harmony card. No matter whether they meet each other, no words, no words, no peace
30 color Three-Step high and card, there is a color three pairs in turn increase one or in turn increase two numbers in turn
31. Each deck and each deck must have a sequence card of 5. Disallow
32. triplicate; 3. subscribe with the same ordinal number (horizontal bar)
33, 3, and 3

12fan
34 do not rely on a single 3 colors of 147, 258, 369 can not be misplaced ordinal number card and any 14 cards in the Southeast and northwest China. No matter what the five steps are, do not seek human, single fishing
Up to 147, 258, and 369 of the three colors of the 35-Dragon combination cannot be misplaced.
36 or more cards that consist of a sequence card of 6-9 shunzi, engraved sub-cards, and cards. Wordless
37 less than five are the cards and cards which consist of the sequence number card 1-4. Wordless
38. 3. 3.

8fan
3 pairs of three colors of 39 hualong are connected to a sequence card of 1-9.
40. A card consists of a card chart with no difference between top and bottom, including 1234589 pie, 245689 entries, and a whiteboard. One missing entry
When the three colors are the same as those of the card, there are three colors and three orders with the same number.
42 three-color three-section high and card, there are 3 kinds of color 3 in turn ...... the remaining full text>
 

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.