import tkinter as tk
from tkinter import messagebox

# 成语词典：key=当前成语，value=要接的成语
idiom_map = {
    "一帆风顺": "顺理成章",
    "顺理成章": "章台杨柳",
    "章台杨柳": "柳暗花明",
    "柳暗花明": "明察秋毫",
    "明察秋毫": "毫发无损",
    "毫发无损": "损人利己",
    "损人利己": "己所不欲",
    "己所不欲": "欲擒故纵",
    "欲擒故纵": "纵虎归山",
    "纵虎归山": "山清水秀",
    "山清水秀": "秀外慧中",
    "秀外慧中": "中流砥柱",
    "中流砥柱": "柱石之坚",
    "柱石之坚": "坚不可摧",
    "坚不可摧": "摧枯拉朽",
    "摧枯拉朽": "朽木难雕",
    "朽木难雕": "雕虫小技",
    "雕虫小技": "技高一筹",
    "技高一筹": "筹谋已久",
    "筹谋已久": "久别重逢",
    "久别重逢": "逢凶化吉",
    "逢凶化吉": "吉人天相",
    "吉人天相": "相由心生",
}

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

        self.current_idiom = ""
        self.reset_game()

        # 标题
        tk.Label(root, text="🎮 成语接龙", font=("SimHei", 20)).pack(pady=8)

        # 提示标签
        self.tip_label = tk.Label(root, text="", font=("SimHei", 14), fg="#222222")
        self.tip_label.pack(pady=4)

        # 输入区
        tk.Label(root, text="输入你的成语：", font=("SimHei", 11)).pack()
        self.input_box = tk.Entry(root, font=("SimHei", 14), width=22)
        self.input_box.pack(pady=4)
        self.input_box.bind("<Return>", self.submit)  # 回车提交

        # 按钮行
        frame_btn = tk.Frame(root)
        frame_btn.pack(pady=8)
        tk.Button(frame_btn, text="提交", command=self.submit, width=8, font=("SimHei",11)).grid(row=0, column=0, padx=6)
        tk.Button(frame_btn, text="重新开始", command=self.reset_game, width=8, font=("SimHei",11)).grid(row=0, column=1, padx=6)

        # 日志框
        tk.Label(root, text="游戏记录：", font=("SimHei",10)).pack()
        self.log = tk.Text(root, height=7, width=55, font=("SimHei", 10))
        self.log.pack()

    def reset_game(self):
        self.current_idiom = "一帆风顺"
        if hasattr(self, "tip_label"):
            self.tip_label.config(text=f"上一个成语：【{self.current_idiom}】")
            self.log.delete(1.0, tk.END)
            self.log.insert(tk.END, f"✅ 游戏启动！起始成语：{self.current_idiom}\n")

    def submit(self, event=None):
        text = self.input_box.get().strip()
        self.input_box.delete(0, tk.END)

        if not text:
            messagebox.showwarning("提醒", "请输入成语！")
            return

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

        # 判断接龙是否合法
        if first_char != last_char:
            self.log.insert(tk.END, f"❌ {text} 接龙失败！需要以【{last_char}】开头\n")
            self.log.see(tk.END)
            return

        # 判断词库有没有后续成语给AI接
        if text not in idiom_map:
            self.log.insert(tk.END, f"🎉 你出【{text}】，词库没有可接成语，本局胜利！\n")
            self.log.see(tk.END)
            messagebox.showinfo("游戏结束", "恭喜！AI接不上来了！点重新开始再来一局")
            return

        ai_next = idiom_map[text]
        self.log.insert(tk.END, f"✅ 你：{text} → AI：{ai_next}\n")
        self.log.see(tk.END)
        self.current_idiom = ai_next
        self.tip_label.config(text=f"上一个成语：【{self.current_idiom}】")

if __name__ == "__main__":
    window = tk.Tk()
    game = IdiomLinkGame(window)
    window.mainloop()
