The blog link address: http://blog.csdn.net/weixin_41082095/article/details/79258241
The source code download LUA
Whether a set of mahjong can hu card must meet the following formula (special card type to go out, seven pairs, dragon seven pairs)
AAA + N * N * ABC + 1 * AA
see you understand? If a deck of CARDS carved by N, N CARDS, one and only pair of the brand! Then we can think the deck hu!
If you haven't understand it doesn't matter, the following we describe what mean!
AAA ABC AA
AAA: son engraved three colors, points the same token, commonly known as a cut card
ABC: even brand three colors, points vary a little each other such as: 123, 345, 678 commonly known as a CARDS
AA: for two colors, points the same token, commonly known as mahjong
Can recall we usually play mahjong after hu card card type are to meet such a formula,
Now we know that the hu brand formula, so we in the programming language is how to calculate??????
To do a good job, must first sharpen his!!!!!! First, we define a mahjong data structure
We know that a pair of mahjong there are 3 kinds of color (article, tube,), 9 points (1-9), we can define a three dimensional array to store our hand
The Local handCard=
{
,0,0,0,0,0,0,0,0 {0}, - said
,0,0,0,0,0,0,0,0 {0}, - said tube
,0,0,0,0,0,0,0,0 {0} - said article
}
As shown in figure:
This deck of CARDS in array can so to define the
The Local handCard=
{
,2,1,0,1,0,2,0,0 {1}, - said
,0,1,1,1,0,2,0,1 {0}, - said tube
,0,0,0,0,0,0,0,0 {0} - said article
}
Read the understands everything, each value several times for each card, we can also in handCard more define an array used to represent the attribute of each card, convenient to JiSuanLian brand behind us
handCard [4]={}
For I=1, 3 do
For j=1, 9 do
For h=1, card [I] [j] do
The local cardData={https://bbs.csdn.net/topics/}
CardData. Color=I
CardData. Point=j
Card [4] [# card + 1] [4]=cardData
End
End
End
So we hand them how to use the data to represent was finished, then we can start to open card, define several array are used to store appeared four times with a card, three CARDS, two CARDS,
local cardData=https://bbs.csdn.net/topics/{}
Self. TwoCardAry={}
Self. ThreeCardAry={}
Self. FourCardAry={}
Self. CloneCardList={}
For I=1, 3 do
For j=1, 9 do
cardData=https://bbs.csdn.net/topics/{}
If tempCardList [I] [j]==2 then
CardData. Color=I
CardData. Point=j
Self. TwoCardAry [# self. TwoCardAry + 1]=cardData
Elseif tempCardList [I] [j]==3 then
CardData. Color=I
CardData. Point=j
Self. ThreeCardAry [# self. ThreeCardAry + 1]=cardData
Elseif tempCardList [I] [j]==4 then
CardData. Color=I
CardData. Point=j
Self. FourCardAry [# self. FourCardAry + 1]=cardData
End
End
End
Finally, in turn, can form each card type are traversal time, see if we can meet before we define the final formula of hu CARDS
- whether hu CARDS parameter 1 list 2 dozen that card 3 short of the design and color of
The function Algorithm: PlayerIs_Hu (cardList nSendCardValue, nQueSe)
The local tempCardList={}
TempCardList [4]={}
For I=1, 3 do
TempCardList [I]={}
For j=1, 9 do
[j]=cardList tempCardList [I] [I] [j]
End
End
TempCardList [nSendCardValue color] [nSendCardValue point]=tempCardList [nSendCardValue. Color] [nSendCardValue. Point] + 1
Self: SortPlayerCards (tempCardList)
- judging their lack of design and color is finished without
For I=1, 9 do
If tempCardList [nQueSe] [I] & gt; 0 then return false end
End
The local cardData=https://bbs.csdn.net/topics/{}
Self. TwoCardAry={}
Self. ThreeCardAry={}
Self. FourCardAry={}
Self. CloneCardList={}
For I=1, 3 do
For j=1, 9 do
cardData=https://bbs.csdn.net/topics/{}
If tempCardList [I] [j]==2 then
CardData. Color=I
CardData. Point=j
Self. TwoCardAry [# self. TwoCardAry + 1]=cardData
Elseif tempCardList [I] [j]==3 then
CardData. Color=I
CardData. Point=j
Self. ThreeCardAry [# self. ThreeCardAry + 1]=cardData
Elseif tempCardList [I] [j]==4 then
CardData. Color=I
CardData. Point=j
Self. FourCardAry [# self. FourCardAry + 1]=cardData
End
End
End
If # self. TwoCardAry==7 then return true end - 7
# # if self. TwoCardAry==5 and the self. The fourCardAry==1 then return true end - dragon seven to
The local tempFour=0
The local tempThree=0
The local tempTwo=0
If next (self twoCardAry) ~=nil then tempTwo=# self. TwoCardAry end
If next (self threeCardAry) ~=nil then tempThree=# self. ThreeCardAry end
If next (self fourCardAry) ~=nil then tempFour=# self. FourCardAry end
- to hu CARDS must satisfy the AAA ABC AA must want to have a pair of the CARDS
The local isHuCard=false
For I=0, tempFour do
For j=0, tempThree do
For k=0, tempTwo do
Self. CloneCardList={}
For g=1, # tempCardList [4] do
Self. CloneCardList [g]=tempCardList [4] [g]
End
IsHuCard=self: CalcIsHuCard (I, j, k)
If isHuCard==true then return true end
End
End
End
Return false
End
The function Algorithm: CalcIsHuCard (nFourIndex nThreeIndex, nTwoIndex)
The local FourCardData=https://bbs.csdn.net/topics/nil
The local ThreeCardData=https://bbs.csdn.net/topics/nil
The local TwoCardData=https://bbs.csdn.net/topics/nil
If nFourIndex> 0 then FourCardData=https://bbs.csdn.net/topics/self.fourCardAry [nFourIndex] end
If nThreeIndex> 0 then ThreeCardData=https://bbs.csdn.net/topics/self.threeCardAry [nThreeIndex] end
If nTwoIndex> nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull