import tkinter as tk
from tkinter import messagebox
import random

# 成语词库
idiom_list = [
    "一帆风顺", "二龙戏珠", "三心二意", "四面八方", "五光十色",
    "六六大顺", "七上八下", "八面玲珑", "九牛一毛", "十全十美",
    "百发百中", "千方百计", "万紫千红", "红红火火", "火上浇油",
    "油然而生", "生龙活虎", "虎虎生威", "威风凛凛", "凛若冰霜",
    "霜气横秋", "秋高气爽", "爽然若失", "失魂落魄", "魄散魂飞",
    "飞蛾扑火", "火树银花", "花前月下", "下里巴人", "人定胜天",
    "天罗地网", "网开一面", "面如土色", "色胆包天", "天经地义",
    "义薄云天", "天长地久", "久别重逢", "逢凶化吉", "吉人天相"
]

class IdiomGame:
    def __init__(self, root):
        self.root = root
        self.root.title("成语接龙小游戏")
        self.root.geometry("520x380")
        self.root.resizable(False, False)

        # 游戏数据
        self.score = 0
        self.current_start_char = ""
        self.now_idiom = ""

        # 界面组件
        tk.Label(root, text="📖 成语接龙", font=("黑体", 20, "bold")).pack(pady=12)

        # 显示当前需要接的字
        self.show_label = tk.Label(root, text="游戏即将开始", font=("微软雅黑", 14))
        self.show_label.pack(pady=8)

        # 输入框
        tk.Label(root, text="请输入接龙成语：", font=("微软雅黑", 11)).pack()
        self.input_box = tk.Entry(root, font=("微软雅黑", 13), width=22)
        self.input_box.pack(pady=6)
        self.input_box.bind("<Return>", self.submit_idiom)  # 回车直接提交

        # 按钮容器
        btn_frame = tk.Frame(root)
        btn_frame.pack(pady=10)

        tk.Button(btn_frame, text="确认提交", command=self.submit_idiom,
                  font=("微软雅黑", 11), bg="#4298f5", fg="white", width=8).grid(row=0, column=0, padx=6)
        tk.Button(btn_frame, text="下一题", command=self.next_round,
                  font=("微软雅黑", 11), bg="#34c759", fg="white", width=8).grid(row=0, column=1, padx=6)
        tk.Button(btn_frame, text="重新开局", command=self.restart_game,
                  font=("微软雅黑", 11), bg="#ff9500", fg="white", width=8).grid(row=0, column=2, padx=6)

        # 分数显示
        self.score_label = tk.Label(root, text=f"当前得分：{self.score}", font=("微软雅黑", 12))
        self.score_label.pack(pady=8)

        # 游戏说明
        tip = """游戏规则：
根据上一个成语最后一个字，输入首字相同的成语即可得分
同音不同字不可以哦！"""
        tk.Label(root, text=tip, font=("微软雅黑", 9), fg="#555").pack(pady=5)

        # 开局第一题
        self.next_round()

    def next_round(self):
        """随机生成下一个成语，提取最后一字作为接龙开头"""
        self.now_idiom = random.choice(idiom_list)
        self.current_start_char = self.now_idiom[-1]
        self.show_label.config(text=f"上一个成语：【{self.now_idiom}】\n请接以「{self.current_start_char}」开头的成语")
        self.input_box.delete(0, tk.END)

    def submit_idiom(self, event=None):
        user_input = self.input_box.get().strip()
        if not user_input:
            messagebox.showwarning("提示", "请输入成语！")
            return

        # 判断条件：在词库内 并且 首字匹配
        if user_input in idiom_list and user_input[0] == self.current_start_char:
            self.score += 10
            self.score_label.config(text=f"当前得分：{self.score}")
            messagebox.showinfo("答对啦", "+10分！进入下一轮")
            self.now_idiom = user_input
            self.current_start_char = self.now_idiom[-1]
            self.show_label.config(text=f"上一个成语：【{self.now_idiom}】\n请接以「{self.current_start_char}」开头的成语")
            self.input_box.delete(0, tk.END)
        else:
            messagebox.showerror("错误", "成语不存在或首字不匹配，再试试吧！")

    def restart_game(self):
        """重置游戏"""
        self.score = 0
        self.score_label.config(text=f"当前得分：{self.score}")
        self.next_round()

if __name__ == "__main__":
    main_window = tk.Tk()
    game = IdiomGame(main_window)
    main_window.mainloop()