import random
import tkinter as tk
from tkinter import ttk, messagebox

# 130+ 单词库（必须是这里面的才算正确）
word_list = [
    "apple", "ant", "angel", "animal", "air",
    "book", "bag", "ball", "bird", "bear", "boat", "baby", "back", "bed",
    "cat", "car", "cup", "cake", "chair", "clock", "cow", "city", "card", "cash",
    "dog", "duck", "door", "desk", "date", "dark", "dress", "dance", "draw",
    "egg", "ear", "eye", "elephant", "end", "easy", "email", "eggplant",
    "fish", "fan", "food", "foot", "face", "farm", "fire", "fly", "fruit",
    "goat", "game", "girl", "gun", "garden", "glass", "goose", "green", "grape",
    "hat", "hand", "hair", "horse", "home", "hill", "hot", "heart", "half",
    "ice", "ink", "island", "idea", "iron", "insect", "image",
    "juice", "jump", "job", "jacket", "jar", "joke", "join",
    "kite", "king", "kitchen", "key", "kid", "knife", "kick",
    "lion", "lamp", "leaf", "leg", "lake", "lock", "love", "lunch", "list",
    "milk", "map", "moon", "mouse", "mouth", "music", "monkey", "mother",
    "nose", "net", "name", "nurse", "neck", "note", "north",
    "orange", "open", "oven", "ocean", "oil", "office", "onion",
    "pig", "pen", "pencil", "plane", "plant", "park", "pink", "phone", "picture",
    "queen", "quilt", "quick", "question", "quail", "quarter",
    "rabbit", "rain", "rice", "red", "room", "road", "ring", "rock", "robot",
    "sun", "star", "school", "ship", "shoe", "snake", "snow", "soup", "soap", "swim",
    "tree", "tiger", "tent", "tea", "toe", "tooth", "train", "table", "tall", "taxi",
    "umbrella", "uncle", "under", "unit", "uniform", "ugly",
    "violin", "van", "vest", "village", "volcano", "voice",
    "water", "window", "wall", "wolf", "worm", "watch", "white", "wheel", "whale",
    "box", "fox", "yellow", "yoyo", "zoo", "zebra", "zero"
]

class WordChainGame:
    def __init__(self, root):
        self.root = root
        self.root.title("英语单词接龙游戏")
        self.root.geometry("600x450")
        self.root.resizable(False, False)

        self.used_words = []
        self.score = 0
        self.current_word = ""

        self.font_main = ("微软雅黑", 14)
        self.font_title = ("微软雅黑", 20, "bold")
        self.font_tip = ("微软雅黑", 12)

        tk.Label(root, text="英语单词接龙", font=self.font_title, fg="#1E90FF").pack(pady=15)
        self.score_label = tk.Label(root, text=f"当前分数：{self.score}", font=self.font_main, fg="#FF4500")
        self.score_label.pack(pady=5)

        self.word_label = tk.Label(root, text="点击【开始游戏】", font=("微软雅黑", 16, "bold"))
        self.word_label.pack(pady=15)

        input_frame = tk.Frame(root)
        input_frame.pack(pady=10)

        self.tip_label = tk.Label(input_frame, text="请输入单词：", font=self.font_main)
        self.tip_label.grid(row=0, column=0, padx=5)

        self.entry = ttk.Entry(input_frame, font=self.font_main, width=22)
        self.entry.grid(row=0, column=1, padx=8)
        self.entry.bind("<Return>", self.check_answer)
        self.entry.config(state=tk.DISABLED)

        btn_frame = tk.Frame(root)
        btn_frame.pack(pady=12)

        self.start_btn = ttk.Button(btn_frame, text="开始游戏", command=self.start_game, width=12)
        self.start_btn.grid(row=0, column=0, padx=15)

        self.submit_btn = ttk.Button(btn_frame, text="提交答案", command=self.check_answer, width=12, state=tk.DISABLED)
        self.submit_btn.grid(row=0, column=1, padx=15)

        self.msg_label = tk.Label(root, text="", font=self.font_tip, fg="red")
        self.msg_label.pack(pady=8)

        rule_label = tk.Label(root, text="规则：以上一单词最后一个字母开头，必须是正确单词", font=("微软雅黑", 10), fg="#666")
        rule_label.pack(pady=5)

        # 右下角姓名
        tk.Label(root, text="东台市实验小学  616班  徐王菲", font=("微软雅黑", 11), fg="#555").place(x=580, y=430, anchor="se")

    def start_game(self):
        self.used_words.clear()
        self.score = 0
        self.current_word = random.choice(word_list)
        self.used_words.append(self.current_word)

        self.score_label.config(text=f"当前分数：{self.score}")
        self.word_label.config(text=f"当前单词：{self.current_word}")
        last_char = self.current_word[-1]
        self.tip_label.config(text=f"请以【{last_char}】开头：")
        self.msg_label.config(text="游戏开始！", fg="green")

        self.entry.config(state=tk.NORMAL)
        self.submit_btn.config(state=tk.NORMAL)
        self.start_btn.config(state=tk.DISABLED)
        self.entry.delete(0, tk.END)
        self.entry.focus()

    def check_answer(self, event=None):
        user_word = self.entry.get().strip().lower()
        self.entry.delete(0, tk.END)

        if not user_word:
            self.msg_label.config(text="输入不能为空！", fg="red")
            return

        # 1. 必须是单词库里的词
        if user_word not in word_list:
            self.msg_label.config(text="不是正确单词！游戏结束", fg="red")
            self.game_over()
            return

        # 2. 不能重复
        if user_word in self.used_words:
            self.msg_label.config(text="单词已使用！游戏结束", fg="red")
            self.game_over()
            return

        # 3. 首字母必须匹配
        first_char = user_word[0]
        last_char = self.current_word[-1]

        if first_char != last_char:
            self.msg_label.config(text=f"需以【{last_char}】开头！游戏结束", fg="red")
            self.game_over()
            return

        # 全部正确
        self.score += 1
        self.used_words.append(user_word)
        self.current_word = user_word

        self.score_label.config(text=f"当前分数：{self.score}")
        self.word_label.config(text=f"当前单词：{self.current_word}")
        new_last = self.current_word[-1]
        self.tip_label.config(text=f"请以【{new_last}】开头：")
        self.msg_label.config(text="回答正确！", fg="green")

    def game_over(self):
        messagebox.showinfo("游戏结束", f"最终得分：{self.score}")
        self.start_btn.config(state=tk.NORMAL)
        self.entry.config(state=tk.DISABLED)
        self.submit_btn.config(state=tk.DISABLED)
        self.word_label.config(text="点击【开始游戏】")
        self.tip_label.config(text="请输入单词：")
        self.msg_label.config(text="")

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