麻将之胡牌算法篇

01 小七对算法实现



def num_to_card(num):
    """
    :param num: 代表麻将牌的整数
    :return: 麻将牌
    """
    wan = ['%d万' % i for i in range(1,10)]
    tong = ['%d筒' % i for i in range(1,10)]
    tao = ['%d条' % i for i in range(1,10)]
    fen = ['东风', '南风', '西风', '北风']
    zi = ['发财', '白板', '中']
    mj_dict = {
        1: wan[num % 16 - 1],
        2: tong[num % 16 - 1],
        3: tao[num % 16 - 1],
        4: fen[num % 16 % 3- 1],
        5: zi[num % 16 % 3 - 1],
    }
    if num//16 in [1, 2, 3, 4, 5]:
        return mj_dict.get(num//16)
    else:
        flag = 1
        return '癞子'


def lst_mj(lst):
    """
    :param lst: 一个包含14个整数的按照从小到大排列的有序列表
    :return: 满足小七对福牌输出福牌麻将牌列表,不满足输出0
    """
    flag = 0
    res = []
    index1 = 0
    r = len(lst)-1
    LZ = 10000
    while index1 <= r and not flag:
        if lst[index1] != lst[index1 + 1]:
            pack = num_to_card(lst[index1])
            res.append(pack)
            index1 = index1+1
            if lst[r] == LZ:
                r = r - 1
                pack = pack + '(癞子)'
                res.append(pack)
            else:
                flag = 1
        else:
            if lst[index1] != LZ:
                pack = num_to_card(lst[index1])
                res.append(pack)
                res.append(pack)
            else:
                pack = num_to_card(lst[index1])
                res.append(pack)
                res.append(pack)
                flag = 0
            index1 = index1 + 2
    if flag == 0:
        return res
    else:
        return 0


if __name__ == '__main__':
    text_samples = [
        # NO.1: False 4個對子, 4個單牌,2個癩子
        [0x11, 0x11, 0x21, 0x22, 0x22, 0x34, 0x34, 0x35, 0x35, 0x36, 0x37, 0x38, 10000, 10000],
        # NO.2: True 4個對子, 2個單牌,4個癩子
        [0x11, 0x11, 0x21, 0x22, 0x22, 0x34, 0x34, 0x35, 0x35, 0x36, 10000, 10000, 10000, 10000],
        # NO.3: True 14個癩子
        [10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000],
        # NO.4: False 4個對子,1個刻子,2個單牌,1個癩子
        [0x11, 0x11, 0x21, 0x21, 0x22, 0x22, 0x22, 0x34, 0x34, 0x35, 0x36, 0x44, 0x44, 10000],
        # NO.5: True 2個杠,1個刻子,1个对子,1個癩子
        [0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x22, 0x34, 0x34, 0x34, 0x47, 0x47, 10000],
        # NO.6: False 2个对子, 2个刻子, 2张单牌, 2个癞子
        [0x11, 0x11, 0x11, 0x22, 0x34, 0x34, 0x34, 0x35, 0x36, 0x36, 0x54, 0x54, 10000, 10000],
        # NO.7: False 9个单牌, 5个癞子
        [0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x10000, 10000, 10000, 10000, 10000]
    ]
    print("***测试结果如下,满足小七对输出福牌列表,不满足输出0***")
    for sample in text_samples:
        card_list = lst_mj(sample)
        print('NO.%d: ' % (text_samples.index(sample) + 1), card_list)

你可能感兴趣的:(firefly搭建麻将游戏后台)