示例#1
0
def test_minmax_engine():
    lorder_cards = "3 3 4 4 5 5 6 8"
    farmer_cards = "4 4 5 5 3 7"
    best_move = start_engine(lorder_cards=lorder_cards.split(),
                             farmer_cards=farmer_cards.split(),
                             farmer_move=[])
    print("Best Move is %s" % best_move)
示例#2
0
    def run(lorder_cards=None, farmer_cards=None, farmer_move=None):
        if farmer_move is None:
            farmer_move = list()
        if farmer_cards is None:
            farmer_cards = list()
        if lorder_cards is None:
            lorder_cards = list()
        UIEngine.declare()

        if not lorder_cards and not farmer_cards:
            print("请输入对手的牌(以空格间隔): ")
            farmer_cards = input().split()
            while not validate_cards(farmer_cards):
                print("对手的牌输入错误,请重新输入: ")
                farmer_cards = input().split()

            print("请输入自己的牌(以空格间隔): ")
            lorder_cards = input().split()
            while not validate_cards(lorder_cards):
                print("自己的牌输入错误,请重新输入: ")
                lorder_cards = input().split()

        lorder_cards = format_input_cards(lorder_cards)
        farmer_cards = format_input_cards(farmer_cards)

        print("初始状态: ")
        print("自己家的牌: %s" % format_output_cards(lorder_cards))
        print("对手家的牌: %s" % format_output_cards(farmer_cards))
        print("当前出牌者: %s" % "自己")
        print("-" * 20)

        # LandLorder do the first move
        lorder_move = start_engine(lorder_cards=lorder_cards,
                                   farmer_cards=farmer_cards,
                                   farmer_move=farmer_move)

        if lorder_move is None:
            print("自己必败!")
            return

        lorder_cards = get_rest_cards(lorder_cards, lorder_move)
        if len(lorder_cards) == 0:
            print("自己出牌: %s" % format_output_cards(lorder_move))
            print("自己胜利!")
            return

        # Farmer and LandLorder play one by one
        while True:
            # Print the Situation after Lorder play a move
            str_lorder_move = format_output_cards(
                lorder_move) if lorder_move else 'Pass!'
            print("自己家的牌: %s" % format_output_cards(lorder_cards))
            print("对手家的牌: %s" % format_output_cards(farmer_cards))
            print("自己已出牌: %s" % str_lorder_move)
            print("-" * 20)

            # Farmer plays a move
            print("请帮对手出牌:")
            farmer_move = input("")
            if (farmer_move in ['pass', 'Pass', 'PASS', '不要']) or \
                    len(farmer_move.strip()) == 0:
                farmer_move = []
            elif farmer_move == 'quit':
                exit(0)
            else:
                farmer_move = format_input_cards(farmer_move.split())

            possible_moves = get_resp_moves(farmer_cards, lorder_move)
            # must sort, for 'not in' check.
            possible_moves = [sorted(move) for move in possible_moves]
            while farmer_move not in possible_moves:
                print("错误的出牌!请重新帮对手出牌: ")
                farmer_move = input("")
                if farmer_move in ['pass', 'Pass', 'PASS']:
                    farmer_move = []
                else:
                    farmer_move = format_input_cards(farmer_move.split())
                possible_moves = get_resp_moves(farmer_cards, lorder_move)
                # must sort, for 'not in' check.
                possible_moves = [sorted(move) for move in possible_moves]

            farmer_cards = get_rest_cards(farmer_cards, farmer_move)
            if len(farmer_cards) == 0:
                print("对手出牌: %s" % format_output_cards(farmer_move))
                print("对手胜利!")
                return

            str_farmer_move = format_output_cards(
                farmer_move) if farmer_move else 'Pass!'
            print("自己家的牌: %s" % format_output_cards(lorder_cards))
            print("对手家的牌: %s" % format_output_cards(farmer_cards))
            print("对手已出牌: %s" % str_farmer_move)
            print("-" * 20)

            # LandLorder plays a move
            lorder_move = start_engine(lorder_cards=lorder_cards,
                                       farmer_cards=farmer_cards,
                                       farmer_move=farmer_move)

            if lorder_move is None:
                print("自己必败!")
                return

            lorder_cards = get_rest_cards(lorder_cards, lorder_move)
            if len(lorder_cards) == 0:
                print("自己出牌: %s" % format_output_cards(lorder_move))
                print("自己胜利!")
                return
示例#3
0
def ddz():
    #data = request.values.get('data', None)
    data = 3, 2, 0, 1, 0, 2, 1, 1, 3, 1, 2, 1, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0
    role = 0  #request.values.get('role', None)
    type = 0  #request.values.get('type', None)

    if not data:
        abort(400)

    data = list(data)
    data_arr = []
    for d in data:
        if d != ',' and d != ' ':
            data_arr.append(int(d))
    if len(data_arr) > 7 * 15:
        data_arr = data_arr[0:7 * 15]

    ddz = Ddz_Core.Doudizhu()

    ai_arrs = []
    for i in range(0, 7):
        ai_arrs.append(data_arr[i * 15:(i + 1) * 15])

    # 角色
    if role == None:
        role = 0
    else:
        role = int(role)
    # 张数数组 转为  牌面数组
    ddz.users[role] = Ddz_Core.num_list_to_cards(ai_arrs[0])

    #敌方剩余手牌数,取牌库剩余最大的几张牌当作敌方手牌
    rest_arr = []
    for i in range(15):
        rest_arr.append(ai_arrs[0][i] + ai_arrs[4][i] + ai_arrs[5][i] +
                        ai_arrs[6][i])  #根据自己手牌和历史总出牌计算牌库剩余牌
    if role == 0:
        len_up = 17 - len_cards(ai_arrs[5])
        len_down = 17 - len_cards(ai_arrs[6])
        if len_up >= len_down:
            len_enemy = len_down
        else:
            len_enemy = len_up
    if role == 1:
        len_enemy = 20 - len_cards(ai_arrs[5])
    if role == 2:
        len_enemy = 20 - len_cards(ai_arrs[6])

    mustHand = False
    # 上家或上上家有效的那一手
    lastHandPokers = Ddz_Core.num_list_to_cards(ai_arrs[2])
    if sum(lastHandPokers) == 0:
        lastHandPokers = Ddz_Core.num_list_to_cards(ai_arrs[3])
        if sum(lastHandPokers) == 0:  # 连续两把都是过,必须出牌
            mustHand = True

    # 牌型
    if type == None:  # 没给的话,根据上把出牌, 找最大的 hand
        lasthand = ddz.oneHandMaxType(lastHandPokers)
        type = lasthand['type']
    else:  # 给定,找出上把 hand
        type = Ddz_Log_Parser.cardTypeToCOMB_TYPE(type)
        lasthand = ddz.oneHand(lastHandPokers, type)

    allHands = ddz.allCanGoHandsByPokeAndLast(ddz.users[role], lasthand)
    if mustHand:
        #主动出牌策略加入残局破解,自己手牌小于13张且敌人手牌小于4张时触发
        my_cards = Ddz_Core.num_list_to_cards(ai_arrs[0])
        enemy_cards = enemy_card(rest_arr, len_enemy)
        if len(my_cards) < 13 and len_enemy < 4:
            lorder_move = start_engine(
                lorder_cards=[poker_mapping[str(x)] for x in my_cards],
                farmer_cards=[poker_mapping[str(x)] for x in enemy_cards],
                farmer_move=[])
            if lorder_move != None:
                return jsonify(lorder_move)
            else:
                allHands.remove(Ddz_Core.HAND_PASS)
        else:
            allHands.remove(Ddz_Core.HAND_PASS)
    scores = []
    for hand in allHands:
        ai_input = np.array(data_arr + ddz.handoutToAi(hand)).reshape(
            [8, 15, 1])
        pred = models[role].predict([ai_input])

        scal = len(hand['poker'])
        if scal == 0:
            scal = 1
        if not IS_CARDNUM_SCAL:
            scal = 1
        scores.append(pred[0][0] * scal)
        # scores.append(pred[0][0])

    idx = scores.index(max(scores))
    hand = (allHands[scores.index(max(scores))])
    hand.get('poker')