import tkinter as tk
from tkinter import messagebox

# 成语词库
idiom_list = [
    "一帆风顺", "二龙戏珠", "三心二意", "四面楚歌", "五光十色",
    "六神无主", "七上八下", "八面玲珑", "九牛一毛", "十全十美",
    "百发百中", "千军万马", "万事大吉", "海阔天空", "空穴来风",
    "风花雪月", "月下老人", "人山人海", "海枯石烂", "烂熟于心",
    "心花怒放", "放虎归山", "山清水秀", "秀外慧中", "中庸之道",
    "道听途说", "说长道短", "短兵相接", "接二连三", "三言两语",
    "语重心长", "长年累月", "月黑风高", "高谈阔论", "论功行赏",
    "赏罚分明", "明察秋毫", "毫发无损", "损人利己", "己所不欲",
    "欲擒故纵", "纵虎归山", "山穷水尽", "尽善尽美", "美中不足",
    "足智多谋", "谋事在人", "人定胜天", "天罗地网", "网开一面"
]

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

        # 游戏数据
        self.score = 0
        self.last_idiom = ""
        self.game_start = False

        # 界面布局
        # 标题
        title_label = tk.Label(root, text="成语接龙", font=("黑体", 24, "bold"), fg="#2E86AB")
        title_label.pack(pady=10)

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

        # 上一个成语提示
        self.last_label = tk.Label(root, text="游戏未开始，请点击【开始游戏】", font=("微软雅黑", 13, "bold"), fg="#D62828")
        self.last_label.pack(pady=8)

        # 输入框区域
        input_frame = tk.Frame(root)
        input_frame.pack(pady=12)
        tk.Label(input_frame, text="请输入成语：", font=("微软雅黑", 12)).grid(row=0, column=0, padx=5)
        self.input_box = tk.Entry(input_frame, font=("微软雅黑", 12), width=20)
        self.input_box.grid(row=0, column=1)
        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.start_game, width=10, bg="#43AA8B", fg="white").grid(row=0, column=0, padx=6)
        tk.Button(btn_frame, text="提交接龙", command=self.submit_idiom, width=10, bg="#577590", fg="white").grid(row=0, column=1, padx=6)
        tk.Button(btn_frame, text="重置游戏", command=self.reset_game, width=10, bg="#F3722C", fg="white").grid(row=0, column=2, padx=6)

        # 历史记录框
        tk.Label(root, text="接龙历史记录：", font=("微软雅黑", 11)).pack()
        self.history_text = tk.Text(root, width=55, height=8, font=("宋体", 10))
        self.history_text.pack(pady=5)
        self.history_text.config(state="disabled")

    # 开始游戏，随机给出第一个成语
    def start_game(self):
        import random
        self.last_idiom = random.choice(idiom_list)
        self.game_start = True
        self.last_label.config(text=f"上一个成语：【{self.last_idiom}】，尾字：{self.last_idiom[-1]}")
        self.write_history(f"游戏开始，首个成语：{self.last_idiom}")
        messagebox.showinfo("提示", f"第一个成语：{self.last_idiom}\n请以「{self.last_idiom[-1]}」开头接成语！")

    # 写入历史记录
    def write_history(self, text):
        self.history_text.config(state="normal")
        self.history_text.insert(tk.END, text + "\n")
        self.history_text.config(state="disabled")
        self.history_text.see(tk.END)

    # 提交成语判断逻辑
    def submit_idiom(self, event=None):
        if not self.game_start:
            messagebox.showwarning("警告", "请先点击【开始游戏】！")
            return
        user_input = self.input_box.get().strip()
        self.input_box.delete(0, tk.END)

        # 判断是否为空
        if len(user_input) == 0:
            messagebox.showinfo("提示", "请输入四字成语！")
            return
        # 判断必须四字
        if len(user_input) != 4:
            messagebox.showerror("错误", "必须输入四个字的成语！")
            return
        # 判断是否在词库内
        if user_input not in idiom_list:
            self.write_history(f"输入错误：{user_input}（不是有效成语）")
            messagebox.showerror("错误", f"{user_input} 不是收录的成语，重新输入！")
            return
        # 判断首尾字是否匹配
        last_char = self.last_idiom[-1]
        first_char = user_input[0]
        if last_char != first_char:
            self.write_history(f"接龙失败：{user_input}，需要以「{last_char}」开头")
            messagebox.showerror("接龙失败", f"上一个成语尾字是「{last_char}」\n你的成语首字是「{first_char}」，不匹配！")
            return

        # 接龙成功
        self.score += 10
        self.last_idiom = user_input
        self.score_label.config(text=f"当前得分：{self.score}")
        self.last_label.config(text=f"上一个成语：【{self.last_idiom}】，尾字：{self.last_idiom[-1]}")
        self.write_history(f"接龙成功 +10分：{user_input}")
        messagebox.showinfo("答对啦！", f"接龙成功！\n下一个需要以「{user_input[-1]}」开头")

    # 重置游戏
    def reset_game(self):
        self.score = 0
        self.last_idiom = ""
        self.game_start = False
        self.score_label.config(text=f"当前得分：{self.score}")
        self.last_label.config(text="游戏未开始，请点击【开始游戏】", fg="#D62828")
        self.history_text.config(state="normal")
        self.history_text.delete(1.0, tk.END)
        self.history_text.config(state="disabled")
        messagebox.showinfo("重置成功", "游戏已清空，重新开始！")

# 运行程序
if __name__ == "__main__":
    window = tk.Tk()
    game = IdiomGame(window)
    window.mainloop()