【Python】文字版三国杀小游戏

部署运行你感兴趣的模型镜像

前言

用Python自制的三国杀小游戏。

制作内容

1、基本牌:杀、闪、桃、酒
2、锦囊牌:过河拆桥、无懈可击、无中生有、乐不思蜀、兵粮寸断、桃园结义、顺手牵羊、火攻
3、三国杀回合制游戏流程
4、一个简易对战AI

补充及提示

1、游戏暂未制作花色设定,因此火攻弃牌改为弃相同牌
2、武器、武将等暂未制作
3、出现报错、Bug等请反馈

源码

"""文字版三国杀 v0.1"""

# 定义
import random
import time

game = {'回合': 1, '阶段': '抽牌', '回合次数': 1}
player = {'手牌': [], '状态': [], '体力': 4, '体力上限': 4}
ai = {'手牌': [], '状态': [], '体力': 4, '体力上限': 4}


# 函数


def list_num(list, vaule):  # 用于查找列表中某一元素数量的函数
    global card, player, game, ai
    num = 0
    for i in range(0, len(list) - 1):
        if list[i] == vaule:
            num += 1
    return num


def list_count():  # 专用计算牌堆及牌数的函数
    global card, player, game, ai
    num = 0
    c_list = {}
    end = 0
    for i in range(0, len(card)):
        num += card[i + 1]['数量']
        c_list[i + 1] = {'起始': end + 1, '结束': end + card[i + 1]['数量']}
        end = end + card[i + 1]['数量']
    return num, c_list


def list_read(list):  # 专用确认位置对应卡牌
    global card, player, game, ai
    num, c_list = list_count()
    re = []
    for j in range(0, len(list)):
        for i in range(1, len(c_list) + 1):
            if c_list[i]['结束'] >= list[j] >= c_list[i]['起始']:
                re.append(i)
                break
    return re


def sha(to, kill):  # 杀
    global card, player, game, ai
    if to == 1:  # AI使用
        ai['手牌'].remove(1)
        if player['手牌'].__contains__(2):
            choice = input('受到【杀】,是否使用【闪】(1.是 2.否):')
            if '1' in choice:
                player['手牌'].remove(2)
                print('成功使用【闪】,抵消【杀】')
            else:
                player['体力'] -= kill
                print('AI使用【杀】,您受到' + str(kill) + '点伤害,体力-' + str(kill))
        else:
            player['体力'] -= kill
            print('AI使用【杀】,您受到' + str(kill) + '点伤害,体力-' + str(kill))
    elif to == 2:
        player['手牌'].remove(1)
        if player['状态'].__contains__('使用【酒】'):
            player['状态'].remove('使用【酒】')
        if ai['手牌'].__contains__(2):
            if ai['体力'] <= 1 or kill >= 2:
                ai['手牌'].remove(2)
                print('AI使用【闪】,抵挡该【杀】')
            else:
                ai['体力'] -= kill
                print('【杀】成功,AI受到' + str(kill) + '点伤害')
        else:
            ai['体力'] -= kill
            print('【杀】成功,AI受到' + str(kill) + '点伤害')


def tao(to):  # 桃
    global card, player, game, ai
    if to == 1:
        if player['体力'] == player['体力上限']:
            print('体力已达上限,无法使用【桃】')
        else:
            player['手牌'].remove(3)
            player['体力'] += 1
            print('使用【桃】,恢复一点体力')
    elif to == 2:
        ai['手牌'].remove(3)
        ai['体力'] += 1
        print('AI使用【桃】,恢复一点体力')


def guo_he_chai_qiao(to):  # 过河拆桥
    global card, player, game, ai
    if to == 2:
        if len(ai['手牌']) > 0:
            player['手牌'].remove(4)
            if ai['手牌'].__contains__(5):
                # ai判断是否使用【无懈可击】
                k = 0
                for i in range(0, len(ai['手牌']) - 1):
                    if card[ai['手牌'][i]]['权重'] >= 3:
                        k += 1
                if k >= (int(len(ai['手牌']) / 2)):
                    ai['手牌'].remove(5)
                    print('AI使用【无懈可击】使【过河拆桥】无效')
                    return
            while True:
                chai = input('对方共有' + str(len(ai['手牌'])) + '张手牌,请输入要【过河拆桥】的牌序('
                                                                 '大于手牌视为总数为最后一张手牌)\n请输入:')
                if chai.isdigit():
                    chai = abs(int(chai))
                    if chai > len(ai['手牌']):
                        chai = len(ai['手牌'])
                    print('成功使用【过河拆桥】使对方弃置【' + str(card[ai['手牌'][chai - 1]]['名称']) + '】')
                    del ai['手牌'][chai - 1]
                    return
                else:
                    print('请输入牌序数字')
        else:
            print('AI没有手牌,无法使用【过河拆桥】')
            return
    elif to == 1:
        ai['手牌'].remove(4)
        if player['手牌'].__contains__(5):
            choice = input('AI对你使用【过河拆桥】,是否使用【无懈可击】(1.是 2.否):')
            if '1' in choice:
                player['手牌'].remove(5)
                print('成功使用【无懈可击】抵消【过河拆桥】')
                return
        put = random.randint(0, len(player['手牌']) - 1)
        print('AI【过河拆桥】生效,你的【' + str(card[player['手牌'][put]]['名称']) + '】被拆除')
        del player['手牌'][put]
        return


def wu_zhong_sheng_you(to):  # 无中生有
    global card, player, game, ai
    if to == 1:
        # ai不会对玩家的【无中生有】使用【无懈可击】
        player['手牌'].remove(6)
        get_card = [random.randint(1, len(card)), random.randint(1, len(card))]
        player['手牌'].extend(get_card)
        print('使用【无中生有】,摸2张牌,分别为【' + card[get_card[0]]['名称'] + '】和【' + card[get_card[1]]['名称'] + '】')
    elif to == 2:
        ai['手牌'].remove(6)
        if player['手牌'].__contains__(5):
            choice = input('AI使用【无中生有】,是否使用无懈可击抵消(1.是 2.否):')
            if '1' in choice:
                player['手牌'].remove(5)
                print('使用【无懈可击】抵消【无中生有】')
                return
        get_card = [random.randint(1, len(card)), random.randint(1, len(card))]
        ai['手牌'].extend(get_card)
        print('AI使用【无中生有】,摸2张牌')


def jiu(to):
    global card, player, game, ai
    if to == 1:
        player['手牌'].remove(7)
        player['状态'].append('使用【酒】')
        print('使用【酒】,下次使用【杀】伤害+1')
    elif to == 2:
        ai['手牌'].remove(7)
        ai['状态'].append('使用【酒】')
        print('AI使用【酒】,下次使用【杀】伤害+1')


def le_bu_si_shu(to):  # 乐不思蜀
    global card, player, game, ai
    if to == 1:
        ai['手牌'].remove(8)
        if player['手牌'].__contains__(5):
            choice = input('AI对你使用了【乐不思蜀】,是否使用【无懈可击】使【乐不思蜀】失效(1.是 2.否):')
            if '1' in choice:
                print('成功使用【无懈可击】使【乐不思蜀】失效')
                return
        print('受到【乐不思蜀】效果,轮到你的回合时将会判定:若结果不为[红桃],你将会跳过本回合出牌阶段')
        player['状态'].append('受到【乐不思蜀】')
    elif to == 2:
        player['手牌'].remove(8)
        if ai['手牌'].__contains__(5):
            ai['手牌'].remove(5)
            print('AI使用【无懈可击】抵消该【乐不思蜀】')
        else:
            ai['状态'].append('受到【乐不思蜀】')
            print('成功使用【乐不思蜀】,若判定结果不为[红桃],则AI跳过下回合出牌阶段')


def bin_si(who):  # 处理玩家濒死状态
    global card, player, game, ai
    if who == 1:
        if player['手牌'].__contains__(3) or player['手牌'].__contains__(7):
            choice = input('您已进入濒死状态,是否使用桃或酒(1.是 2.否):')
            if '1' in choice:
                player['体力'] += 1
                if player['手牌'].__contains__(3):
                    player['手牌'].remove(3)
                    print('使用【桃】,恢复一点血量,脱离濒死状态')
                elif player['手牌'].__contains__(7):
                    player['手牌'].remove(7)
                    print('使用【酒】,恢复一点血量,脱离濒死状态')
            else:
                print('游戏失败,您已死亡')
                exit()
        else:
            print('游戏失败,您已死亡')
            exit()
    elif who == 2:
        if ai['手牌'].__contains__(3) or ai['手牌'].__contains__(7):
            if ai['手牌'].__contains__(3):
                ai['手牌'].remove(3)
                ai['体力'] = 1
                print('AI使用【桃】,恢复一点血量,脱离濒死状态')
            elif ai['手牌'].__contains__(7):
                ai['手牌'].remove(7)
                ai['体力'] = 1
                print('AI使用【酒】,恢复一点血量,脱离濒死状态')
        elif player['手牌'].__contains__(3) or player['手牌'].__contains__(7):
            choice = input('AI已进入濒死状态,是否使用桃或酒帮助(1.是 2.否):')
            if '1' in choice:
                ai['体力'] = 1
                if player['手牌'].__contains__(3):
                    player['手牌'].remove(3)
                    print('使用【桃】,AI恢复一点血量,脱离濒死状态')
                elif player['手牌'].__contains__(7):
                    player['手牌'].remove(7)
                    print('使用【酒】,AI恢复一点血量,脱离濒死状态')
        else:
            print('游戏获胜,AI死亡')
            exit()


def bing_liang_cun_duan(to):
    global card, player, game, ai
    if to == 2:
        player['手牌'].remove(9)
        if ai['手牌'].__contains__(5) and len(ai['手牌']) <= 2:
            ai['手牌'].remove(5)
            print('AI使用【无懈可击】抵消该【兵粮寸断】')
            return
        else:
            ai['状态'].append('受到【兵粮寸断】')
            print('成功使用【兵粮寸断】,轮到AI回合时将判定,若不为[黑桃]则跳过AI摸牌阶段')
            return
    elif to == 1:
        ai['手牌'].remove(9)
        if player['手牌'].__contains__(5):
            choice = input('受到【兵粮寸断】,是否使用【无懈可击】抵消(1.是 2.否):')
            if '1' in choice:
                player['手牌'].remove(5)
                print('成功使用【无懈可击】抵消该【兵粮寸断】')
                return
        player['状态'].append('受到【兵粮寸断】')
        print('受到【兵粮寸断】,轮到您的回合时将判定,若结果不为[黑桃]则跳过您的摸牌阶段')


def tao_yuan_jie_yi(use):
    global card, player, game, ai
    if use == 1:
        player['手牌'].remove(10)
        if ai['手牌'].__contains__(5) and ai['体力'] == ai['体力上限']:  # 当对方只给自己回血时会出无懈
            ai['手牌'].remove(5)
            print('AI使用【无懈可击】抵消【桃园结义】')
            return
        else:
            if player['体力上限'] - player['体力'] >= 1:
                player['体力'] += 1
            elif ai['体力上限'] - ai['体力'] >= 1:
                ai['体力'] += 1
            print('使用【桃园结义】,AI与玩家各恢复1点体力,若达到上限则不恢复')
    elif use == 2:
        ai['手牌'].remove(10)
        if player['手牌'].__contains__(5):
            choice = input('AI使用【桃园结义】,是否使用【无懈可击】抵消(1.是 2.否):')
            if '1' in choice:
                player['手牌'].remove(5)
                print('成功使用【无懈可击】抵消【桃园结义】')
                return
        if player['体力上限'] - player['体力'] >= 1:
            player['体力'] += 1
        elif ai['体力上限'] - ai['体力'] >= 1:
            ai['体力'] += 1
        print('AI使用【桃园结义】,AI与玩家各恢复1点体力,若达到上限则不恢复')


def shun_shou_qian_yang(to):  # 顺手牵羊
    global card, player, game, ai
    if to == 2:
        if len(ai['手牌']) > 0:
            player['手牌'].remove(11)
            if ai['手牌'].__contains__(5):
                # ai判断是否使用【无懈可击】
                k = 0
                for i in range(0, len(ai['手牌']) - 1):
                    if card[ai['手牌'][i]]['权重'] >= 3:
                        k += 1
                if k >= (int(len(ai['手牌']) / 2)):
                    ai['手牌'].remove(5)
                    print('AI使用【无懈可击】使【顺手牵羊】无效')
                    return
            while True:
                chai = input('对方共有' + str(len(ai['手牌'])) + '张手牌,请输入要【顺手牵羊】的牌序('
                                                                 '大于手牌视为总数为最后一张手牌)\n请输入:')
                if chai.isdigit():
                    chai = abs(int(chai))
                    if chai > len(ai['手牌']):
                        chai = len(ai['手牌'])
                    print('成功使用【顺手牵羊】使获得对方【' + str(card[ai['手牌'][chai - 1]]['名称']) + '】')
                    player['手牌'].append(ai['手牌'][chai - 1])
                    del ai['手牌'][chai - 1]
                    return
                else:
                    print('请输入牌序数字')
        else:
            print('AI没有手牌,无法使用【顺手牵羊】')
            return
    elif to == 1:
        ai['手牌'].remove(11)
        if player['手牌'].__contains__(5):
            choice = input('AI对你使用【顺手牵羊】,是否使用【无懈可击】(1.是 2.否):')
            if '1' in choice:
                player['手牌'].remove(5)
                print('成功使用【无懈可击】抵消【过河拆桥】')
                return
        put = random.randint(0, len(player['手牌']) - 1)
        print('AI【顺手牵羊】生效,获得你的【' + str(card[player['手牌'][put]]['名称']) + '】')
        ai['手牌'].append(player['手牌'][put])
        del player['手牌'][put]
        return


def huo_gong(to):
    global card, player, game, ai
    if to == 1:
        ai['手牌'].remove(12)
        while True:
            if player['手牌'].__contains__(5):
                choice = input('AI对您使用【火攻】,是否使用【无懈可击】抵消(1.是 2.否):')
                if '1' in choice:
                    player['手牌'].remove(5)
                    print('成功使用【无懈可击】抵消该【火攻】')
                    return
            do = input('AI使用【火攻】,请展示一张手牌(输入序号):')
            if do.isdigit() and abs(int(do)) <= len(player['手牌']):
                do = abs(int(do))
                print('我方展示【' + card[player['手牌'][do - 1]]['名称'] + '】')
                if ai['手牌'].__contains__(player['手牌'][do - 1]):
                    ai['手牌'].remove(player['手牌'][do - 1])
                    player['体力'] -= 1
                    print('AI弃置一张【' + card[player['手牌'][do - 1]]['名称'] + '】,对您造成1点火焰伤害')
                    return
                else:
                    print('AI未弃置相同牌')
                    return
    elif to == 2:
        if len(ai['手牌']) > 0:
            player['手牌'].remove(12)
            show = random.randint(0, len(ai['手牌']) - 1)
            print('AI展示【' + card[ai['手牌'][show]]['名称'] + '】')
            if player['手牌'].__contains__(ai['手牌'][show]):
                choice = input('您拥有相同牌,是否弃置并对AI造成点火焰伤害(1.是 2.否):')
                if '1' in choice:
                    player['手牌'].remove(ai['手牌'][show])
                    ai['体力'] -= 1
                    print('我方弃置【' + card[ai['手牌'][show]]['名称'] + '】,对AI造成1点火焰伤害')
                    return
                else:
                    print('您未弃置相同牌')
                    return
            else:
                print('您没有相同牌')
                return
        else:
            print('AI没有手牌,无法使用【火攻】')
            return


# 数据表
card = {
    1: {'名称': '杀', '函数': sha, '权重': 1, '数量': 30, '总数': 30},
    2: {'名称': '闪', '函数': None, '权重': 2, '数量': 15, '总数': 15},
    3: {'名称': '桃', '函数': tao, '权重': 3, '数量': 8, '总数': 8},
    4: {'名称': '过河拆桥', '函数': guo_he_chai_qiao, '权重': 3, '数量': 16, '总数': 16},
    5: {'名称': '无懈可击', '函数': None, '权重': 5, '数量': 3, '总数': 3},
    6: {'名称': '无中生有', '函数': wu_zhong_sheng_you, '权重': 2, '数量': 4, '总数': 4},
    7: {'名称': '酒', '函数': jiu, '权重': 2, '数量': 5, '总数': 5},
    8: {'名称': '乐不思蜀', '函数': le_bu_si_shu, '权重': 5, '数量': 3, '总数': 3},
    9: {'名称': '兵粮寸断', '函数': bing_liang_cun_duan, '权重': 3, '数量': 5, '总数': 5},
    10: {'名称': '桃园结义', '函数': tao_yuan_jie_yi, '权重': 2, '数量': 2, '总数': 2},
    11: {'名称': '顺手牵羊', '函数': shun_shou_qian_yang, '权重': 3, '数量': 10, '总数': 10},
    12: {'名称': '火攻', '函数': huo_gong, '权重': 3, '数量': 8, '总数': 8},
}

# 流程
game['回合'] = random.randint(1, 2)
if game['回合'] == 1:
    print('我方先手')
else:
    print('AI先手')
time.sleep(1)
print('开局双方抽取4张手牌')
time.sleep(1)
num, c_list = list_count()
get_card_1 = [random.randint(1, num), random.randint(1, num), random.randint(1, num),
              random.randint(1, num)]
h1 = list_read(get_card_1)
get_card_2 = [random.randint(1, num), random.randint(1, num), random.randint(1, num),
              random.randint(1, num)]
h2 = list_read(get_card_2)
player['手牌'].extend(h1)
print('您抽取到的手牌分别为【' + card[h1[0]]['名称'] + '】【' + card[h1[1]]['名称'] + '】【' +
      card[h1[2]]['名称'] + '】【' + card[h1[3]]['名称'] + '】')
ai['手牌'].extend(h2)

# 游戏主循环
while True:
    # 濒死判定
    if player['体力'] == 0:
        bin_si(1)
    elif ai['体力'] == 0:
        bin_si(2)
    # 回合判定
    if game['回合'] == 1:
        game['回合次数'] += 1
        print('我方回合')
        time.sleep(1)
        game['阶段'] = '抽牌'
        if player['状态'].__contains__('受到【兵粮寸断】'):
            print('受到【兵粮寸断】,进行判定')
            time.sleep(1)
            pan = random.randint(1, 4)
            if pan == 1:
                print('判定结果为[黑桃],【兵粮寸断】不生效')
                player['状态'].remove('受到【兵粮寸断】')
            else:
                if pan == 2:
                    print('判定结果为[方块],', end='')
                elif pan == 3:
                    print('判定结果为[红桃],', end='')
                elif pan == 4:
                    print('判定结果为[梅花],', end='')
                player['状态'].remove('受到【兵粮寸断】')
                print('【兵粮寸断】生效')
                game['阶段'] = '出牌'
        if game['阶段'] == '抽牌':
            num, c_list = list_count()
            get_card = [random.randint(1, num), random.randint(1, num)]
            h = list_read(get_card)
            player['手牌'].extend(h)
            print('回合开始,抽取2张牌,分别为【' + card[h[0]]['名称'] + '】和【' + card[h[1]]['名称'] + '】')
            time.sleep(1)
        game['阶段'] = '出牌'
        if player['状态'].__contains__('受到【乐不思蜀】'):
            print('受到【乐不思蜀】,进行判定')
            time.sleep(1)
            pan = random.randint(1, 4)
            if pan == 1:
                print('判定结果为[红桃],【乐不思蜀】不生效')
                player['状态'].remove('受到【乐不思蜀】')
            else:
                if pan == 2:
                    print('判定结果为[方块],', end='')
                elif pan == 3:
                    print('判定结果为[黑桃],', end='')
                elif pan == 4:
                    print('判定结果为[梅花],', end='')
                print('【乐不思蜀】生效')
        if player['状态'].__contains__('受到【乐不思蜀】') is False:
            while game['阶段'] == '出牌' or len(player['手牌']) == 0:
                # 特殊设定
                k = []
                if player['状态'].__contains__('使用【酒】') or player['状态'].__contains__('使用【酒】,下回合结束'):
                    for i in range(0, len(player['状态'])):
                        if player['状态'][i] == '使用【酒】':
                            player['状态'][i] = '使用【酒】,下回合结束'
                        elif player['状态'][i] == '使用【酒】,下回合结束':
                            k.append(i)
                for i in k:
                    del player['状态'][i]
                # 出牌
                my_card = ''
                for i in range(0, len(player['手牌'])):
                    my_card += '【' + card[player['手牌'][i]]['名称'] + '】'
                print('——第' + str(game['回合次数'] // 2) + '轮——')
                print('AI体力:' + str(ai['体力']), 'AI手牌数:' + str(len(ai['手牌'])))
                print('体力:' + str(player['体力']), '手牌:' + str(my_card))
                doing = input('(输入牌序数字,输入0视为结束出牌阶段,超过手牌长度视为出最后一张手牌)\n请出牌:')
                if '0' in doing:
                    game['阶段'] = '弃牌'
                    break
                elif doing.isdigit():
                    doing = abs(int(doing))
                    if doing > len(player['手牌']):
                        doing = len(player['手牌'])
                    else:
                        z = card[player['手牌'][doing - 1]]['函数']
                        if z is None:
                            print('改手牌不可直接打出')
                        else:
                            p = player['手牌'][doing - 1]
                            if p == 1:
                                if player['状态'].__contains__('使用【酒】,下回合结束'):
                                    z(2, 2)
                                else:
                                    z(2, 1)
                                game['阶段'] = '弃牌'
                            elif p == 3:
                                z(1)
                            elif p == 4:
                                z(2)
                            elif p == 6:
                                z(1)
                            elif p == 7:
                                z(1)
                            elif p == 8:
                                z(2)
                            elif p == 9:
                                z(2)
                            elif p == 10:
                                z(1)
                            elif p == 11:
                                z(2)
                            elif p == 12:
                                z(2)
                else:
                    print('请输入牌序数字')
        else:
            player['状态'].remove('受到【乐不思蜀】')
            game['阶段'] = '弃牌'
        if game['阶段'] == '弃牌':
            put_list = []
            if len(player['手牌']) > player['体力上限']:
                put = len(player['手牌']) - player['体力上限']
                k = []
                while put > 0:
                    my_card = ''
                    for i in range(0, len(player['手牌'])):
                        my_card += '【' + card[player['手牌'][i]]['名称'] + '】'
                    print('AI体力:' + str(ai['体力']), 'AI手牌数:' + str(len(ai['手牌'])))
                    print('体力:' + str(player['体力']), '手牌:' + str(my_card))
                    qi = input('您需要弃' + str(put) + '张手牌,请输入要弃置的手牌序号(可通过“,”隔开序号以弃多张牌):')
                    if ',' in qi or ',' in qi:
                        sq = 0
                        for i in range(sq, len(qi)):
                            if qi[i] == ',' or qi[i] == ',':
                                if qi[sq: i].isdigit():
                                    num = abs(int(qi[sq: i]))
                                    if num > len(player['手牌']):
                                        print('请重新输入')
                                        break
                                    else:
                                        put_list.append(player['手牌'][num - 1])
                                        k.append(num - 1)
                                        put -= 1
                                        sq = i + 1
                                else:
                                    print('请重新输入')
                                    break
                        if qi[sq: len(qi)].isdigit():
                            num = abs(int(qi[sq: len(qi)]))
                            if num > len(player['手牌']):
                                print('请重新输入')
                                break
                            else:
                                put_list.append(player['手牌'][num - 1])
                                k.append(num - 1)
                                put -= 1
                        break
                    else:
                        if qi.isdigit():
                            num = abs(int(qi))
                            if num > len(player['手牌']):
                                print('请重新输入')
                                continue
                            else:
                                put_list.append(player['手牌'][num - 1])
                                del player['手牌'][num - 1]
                                put -= 1
                        else:
                            print('请重新输入')
                            continue
                if k is not []:
                    for i in k:
                        del player['手牌'][i]
            if not put_list:
                print('我方弃置:无')
            else:
                put_card = ''
                for i in range(0, len(put_list)):
                    put_card += '【' + card[put_list[i]]['名称'] + '】'
                print('我方弃置:' + str(put_card))
        print('我方回合结束,进入AI回合')
        game['回合'] = 2
        game['阶段'] = '抽牌'
    elif game['回合'] == 2:
        game['回合次数'] += 1
        print('AI回合')
        time.sleep(1)
        if ai['状态'].__contains__('受到【兵粮寸断】'):
            print('受到【兵粮寸断】,进行判定')
            time.sleep(1)
            pan = random.randint(1, 4)
            if pan == 1:
                print('判定结果为[黑桃],【兵粮寸断】不生效')
                ai['状态'].remove('受到【兵粮寸断】')
            else:
                if pan == 2:
                    print('判定结果为[方块],', end='')
                elif pan == 3:
                    print('判定结果为[红桃],', end='')
                elif pan == 4:
                    print('判定结果为[梅花],', end='')
                print('【兵粮寸断】生效')
                ai['状态'].remove('受到【兵粮寸断】')
                game['阶段'] = '出牌'
        if game['阶段'] == '抽牌':
            num, c_list = list_count()
            get_card = [random.randint(1, num), random.randint(1, num)]
            h = list_read(get_card)
            ai['手牌'].extend(h)
            print('AI抽取两张手牌')
        time.sleep(1)
        game['阶段'] = '出牌'
        if ai['状态'].__contains__('受到【乐不思蜀】'):
            print('AI受到【乐不思蜀】,进行判定')
            time.sleep(1)
            pan = random.randint(1, 4)
            if pan == 1:
                print('判定结果为[红桃],【乐不思蜀】不生效')
                ai['状态'].remove('受到【乐不思蜀】')
            else:
                if pan == 2:
                    print('判定结果为[方块],', end='')
                elif pan == 3:
                    print('判定结果为[黑桃],', end='')
                elif pan == 4:
                    print('判定结果为[梅花],', end='')
                print('【乐不思蜀】生效')
                game['阶段'] = '弃牌'
                ai['状态'].remove('受到【乐不思蜀】')
        else:
            pan = 1
            a = []
            for j in range(1, len(ai['手牌'])):
                # 酒效果
                k = []
                if ai['状态'].__contains__('使用【酒】') or ai['状态'].__contains__('使用【酒】,下回合结束'):
                    for i in range(0, len(ai['状态'])):
                        if ai['状态'][i] == '使用【酒】':
                            ai['状态'][i] = '使用【酒】,下回合结束'
                        elif ai['状态'][i] == '使用【酒】,下回合结束':
                            k.append(i)
                for i in k:
                    del ai['状态'][i]
                # 出牌
                for i in ai['手牌']:  # AI出牌,不包括杀(使用杀后回合结束)
                    c = i
                    if c == 3:
                        if list_num(ai['手牌'], 3) >= 2 and ai['体力'] < ai['体力上限']:  # 至少保留1张【桃】,濒死时使用
                            card[c]['函数'](2)
                            break
                    elif c == 4:
                        if len(player['手牌']) > 0:  # (由于双方可以看到对方手牌数,不算作弊),当对方有手牌时使用【过河拆桥】
                            card[c]['函数'](1)
                            break
                    elif c == 6:
                        if len(ai['手牌']) <= ai['体力上限'] - 2:  # 在手牌有空间时无中,防止摸了就弃
                            card[c]['函数'](2)
                            break
                    elif c == 7:
                        if list_num(ai['手牌'], 1) >= 1:  # 当有杀时,才喝酒
                            card[c]['函数'](2)
                            break
                    elif c == 8:
                        if player['状态'].__contains__('受到【乐不思蜀】') is False:
                            card[c]['函数'](1)
                            break
                    elif c == 9:
                        if player['状态'].__contains__('受到【兵粮寸断】') is False:
                            card[c]['函数'](1)
                            break
                    elif c == 10:
                        if ai['体力'] < ai['体力上限'] and player['体力'] == player['体力上限'] or ai['体力'] <= 2:
                            card[c]['函数'](2)   # 条件:只给自己回血或自己实在没血
                            break
                    elif c == 11:
                        if len(player['手牌']) > 0:
                            card[c]['函数'](1)
                            break
                    elif c == 12:
                        if len(player['手牌']) > 0:
                            card[c]['函数'](1)
                            break
            if game['阶段'] == '出牌':
                for i in range(0, len(ai['手牌'])):  # AI出【杀】
                    if ai['手牌'][i] == 1:
                        if ai['状态'].__contains__('使用【酒】,下回合结束'):
                            sha(1, 2)
                        else:
                            sha(1, 1)
                        game['阶段'] = '弃牌'
                        break
        game['阶段'] = '弃牌'
        if game['阶段'] == '弃牌':
            if len(ai['手牌']) > ai['体力上限']:  # AI弃牌,弃置权重最低的卡牌
                put = len(ai['手牌']) - ai['体力上限']
                put_list = []
                put_card = []
                put_card_d_1 = []
                put_card_d_2 = []
                for i in ai['手牌']:
                    put_card_d_1.append(card[i]['权重'])
                    put_card_d_2.append(i)
                    put_card.append(card[i]['权重'])
                put_card_l = sorted(put_card)
                for i in range(0, put):
                    for j in range(0, len(put_card_d_1)):
                        if put_card_d_1[j] == put_card_l[i]:
                            put_list.append(put_card_d_2[put_card_d_1[j]])
                            del put_card_d_2[put_card_d_1[j]]
                            del put_card_d_1[j]
                            break
                for i in put_list:
                    ai['手牌'].remove(i)
                put_card_s = ''
                for i in range(0, len(put_list)):
                    put_card_s += '【' + card[put_list[i]]['名称'] + '】'
                print('AI弃置:' + str(put_card_s))
            else:
                print('AI弃置:无')
        time.sleep(1)
        print('AI回合结束,进入我方回合')
        game['回合'] = 1
        game['阶段'] = '抽牌'

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值