Python成语接龙人人对战

这是一个可以实现人人对战的函数代码,可以在你写的成语接龙程序中引用这个函数

函数如下:

def idiom_pp(start=0, mode=0, opt=0):      #人人对战
    memory = set()                          # 记忆集合,用于判断成语是否被重复使用
    '''start = 0表示p1先手,start=1表示p2先手,返回值为0表示p1失败,为1表示p2失败'''
    while True:
        p1 = input("p1请输入成语:")
        if p1.strip() == '':
            print("游戏结束!p1输了")
            return 0
        if idiom_exists(p1) == False:
            print("游戏结束,该成语不存在")
            return 0
        p2 = input("p2请输入成语:")
        if p2.strip() == '':
            print("游戏结束!p2输了")
            return 1
        if idiom_exists(p2) == False:
            print("游戏结束,该成语不存在")
            return 1
        memory.add(p2)
        cycle_flag = 0  # 控制while True循环次数
        while True:
            p1 = idiom_select(p2, mode, opt)
            cycle_flag += 1
            if p1 not in memory:
                break
            if cycle_flag == 10:
                p1 = None
                break
            if p1 == None:
                print("恭喜p2,p2赢了!")
                return 1
            else:
                print(p1)
                memory.add(p1)
    while True:
        p1 = input("p1请输入成语:")
        if p1.strip() == '':
            print("游戏结束!p1输了")
        if idiom_exists(p1) == False:
            print("游戏结束,该成语不存在")
            return 0
        if p1 in memory:
            print("游戏结束!该成语已经使用过")
            return 0
        if idiom_test(p1, p2, mode, opt) == False:
            print("游戏结束,你未遵循游戏规则")
            return 0
        memeory.add(p2)
        cycle_flag = 0

    while True:
        p1 = idiom_select(p2, mode, opt)
        cycle_flag += 1
        if p1 not in memory:
            break
        if cycle_flag == 10:
            p1 = None
            break
    if p1 == None:
        print("恭喜p2,p2赢了!")
        return 1
    else:
        print(p1)
        memory.add(p1)

哪里不会的可以问我,可以一起探讨。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值