import tkinter as tk
from tkinter import messagebox
import random

# 成语库，可以自行扩充
IDIOMS = [
    "一心一意", "意气风发", "发扬光大", "大海捞针", "针锋相对",
    "对牛弹琴", "琴棋书画", "画蛇添足", "足智多谋", "谋事在人",
    "人山人海", "海阔天空", "空穴来风", "风调雨顺", "顺理成章",
    "章台杨柳", "柳暗花明", "明察秋毫", "毫不在意", "意在笔先",
    "先声夺人", "人定胜天", "天罗地网", "网开一面", "面如土色",
    "色胆包天", "天经地义", "义薄云天", "天长地久", "久别重逢",
    "逢凶化吉", "吉人天相", "相敬如宾", "宾至如归", "归心似箭"
]

class IdiomGame:
    def __init__(self, root):
        self.root = root
        self.root.title("成语接龙")
        self.root.geometry("520x420")

        self.score = 0
        self.last_char = ""
        self.history = []

        # 标题
        tk.Label(root, text="🎮 成语接龙游戏", font=("微软雅黑", 18)).pack(pady=10)

        # 提示标签
        self.info_label = tk.Label(root, text="游戏开始！请输入第一个成语", font=("微软雅黑",12))
        self.info_label.pack(pady=5)

        # 上一个成语显示
        self.last_idiom_label = tk.Label(root, text="", font=("微软雅黑",13,"bold"), fg="#d02020")
        self.last_idiom_label.pack(pady=5)

        # 输入框
        self.entry = tk.Entry(root, font=("微软雅黑",14), width=22)
        self.entry.pack(pady=8)
        self.entry.bind("<Return>", self.player_input)

        # 按钮区域
        frame_btn = tk.Frame(root)
        frame_btn.pack(pady=5)
        tk.Button(frame_btn, text="提交", command=self.player_input, font=("微软雅黑",11), width=8).grid(row=0,column=0,padx=5)
        tk.Button(frame_btn, text="重新开始", command=self.reset_game, font=("微软雅黑",11), width=8).grid(row=0,column=1,padx=5)

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

        # 历史记录文本框
        tk.Label(root, text="接龙记录：", font=("微软雅黑",11)).pack()
        self.history_text = tk.Text(root, height=8, width=55, font=("微软雅黑",10))
        self.history_text.pack(pady=3)

    def player_input(self, event=None):
        user_idiom = self.entry.get().strip()
        self.entry.delete(0, tk.END)

        if len(user_idiom) != 4:
            messagebox.showwarning("提示", "请输入4字成语！")
            return

        if user_idiom not in IDIOMS:
            messagebox.showwarning("提示", f"【{user_idiom}】不在成语库中！")
            return

        # 判断接龙规则
        if self.last_char != "":
            if user_idiom[0] != self.last_char:
                messagebox.showinfo("接不上", f"需要以【{self.last_char}】开头！")
                return

        # 用户成功
        self.history.append(f"你：{user_idiom}")
        self.score += 1
        self.last_char = user_idiom[-1]

        # 电脑找成语
        computer_idiom = self.find_idiom(self.last_char)
        if computer_idiom is None:
            self.info_label.config(text="🎉恭喜！电脑接不上，你赢了！")
            self.refresh_ui()
            return

        self.history.append(f"电脑：{computer_idiom}")
        self.last_char = computer_idiom[-1]

        self.refresh_ui()

    def find_idiom(self, start_char):
        candidates = [i for i in IDIOMS if i[0]==start_char and i not in self.history]
        if not candidates:
            return None
        return random.choice(candidates)

    def refresh_ui(self):
        self.score_label.config(text=f"得分：{self.score}")
        self.last_idiom_label.config(text=f"下一个成语需要以【{self.last_char}】开头")
        self.history_text.delete(1.0, tk.END)
        self.history_text.insert(tk.END, "\n".join(self.history))

    def reset_game(self):
        self.score = 0
        self.last_char = ""
        self.history.clear()
        self.info_label.config(text="游戏开始！请输入第一个成语")
        self.last_idiom_label.config(text="")
        self.score_label.config(text=f"得分：{self.score}")
        self.history_text.delete(1.0, tk.END)
        self.entry.delete(0, tk.END)


if __name__ == "__main__":
    win = tk.Tk()
    app = IdiomGame(win)
    win.mainloop()
