import tkinter as tk
from tkinter import messagebox

# 简单成语词典（可以替换成更大的词库）
IDIOMS = set([
    "一见钟情", "情同手足", "足智多谋", "谋事在人", "人山人海",
    "海阔天空", "空前绝后", "后来居上", "上行下效", "笑逐颜开",
    "开门见山", "山清水秀", "秀外慧中", "中流砥柱", "众所周知",
    "知难而进", "进退维谷", "谷马砺兵", "兵荒马乱", "乱七八糟",
    "画龙点睛", "井底之蛙", "亡羊补牢", "守株待兔", "狐假虎威"
])

class IdiomChainGame:
    def __init__(self, root):
        self.root = root
        self.root.title("成语接龙小游戏")
        self.root.geometry("400x300")

        self.current_idiom = "一见钟情"  # 初始成语
        self.used_idioms = {self.current_idiom}

        # 标签：显示当前成语
        self.label_current = tk.Label(root, text=f"当前成语：{self.current_idiom}", font=("Arial", 14))
        self.label_current.pack(pady=20)

        # 输入框
        self.entry = tk.Entry(root, font=("Arial", 14), width=20)
        self.entry.pack(pady=10)

        # 提交按钮
        self.button_submit = tk.Button(root, text="接龙！", command=self.submit_idiom, font=("Arial", 12))
        self.button_submit.pack(pady=10)

        # 历史记录（可选）
        self.history_label = tk.Label(root, text="接龙历史：", font=("Arial", 10))
        self.history_label.pack(anchor="w", padx=20)
        self.history_text = tk.Text(root, height=6, state="disabled", font=("Arial", 10))
        self.history_text.pack(padx=20, pady=10, fill="both")

        self.update_history()

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

        if len(user_input) != 4:
            messagebox.showerror("错误", "成语必须是四个字！")
            return

        if user_input in self.used_idioms:
            messagebox.showerror("错误", "这个成语已经用过了！")
            return

        if user_input not in IDIOMS:
            messagebox.showerror("错误", "这不是一个有效的成语！")
            return

        last_char = self.current_idiom[-1]
        first_char = user_input[0]

        if last_char != first_char:
            messagebox.showerror("错误", f"必须用「{last_char}」开头！")
            return

        # 接龙成功
        self.used_idioms.add(user_input)
        self.current_idiom = user_input
        self.label_current.config(text=f"当前成语：{self.current_idiom}")
        self.entry.delete(0, tk.END)
        self.update_history()

    def update_history(self):
        self.history_text.config(state="normal")
        self.history_text.delete(1.0, tk.END)
        history_str = " → ".join(self.used_idioms)
        self.history_text.insert(tk.END, history_str)
        self.history_text.config(state="disabled")


if __name__ == "__main__":
    root = tk.Tk()
    app = IdiomChainGame(root)
    root.mainloop()