Simple Mahjong AI algorithm

Simple Mahjong AI algorithm

To write a small game of mahjong, I need an AI algorithm to automatically play cards. I wrote one by myself. Although it is not very smart, I am surprised by its simplicity, so I record it. Implemented in python.

  1. Summarize the rules of Mahjong first. The seemingly complicated rules of Mahjong can be represented by a formula. Hu card = AA and (AAA or AAAA or ABC)
    AA: yes mahjong
    AAA: yes touch card
    AAAA: yes kong card
    ABC: yes consecutive card
  2. Representation of the card. Use numbers to represent mahjong tiles, such as 11-19 for 10,000 to 90,000, 31-39 for one to nine, and 51-59 for one to nine. The reason why there are 10 numbers between the sliver and the bobbin is for the realization of the algorithm.
  3. Judgment Hu. Use a recursive function to judge Hu cards. The card cardList is required to be arranged according to the number size of the card face, from small to large.
    # cardList: 是手上的牌,需要从小到大排列
    def match(cardList, majiang=False):
        if len(cardList) == 0 and majiang:
            return True

        rst = False
        # AA
        if not majiang and len(cardList) >= 2 and cardList[0] == cardList[1]:
            list1 = [] + cardList
            list1.pop(0)
            list1.pop(0)
            rst = match(list1, True)

        # AAA
        if not rst and len(cardList) >= 3 and cardList[0] == cardList[1] == cardList[2]:
            list1 = [] + cardList
            list1.pop(0)
            list1.pop(0)
            list1.pop(0)
            rst = match(list1, majiang)

        # AAAA
        if not rst and len(cardList) >= 4 and cardList[0] == cardList[1] == cardList[2] == cardList[3]:
            list1 = [] + cardList
            list1.pop(0)
            list1.pop(0)
            list1.pop(0)
            list1.pop(0)
            rst = match(list1, majiang)

        # ABC
        if not rst and len(cardList) >= 3:
            list1 = []
            a = cardList[0]
            b = False
            c = False
            for i in range(1, len(cardList)):
                if not b and cardList[i] == a + 1:
                    b = True
                elif not c and cardList[i] == a + 2:
                    c = True
                else:
                    list1.append(cardList[i])

            if(b and c):
                rst = match(list1, majiang)

        return rst
  1. Judge the card. To play is to play the most useless card in your hand. First, calculate a score for all the cards in your hand, and the card with the lowest score is the most useless card. How is the score calculated for each card?
    As shown in the figure below, the calculation method of the score of the card x. A hand such as AAAA has the highest score and will not be knocked out; a hand with a lower score, such as AC, has a lower score and is more likely to be knocked out.
    Enter image descriptionNot only are the cards in hand counted, but cards that haven't been shown are also taken into account. For example, there are cards such as AC in the hand, but there are more A and B cards in the cards that have not appeared. Then the probability of making AAA or ABC cards increases, the score is higher, and the probability of being played is smaller. However, no amount of unappeared cards can compare to the fact that there are already formed cards like ABC in the hand, so the value of the unappeared cards is lower. For example, the cards in hand count for 100 points, and the cards that do not appear can only count for 5 points.
    para = [10, 2, 1]
    scale = 50
    # cards:手上的牌
    # dark:未出现过的牌
    def cardsScore(cards, dark):
        scores = []
        
        for c in cards:
            score = 0
            for cc in cards:
                gap = abs(cc - c)
                if gap < 3:
                    score += para[gap] * scale
                    
            for cc in dark:
                gap = abs(cc - c)
                if gap < 3:
                    score += para[gap]
                    
            scores.append(score)
            
        return scores
  1. test. Through testing, the AI ​​algorithm implemented by a few lines of code is still very easy to win. In four such ai games, 95% of them can be handed, 5% of them will be yellow, and no ai will be handed. The values ​​of para and scale can be adjusted to improve the intelligence of ai.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325073921&siteId=291194637