Mahjong Hu Card, the algorithm of hearing cards

Source: Internet
Author: User
Tags lua


Algorithm analysis refer to the following blog Mahjong Poker algorithm


I used LUA to write a simplified mahjong algorithm, only to consider mahjong cards and million

Card number 1~9 corresponds to 190,000, 10-16 is the word card


Here's the LUA code


Local print = printlocal Tconcat = table.concatlocal Tinsert = table.insertlocal Srep = string.replocal type = typelocal p    airs = pairslocal ToString = tostringlocal Next = Next function print_r (root) Local cache = {[Root] = "."}             Local function _dump (t,space,name) Local temp = {} for k,v in pairs (t) do local key = ToString (k) If CACHE[V] then Tinsert (temp, "+": Key: " {" ..            CACHE[V]: "}") ElseIf type (v) = = "Table" then local New_key = Name: "." .. Key Cache[v] = New_key Tinsert (temp, "+"): Key: _dump (V,space. (Next (T,k) and "|" or ""): Srep ("", #key), New_key)) Else Tinsert (temp, "+": Key: " [" ..            ToString (V): "]") End End return Tconcat (temp, "\ n").     Space) End Print (_dump (Root, "", "")) endlocal Hu = {}function analyze (cards, Zi) Print_r (cards) print ("Zi", Zi) If not next (cards) then return True End local start local over if Zi then start = ten over = + Else start = 1 OVER = 9 End Local finish = true for i = start, over does if cards[i] and Cards[i] ~= 0 Then St    Art = I finish = False break end End If finish then--hand is empty return True end Print ("Start", start) local result if Cards[start] and Cards[start] >= 3 then--engraved cards[start] = cards [Start]-3 result = analyze (cards, zi) cards[start] = Cards[start] + 3 return result End If n Ot Zi and Cards[start +1] and Cards[start + 2] and Cards[start + 1] > 0 and Cards[start + 2] > 0 Then Cards[s        Tart] = Cards[start]-1 Cards[start + 1] = Cards[start + 1]-1 Cards[start + 2] = Cards[start + 2]-1        result = analyze (cards, zi) cards[start] = Cards[start] + 1 Cards[start + 1] = Cards[start + 1] + 1 Cards[start + 2]= Cards[start + 2] + 1 return result end return falseendfunction fmod (arr, num) Local count = 0 for K, V in pairs (arr) does count = count + V End return count% numend--can be Hu function Hu:win (cards) Local JIANGP OS local Yushu Local jiangexisted = False local wan = {} local Zi = {} for k, V in pairs (cards) do I F K < ten then wan[k] = v else zi[k] = V End end--Wan Yushu = Fmod (WAN, 3    if Yushu = = 1 then return false ElseIf Yushu = = 2 then Jiangpos = Wan Jiangexisted = True End--zi Yushu = Fmod (Zi, 3) if Yushu = = 1 then return false ElseIf Yushu = 2 Then If Jiangexi       Sted then return false Else Jiangpos = Zi end End If Jiangpos = = Wan Then If not analyze (Zi, true) then return to false end ElseIf not analyze (WAN, false) then return Fals E End If not jianGPos then – no Jiang return False end--Jiang local success = False for K, V in pairs (jiangpos) does                If v >= 2 then jiangpos[k] = V-2 if analyze (jiangpos, jiangpos = Zi) Then         Success = True End Jiangpos[k] = v if success then break end End end return successend--How to listen card function hu:ting (cards) Local success = False local post = {} local WI ll = {} for k, V in pairs (cards) does if v > 0 then cards[k] = cards[k]-1 Print_r (Cards  ) Local willcards = {} For i = 1, + do if I ~= K then if Cards[i]                    Then cards[i] = Cards[i] + 1 Else cards[i] = 1 End success = Self:win (cards) cards[i] = cards[i]-1 I          F Success Then              Table.insert (willcards, i) end end end Cards[k] = CA RDS[K] + 1 if Next (willcards) then will[k] = Willcards End End End Retur n willendlocal player = {[1] = 1, [2] = 1, [3] = 1, [4] = 3, [ten] = 3, [n] = 1, [] = 1,}print_ R (hu:ting (player)) return Hu



Mahjong Hu Card, the algorithm of hearing cards

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.