111111111111111111111111111

import random

# 读取成语库
def load_idioms(filename='idiom.txt'):
    with open(filename, 'r', encoding='utf-8') as f:
        idioms = [line.strip() for line in f if len(line.strip()) == 4]
    return idioms

idioms = load_idioms()

# 将成语按首字索引
idiom_dict = {}
for idiom in idioms:
    first = idiom[0]
    if first not in idiom_dict:
        idiom_dict[first] = []
    idiom_dict[first].append(idiom)

# 检查是否是合法成语
def is_valid_idiom(word):
    return word in idioms

# 机器随机接一个成语
def machine_reply(last_char, used):
    if last_char not in idiom_dict:
        return None
    options = [i for i in idiom_dict[last_char] if i not in used]
    return random.choice(options) if options else None

# 检查是否是"天"字音
def is_tian_pronunciation(ch):
    return ch == '天'

def idiom_game(level=1):
    print("欢迎进入成语接龙游戏!")
    score = 0
    used = set()
    attempts = 0
    max_attempts = 30
    machine_attempts = 3

    start = input("请输入一个成语或一个汉字开始:").strip()
    if is_tian_pronunciation(start[0]):
        print("第一个字不能是“天”字音,请重新输入")
        return

    # 人输入成语流程
    current_idiom = start if len(start) == 4 else None
    if current_idiom and not is_valid_idiom(current_idiom):
        print(f"{current_idiom} 不在成语库中,请重新开始")
        return

    # 主循环
    while True:
        if current_idiom:
            if not is_valid_idiom(current_idiom):
                print("不对啊,请重新输入。输入回车自动接龙,输入0结束")
                current_idiom = input("你来接龙:").strip()
                continue
            if current_idiom in used:
                print("这个成语已经用过了,请重新输入。")
                current_idiom = input("你来接龙:").strip()
                continue

            used.add(current_idiom)
            score += 1
            print("ok,请继续!")

        if level == 3:
            if current_idiom == "天天向上":
                print(f"🎉 祝贺你完成接龙!总分:{score}")
                return

            if attempts >= max_attempts:
                print(f"💡 接龙失败,加油!总分:{score}")
                return

        # 机器接龙
        last_char = current_idiom[-1] if current_idiom else start[0]
        reply = machine_reply(last_char, used)
        if reply:
            print(f"机器接:{reply}")
            used.add(reply)
            current_idiom = input("你来接龙:").strip()
            attempts += 1
            if current_idiom == "0":
                print(f"游戏结束,总得分:{score}")
                return
        else:
            if level == 3:
                machine_attempts -= 1
                if machine_attempts > 0:
                    print(f"🤖 机器没词了,再试一次(剩余{machine_attempts}次)")
                    current_idiom = input("你来接龙:").strip()
                    continue
                else:
                    print(f"🤖 机器接龙失败,游戏结束。总得分:{score}")
                    return
            else:
                print("机器接不上了,游戏结束")
                return

# 选择难度等级
def main():
    print("请选择难度等级:1(基础) 2(积分) 3(闯关)")
    level = input("输入难度等级(1/2/3):").strip()
    if level not in {'1', '2', '3'}:
        print("难度输入不正确")
        return
    idiom_game(level=int(level))

if __name__ == "__main__":
    main()

222222222

import random

# 读取成语库
def load_idioms(file):
    with open(file, 'r', encoding='utf-8') as f:
        idioms = [line.strip() for line in f if len(line.strip()) == 4]
    return idioms

# 根据首字筛选成语
def find_idioms_start_with(char, idioms):
    return [idiom for idiom in idioms if idiom.startswith(char)]

# 成语接龙主逻辑
def idiom_game(level=1):
    idioms = load_idioms("idiom.txt")
    used = set()
    score = 0
    max_hints = 3
    max_human_turns = 30
    goal = "天天向上"

    print("欢迎来到成语接龙游戏!")
    print("输入一个字开始接龙,输入回车让电脑接,输入0退出。")

    # 首字输入限制(难度3)
    first_input = input("请输入一个字: ").strip()
    if level == 3 and first_input.startswith("天"):
        print("开头不能是‘天’字音,请重新开始。")
        return

    options = find_idioms_start_with(first_input, idioms)
    if not options:
        print("没有以这个字开头的成语。游戏结束。")
        return

    current = random.choice(options)
    used.add(current)
    print(f"{current} ok,请继续!")

    human_turns = 1
    hints_used = 0

    while True:
        user_input = input("请输入接龙成语或操作指令: ").strip()
        
        # 退出指令
        if user_input == '0':
            print(f"游戏结束,总得分: {score} 分")
            break

        # 自动接龙
        if user_input == '':
            next_options = find_idioms_start_with(current[-1], idioms)
            next_options = [x for x in next_options if x not in used]
            if next_options:
                current = random.choice(next_options)
                used.add(current)
                print(f"电脑:{current} ok,请继续!")
            else:
                print("电脑找不到成语了,游戏结束。")
                print(f"总得分: {score}")
                break
            continue

        # 用户接龙验证
        if len(user_input) == 4 and user_input in idioms and user_input not in used:
            if user_input[0] == current[-1]:
                current = user_input
                used.add(current)
                score += 1
                human_turns += 1
                print("ok,请继续!")
                if level == 3 and current == goal:
                    print(f"🎉 成功接到目标成语【{goal}】!总得分: {score}")
                    break
                if level == 3 and human_turns >= max_human_turns:
                    print(f"😢 超过最大接龙次数。游戏失败。总得分: {score}")
                    break
            else:
                print("不对啊,请重新输入。输入回车自动接龙,输入0结束")
        else:
            print("不对啊,请重新输入。输入回车自动接龙,输入0结束")

        # 提示次数控制(仅限难度3)
        if level == 3 and hints_used >= max_hints:
            print("提示次数用完,自己接吧~")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值