Simple mahjong ai algorithm

Keywords: Python

Simple mahjong ai algorithm

Write a mahjong game, need to automatically play ai algorithm, do it yourself, although not very smart, but surprised by its simplicity, just record it. Implemented in python.

  1. First, summarize the rules of mahjong. The seemingly complex rules of mahjong can be expressed by a formula. Hu = AA and (AAA or AAAA or ABC)
    AA: Mahjong
    AAA: touch the card
    AAAA: it's the bar
    ABC: it's a streak
  2. The representation of the card. Use numbers to represent mahjong, such as 11-19 for 10000 to 90000, 31-39 for one to nine, 51-59 for one to nine. Why there are 10 numbers between the sliver and the bobbin is for the realization of the algorithm.
  3. Judge Hu. Use a recursive function to judge the Hu card. Card list should be arranged from small to large according to the number size of the card.
    # cardList: it's a card on hand. It needs to be arranged from small to large
    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 on your hand. Give all the cards in your hand a score first, and the card with the lowest score is the most useless card. How to calculate the score for each card?
    As shown in the figure below, the score calculation method of face x. Such as AAAA, the card with the highest score will not be knocked out; such as AC, the card with the lower score will increase the possibility of being knocked out.
    Not only to calculate the hand cards, but also to consider the cards that did not appear. If you have A card like AC in your hand, but there are more A and B cards in the cards that haven't appeared. So the probability of getting AAA or ABC card is increased, the score is higher, and the probability of being played is smaller. However, no matter how many cards have not appeared, they can't compare with the fact that there are ABC shaped cards in hand, so the score of the cards that haven't appeared is lower. For example, if you count 100 points for A card in your hand, you can only count 5 points for A card that doesn't appear.
    para = [10, 2, 1]
    scale = 50
    # Cards: cards in hand
    # dark: a card that never appeared
    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 the test, the ai algorithm implemented in a few lines of code is easy to get rid of. Four such ai matches, 95% can be Hu, 5% will be yellow, no ai Hu. You can adjust the values of para and scale to improve ai intelligence.

Posted by daneilair on Sat, 28 Mar 2020 10:11:27 -0700