Mahjong game data structure and AI Algorithms

Source: Internet
Author: User

Having completed the online mahjong game with zero breaks, I feel that there are many things worth recording.

Basic Data Structure

The determination of the data structure determines the difficulty of program development, just like the skeleton of the game. It plays a decisive role in setting the difficulty of computer AI and the gaming experience of players.

0. Mahjong ID Architecture

A one-dimensional array paicaption (143) is used to record the ID and text information of each card. The array content is a text description and the subscript is used as the ID (0-143, 144 cards in total)
1. Basic architecture of Hu card judgment

Use a one-dimensional array to record the quantity of each card type, for example
Int Pai [38] = {0,
, 0, // Pai [1-9 ~ Tens of thousands
0,
0, 0, 0, 0, 0, 0, 0, // Pai [11-19] ~ Copper quota
0,
0, 0, 0, 0, 0, 0, 0, // Pai [21-29 ~ Number of shards
0,
0, 1, 1, 0 // Pai [31-37] Number of white lines in the Southeast and Northwest China
};

2. Basic Architecture

Use a two-dimensional array to record, for example

Selfpai () Note: each person can have up to 18 cards

Parameter 1: the ID of each card, that is, the subscript in the paicaption Array

Parameter 2: the status of each card,-1 = "no card, 1 =" normal card, 2 = "Touch card, 3 =" Eat card, 4 = "dark bar, 5 = clear bar

3. Temporary data architecture

It is mainly used to record the temporary data that triggers the trigger, and record it with a dynamic array.

 

With these basic architectures, the status of all cards and players' hands is clear, making it much easier to develop computer AI and game processes later.

 

Main client Algorithms

0. Standard Hu Pai judgment Algorithm

Use Hu Pai to determine the basic architecture and perform recursive checks. A heap card is composed of an engraved card and a shunzi card. Each recursion reduces the number of cards that are engraved or shunzi by one. When the number is 0, the card is a heap card.

Public Function Hu (arrpai) as Boolean 'check standard Hu card 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
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> 30 then Hu = false: Exit Function
If (I mod 10 <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

The seven-to-one and thirteen-yao models do not rely on the Hu card model. Just write the algorithm separately.

Algorithms for eating, touching, and freezing cards can also be easily used to determine the basic architecture and the basic architecture of hand cards.

1. Game Process Control Algorithms

Several important variable variables are required: currentindex (current control card player ID), modemindex (current control card player ID), dynamic indicator index (current player ID), and hostindex (dealer ID) sharepai (the final card ID, corresponding to the subscript of the paicaption array), fapaiflag (Licensing logo), ibool (to sign), where the variable starting with AI is used for computer AI. The algorithm is implemented as follows:

If your pai =-1 and currentindex = mow.index then' touch your card

Call mow._click' call the card touch Module
Elseif else Pai>-1 and then adjust index <> modemindex and currentindex <> modemindex then 'are the cards played by others, and it is not their turn to control the cards.

Ibool = false
Call setoldhppai: saves the last hand card, including only regular cards
Call chkting 'check the playing card
If chk1_pai ("chkhu") = true then' check the Hu card

Ibool = true
Call delw.pai: put the cards played by others into your own hand Architecture

Aihupaiflag = true
End if
If qgangflag = false then' check the clear bar and touch the card
If chkw.pai ("chkminggang") = true then

Ibool = true
Call delw.pai
Aigangflag = true: aiminggangflag = true: aipengflag = true
Else
If chk1_pai ("chkpeng") = true then

Ibool = true
Call delw.pai
Aipengflag = true
End if
End if
'Check the card
If (please specify index + 1) mod playercount = mow.index then 'confirm that the game is successfully played by the previous store.
If chk1_pai ("chkchi") = true then
Ibool = true: Call delw.pai
Aichiflag = true
End if
End if
End if
Call addnousepai
Call setctrlflag (invalid partition index + 1) mod playercount)
'Check to card completion

If ibool = false then' does not have a card you need
'Send the message to the server and wait for the server's feedback.

Format: the serial number of the card issuer.
Call sendserverdata ("dovalue =" & CSTR (pull interval index) & "_" & CSTR (mopaiindex) & "_ 9999 ")
Else 'has a card
Call eventsound ("to sign", soundsex) 'displays the sound when you select the Hu, eat, and other buttons
Call callneedpaiai
End if

Elseif else Pai>-1 and then adjust index <> motesindex and currentindex = motesindex then 'others play the cards, and it is their turn to control the cards.
'Continue to complete Hu, bar, touch, eat, and other operations

(ImportantThe process is, for example, to send the "Hu" card information to the server, wait for the server to check whether other people have snatched Hu, if no one else grabs Hu, the Hu card process can be completed. If the server is strong enough, you can also put the entire process control on the server side for execution-a test is conducted, and each client occupies about 5-10 MB of memory, the CPU usage of 1000 online users is 5 GB to 10 Gb, And the CPU is overwhelmed by computer ai .)
If left (enddovalue, 1) = CSTR (mopaiindex) then' confirm that it is your desired card information
Call addmediapai 'Restore 14 sheets
Istr = mid (enddovalue, 3)
Enddovalue = ""
Select case CINT (istr)
Case huvalue 'hu
Call enddohupai
Case gangvalue' clear bar
Call enddominggang
Case pengvalue 'touch
Call enddopeng
Case chivalue'
Call enddochi
End select
End if
Elseif else Pai>-1 and then adjust Index = motesindex and currentindex = motesindex then 'it's your turn to control the card
If Len (enddovalue)> 0 then' gives control to players who need operations such as Hu, bar, touch, and eat
Currentindex = CINT (left (enddovalue, 1) '3 _ 3000
Call sendserverdata ("currentindex =" & CSTR (currentindex ))
Call sendserverdata ("enddovalue =" & enddovalue)

No one wants to play the else cards and cannot compete
Enddovalue = ""
If qgangflag = true then
Qgangflag = false
Call sendserverdata ("qgangflag = false ")'
Call enddominggang to complete your own clear bar
Else' transfer control to your next home
Running Pai =-1
Currentindex = (mow.index + 1) mod playercount
Call nextcontrolplayer
End if
End if
End if

 

Main server Algorithms

0. Computer Mahjong AI Algorithm

I have compiled this AI algorithm because it not only enables online games between players, but also allows computers to join. In case of three missing ones, huh, huh. I designed the AI difficulty of my computer to three levels. I mainly talked about the basic and simplest level 1 difficulty:

Card identification is relatively simple. It is similar to card identification on the client.

Playing strategy is the key and determines the difficulty of AI. I divided it into eight steps.

A. Check the audio card

B. Remove the disconsecutive cards with two vacant positions and troubleshoot from both ends

C. Remove the disconsecutive cards separated by one vacant space and troubleshoot from both ends

D. Remove a card that contains 4, 7, 10, and 13 consecutive cards to make the card model a non-fake card. For example, if there are 2344 entries, remove 4.

E. Remove a card that contains 3, 6, 9, and 12 consecutive cards. If yes, play one, eat two, and become the listening card (such as 233, remove three ); if there is no one, the success will become a fake model (such as 233, remove 2 ).

F. Remove a card from the consecutive card numbers 2, 5, 8, and 11, and make the card type an audible card. For example, remove 5 entries for 23445 entries.

G. play a card from the card.

These 8 steps are the basis of the standard Hu Pai AI, which is not involved in the seven-to-Peer special Hu Pai model. You can change the computer to a special Hu Pai AI when it is set to more than 4 or 5 pairs. When the AI difficulty level is 2, the card Recording analysis module is added to consider the cards that have been issued on the desktop. When the AI difficulty level is 3, the card recording process is recorded and analyzed.

1. Mahjong fan number Algorithm

There are 88 kinds of National Standard mahjong. You can refer to Chinese Mahjong Competition Rules or world Mahjong contest Mahjong rules. The process of computation is quite tiring. After all, there are 88 types of data, which can be exhausted directly -_-!

Mahjong has five basic principles:

1. non-repetition Principle

When a group card is determined by the group card conditions, when it is established, it will inevitably coexist with other types, and the other types will not be scored repeatedly.

2. Principle of not removing data

After determining a category, you cannot separate it to form a new category score.

3. principles not the same

If you have already combined a certain type of card, you cannot make up the same score with another deck.

4. high or low principles

If you have more than two cards, you may have more than two cards, but you can only choose one of them for time-based scoring.

5. One-time calculation principle

If there is a deck that has not been combined, you can only calculate it once with the corresponding deck that has been combined.

Be surePay attention to the last calculation principleOtherwise, the number of times is calculated too much.

Also, because different calculation processes may lead to different values, recursion is also required here until the maximum number of values can be calculated.

Conclusion: After the prototype of the game came out, it was good to have a try on it. There was no problem when four people were connected to the Internet, that is, when a computer was involved, the AI was still high, it seems that we have to write an "Ai reduction module" to make the computer "silly" randomly.

Original article by Liangshan thieves

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.