Code Building tutorial Mahjong AI algorithm and data structure chess card

Source: Internet
Author: User

Mahjong ID Architecture


Use one-dimensional array paicaption (143) to record each card ID and text information, the array content is a text description, subscript as an ID (0-143, a total of 144 cards)


1, the basic structure of the judgment of Hu Brand


Use a one-dimensional array to record the number of individual cards, such as

int pai[38] = {0,

1,1,1,0,1,1,1,0,0,//pai[1-9] The number of the million ~ Wan

0,

0,0,0,0,0,3,0,0,0,//pai[11-19] one copper ~ Nine copper number

0,

0,0,0,0,0,0,0,0,0,//pai[21-29] The number of one ~ Shan

0,

0,1,1,1,0,0,0//pai[31-37] The number of whitish in the cardinal

};


2, the basic structure of the hand card


Use a two-dimensional array to record, for example


Selfpai (17,1) Note: Up to 18 cards in each person's hand


Parameter 1: ID of each card, which is the subscript in the paicaption array


Parameter 2: The status of each card, 1 = "no card, 1 =" ordinary card, 2 = "hit card, 3 =" Eat card, 4 = "Dark bar, 5 =" Ming Bar


3, the temporary data structure to eat and touch the bar Hu


It is mainly used to record the temporary data triggered by eating and hitting the bar Hu, and it is not much verbose to record it with dynamic array.



With these basic architectures, all cards and player hand states are at a glance, for later development of the computer AI and game flow is much easier.

Standard HU brand Judgment algorithm


Use Hu to judge the basic structure, recursive check. A pair of Cheng Ho cards by the flush composition, each recursive when the number of the engraved or straight cards minus one, and the last 0 o'clock is the HU card type. (More detailed algorithm chess source Erection www.yasewl.com)


Public Function Hu (Arrpai) as Boolean ' check standard hu type

Dim I as Integer

If remain (Arrpai) = 0 Then HU = true:exit Function

For i = 0 to UBound (Arrpai)

If Arrpai (i) > 0 then Exit for

Next

If Arrpai (i) = 4 Then

Arrpai (i) = 0

If hu (Arrpai) = True then HU = True:exit Function

Arrpai (i) = 4

End If

If Arrpai (i) >= 3 Then

Arrpai (i) = Arrpai (i)-3

If hu (Arrpai) = True then HU = True:exit Function

Arrpai (i) = Arrpai (i) + 3

End If

If Jiang = 0 and Arrpai (i) >= 2 Then

Jiang = 1

Arrpai (i) = Arrpai (i)-2

If hu (Arrpai) = True then HU = True:exit Function

Arrpai (i) = Arrpai (i) + 2

Jiang = 0

End If

If i > then HU = False:exit Function

If (I Mod < 8) and (Arrpai (i + 1) > 0) and (Arrpai (i + 2) > 0) Then

Arrpai (i) = Arrpai (i)-1

Arrpai (i + 1) = Arrpai (i + 1)-1

Arrpai (i + 2) = Arrpai (i + 2)-1

If hu (Arrpai) = True then HU = True:exit Function

Arrpai (i) = Arrpai (i) + 1

Arrpai (i + 1) = Arrpai (i + 1) + 1

Arrpai (i + 2) = Arrpai (i + 2) + 1

End If

HU = False


End Function


Seven pairs, 13, all do not rely on the HU card type belongs to the special case, write the algorithm alone.


The algorithm of eating, touching and bar can be easily solved by judging the basic frame and the basic frame of the hand with the Hu brand.


This article is from the "daytime do not know Night Black" blog, please be sure to keep this source http://13134261.blog.51cto.com/13124261/1947573

Code Building tutorial Mahjong AI algorithm and data structure chess card

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.