import random
import tkinter as tk
from tkinter import ttk, messagebox
import time

class UltimateGuessGame:
    def __init__(self, root):
        self.root = root
        self.root.title("🔥 终极猜数字 · 双人对战版 🔥")
        self.root.geometry("650x500")
        self.root.resizable(False, False)
        self.root.configure(bg="#1a1a1a")

        # 游戏数据
        self.secret = 0
        self.p1_count = 0
        self.p2_count = 0
        self.turn = 1
        self.rounds = 1
        self.current_round = 1
        self.score1 = 0
        self.score2 = 0
        self.max_try = 6
        self.min_num = 1
        self.max_num = 500

        self.setup_ui()

    def setup_ui(self):
        # 标题
        title = tk.Label(self.root, text="终极猜数字游戏 · 双人对战",
                         font=("微软雅黑",22,"bold"), bg="#1a1a1a", fg="#ffcc00")
        title.pack(pady=15)

        # 难度
        fr_diff = tk.Frame(self.root, bg="#1a1a1a")
        fr_diff.pack()
        tk.Label(fr_diff, text="难度：", font=("微软雅黑",12), bg="#1a1a1a", fg="white").grid(row=0,column=0)
        self.diff = ttk.Combobox(fr_diff, values=["简单(1-50,15次)","普通(1-100,10次)","困难(1-200,8次)","地狱(1-500,6次)","神话(1-1000,4次)"], width=22, state="readonly")
        self.diff.current(3)
        self.diff.grid(row=0,column=1,padx=5)

        # 轮数
        tk.Label(fr_diff, text="轮数：", font=("12"), bg="#1a1a1a", fg="white").grid(row=0,column=2,padx=5)
        self.rounds_var = tk.IntVar(value=3)
        tk.Spinbox(fr_diff, from_=1, to=5, textvariable=self.rounds_var, width=5).grid(row=0,column=3)

        # 开始按钮
        self.start_btn = tk.Button(self.root, text="🎮 开始游戏", font=("微软雅黑",14,"bold"),
                                   bg="#ff3333", fg="white", command=self.start_game)
        self.start_btn.pack(pady=8)

        # 显示区域
        self.info = tk.Label(self.root, text="请选择难度并开始游戏", font=("微软雅黑",13),
                             bg="#1a1a1a", fg="#00ffcc")
        self.info.pack(pady=5)

        # 输入框
        self.entry = tk.Entry(self.root, font=("微软雅黑",16), width=12, justify="center")
        self.entry.pack(pady=8)
        self.entry.bind("<Return>", self.submit)
        self.entry.config(state=tk.DISABLED)

        # 按钮
        self.btn = tk.Button(self.root, text="确认猜数字", font=("微软雅黑",13,"bold"),
                             bg="#3366ff", fg="white", command=self.submit, state=tk.DISABLED)
        self.btn.pack(pady=2)

        # 日志
        self.log_label = tk.Label(self.root, text="📜 游戏记录", font=("微软雅黑",11), bg="#1a1a1a", fg="white")
        self.log_label.pack(pady=8)
        self.log = tk.Text(self.root, height=10, width=70, bg="#222", fg="white", font=("Consolas",10))
        self.log.pack()
        self.log.config(state=tk.DISABLED)

    def log_print(self, text):
        self.log.config(state=tk.NORMAL)
        self.log.insert("end", text+"\n")
        self.log.see("end")
        self.log.config(state=tk.DISABLED)

    def set_difficulty(self):
        d = self.diff.current()
        if d == 0:
            self.min_num, self.max_num, self.max_try = 1,50,15
        elif d == 1:
            self.min_num, self.max_num, self.max_try = 1,100,10
        elif d == 2:
            self.min_num, self.max_num, self.max_try = 1,200,8
        elif d == 3:
            self.min_num, self.max_num, self.max_try = 1,500,6
        else:
            self.min_num, self.max_num, self.max_try = 1,1000,4

    def start_game(self):
        self.set_difficulty()
        self.rounds = self.rounds_var.get()
        self.current_round = 1
        self.score1 = 0
        self.score2 = 0
        self.log.delete("1.0", tk.END)
        self.log_print("🚀 游戏开始！")
        self.start_round()

    def start_round(self):
        self.secret = random.randint(self.min_num, self.max_num)
        self.p1_count = 0
        self.p2_count = 0
        self.turn = 1
        self.info.config(text=f"第 {self.current_round}/{self.rounds} 轮 → 玩家1回合")
        self.entry.config(state=tk.NORMAL)
        self.btn.config(state=tk.NORMAL)
        self.entry.delete(0, tk.END)
        self.entry.focus()
        self.log_print(f"\n🎯 第{self.current_round}轮开始！数字范围 {self.min_num}~{self.max_num}")

    def submit(self, event=None):
        s = self.entry.get().strip()
        self.entry.delete(0, tk.END)
        try:
            num = int(s)
        except:
            messagebox.showwarning("错误","请输入有效数字！")
            return

        if self.turn == 1:
            self.p1_count +=1
            c = self.p1_count
            player = "玩家1"
        else:
            self.p2_count +=1
            c = self.p2_count
            player = "玩家2"

        if num > self.secret:
            res = "🔺 大了"
        elif num < self.secret:
            res = "🔻 小了"
        else:
            res = "✅ 猜对！"

        self.log_print(f"{player} 第{c}次：{num} → {res}")

        if num == self.secret:
            if self.turn ==1:
                self.turn =2
                self.info.config(text=f"玩家1猜对！{self.p1_count}次 → 轮到玩家2")
            else:
                self.judge_round()
                return
        elif (self.turn==1 and self.p1_count>=self.max_try):
            self.log_print("💀 玩家1次数用尽！")
            self.turn=2
            self.info.config(text="玩家1失败 → 玩家2回合")
        elif (self.turn==2 and self.p2_count>=self.max_try):
            self.log_print("💀 玩家2次数用尽！")
            self.judge_round()
            return

    def judge_round(self):
        self.log_print(f"\n📊 本轮结果：玩家1 {self.p1_count}次 | 玩家2 {self.p2_count}次")
        win = ""
        if self.p1_count < self.p2_count and self.p1_count<=self.max_try:
            self.score1 +=1
            win="玩家1"
        elif self.p2_count < self.p1_count and self.p2_count<=self.max_try:
            self.score2 +=1
            win="玩家2"
        else:
            win="平局"

        if win!="平局":
            self.log_print(f"🏆 本轮胜者：{win}")
        else:
            self.log_print("🤝 平局")

        self.log_print(f"📈 总分：玩家1 {self.score1} | 玩家2 {self.score2}")
        self.current_round +=1

        if self.current_round > self.rounds:
            self.final_result()
        else:
            self.root.after(1000, self.start_round)

    def final_result(self):
        self.entry.config(state=tk.DISABLED)
        self.btn.config(state=tk.DISABLED)
        self.info.config(text="游戏结束！")
        if self.score1 > self.score2:
            champion = "👑 玩家1 总冠军！"
        elif self.score2 > self.score1:
            champion = "👑 玩家2 总冠军！"
        else:
            champion = "🤝 终极平局！"
        self.log_print("\n"+"="*40)
        self.log_print("🏆 最终冠军："+champion)
        self.log_print("="*40)
        messagebox.showinfo("游戏结束", f"最终比分：{self.score1} - {self.score2}\n"+champion)

if __name__ == "__main__":
    root = tk.Tk()
    app = UltimateGuessGame(root)
    root.mainloop()