import tkinter as tk
from tkinter import font as tkfont
import random

class RPSGameGUI:
    def __init__(self, root):
        self.root = root
        self.root.title("石头剪刀布 - 智能对战版")
        self.root.geometry("500x650")
        self.root.resizable(False, False)
        
        # --- 配色方案 (现代深色风格) ---
        self.colors = {
            "bg": "#2c3e50",          #以此为背景色
            "panel": "#34495e",       # 面板颜色
            "accent": "#e74c3c",      # 强调色(红)
            "text": "#ecf0f1",        # 文字颜色
            "win": "#2ecc71",         # 胜利绿
            "lose": "#e74c3c",        # 失败红
            "draw": "#f1c40f"         # 平局黄
        }
        
        self.root.configure(bg=self.colors["bg"])

        # --- 游戏数据 ---
        self.choices = ["石头", "剪刀", "布"]
        self.icons = {"石头": "✊", "剪刀": "✌️", "布": "🖐️"}
        self.scores = {"玩家": 0, "电脑": 0}
        self.history = [] # 记录历史用于AI逻辑

        # --- 界面构建 ---
        self._create_widgets()

    def _create_widgets(self):
        # 1. 顶部标题区域
        header_frame = tk.Frame(self.root, bg=self.colors["panel"], height=80)
        header_frame.pack(fill="x", padx=10, pady=10)
        
        title_label = tk.Label(
            header_frame, 
            text="⚔️ 石头剪刀布 · 终极对决", 
            font=("Microsoft YaHei", 20, "bold"),
            bg=self.colors["panel"], fg=self.colors["text"]
        )
        title_label.pack(pady=25)

        # 2. 计分板区域
        score_frame = tk.Frame(self.root, bg=self.colors["bg"])
        score_frame.pack(fill="x", padx=20, pady=10)
        
        # 玩家分
        self.player_score_label = tk.Label(
            score_frame, text=f"玩家\n{self.scores['玩家']}", 
            font=("Arial", 16), bg=self.colors["bg"], fg=self.colors["win"],
            width=10, relief="ridge", bd=2
        )
        self.player_score_label.pack(side="left", expand=True, fill="both", padx=5)

        # VS
        vs_label = tk.Label(score_frame, text="VS", font=("Arial", 14, "bold"), bg=self.colors["bg"], fg=self.colors["text"])
        vs_label.pack(side="left")

        # 电脑分
        self.computer_score_label = tk.Label(
            score_frame, text=f"电脑\n{self.scores['电脑']}", 
            font=("Arial", 16), bg=self.colors["bg"], fg=self.colors["lose"],
            width=10, relief="ridge", bd=2
        )
        self.computer_score_label.pack(side="left", expand=True, fill="both", padx=5)

        # 3. 对战显示区域 (核心视觉区)
        battle_frame = tk.Frame(self.root, bg=self.colors["panel"], bd=2, relief="groove")
        battle_frame.pack(fill="both", expand=True, padx=20, pady=20)

        # 玩家出拳显示
        self.player_display = tk.Label(
            battle_frame, text=self.icons["石头"], 
            font=("Segoe UI Emoji", 60), bg=self.colors["panel"], fg=self.colors["text"]
        )
        self.player_display.pack(side="left", expand=True, pady=40)

        # 分隔线
        sep = tk.Frame(battle_frame, width=2, bg=self.colors["bg"])
        sep.pack(side="left", fill="y")

        # 电脑出拳显示
        self.computer_display = tk.Label(
            battle_frame, text=self.icons["石头"], 
            font=("Segoe UI Emoji", 60), bg=self.colors["panel"], fg=self.colors["text"]
        )
        self.computer_display.pack(side="left", expand=True, pady=40)

        # 4. 结果与状态文本
        self.result_label = tk.Label(
            self.root, text="准备开始...", 
            font=("Microsoft YaHei", 14, "bold"), 
            bg=self.colors["bg"], fg=self.colors["text"]
        )
        self.result_label.pack(pady=5)

        # 5. 控制按钮区域
        btn_frame = tk.Frame(self.root, bg=self.colors["bg"])
        btn_frame.pack(fill="x", padx=30, pady=20)

        # 定义按钮样式循环
        for choice in self.choices:
            btn = tk.Button(
                btn_frame, 
                text=f"{self.icons[choice]} {choice}",
                font=("Microsoft YaHei", 12),
                bg="#ecf0f1", fg="#2c3e50",
                activebackground="#bdc3c7",
                cursor="hand2",
                command=lambda c=choice: self.play(c)
            )
            btn.pack(side="left", expand=True, fill="both", padx=5, ipady=10)

        # 6. 底部重置按钮
        reset_btn = tk.Button(
            self.root, text="重置战绩",
            command=self.reset_game,
            bg="#7f8c8d", fg="white",
            relief="flat"
        )
        reset_btn.pack(pady=10)

    def get_computer_choice(self):
        # 简单的 AI 逻辑：如果历史记录足够长，尝试针对玩家的习惯
        if len(self.history) < 3:
            return random.choice(self.choices)
        
        # 统计最近5次玩家最爱出的
        recent_moves = self.history[-5:]
        most_common = max(set(recent_moves), key=recent_moves.count)
        
        # 克制它
        if most_common == "石头": return "布"
        elif most_common == "布": return "剪刀"
        else: return "石头"

    def play(self, player_choice):
        # 1. 获取电脑选择
        computer_choice = self.get_computer_choice()
        
        # 2. 更新历史记录
        self.history.append(player_choice)

        # 3. 更新界面图标
        self.player_display.config(text=self.icons[player_choice])
        self.computer_display.config(text=self.icons[computer_choice])

        # 4. 判定胜负
        if player_choice == computer_choice:
            result_text = "🤝 平局！"
            result_color = self.colors["draw"]
        elif (player_choice == "石头" and computer_choice == "剪刀") or \
             (player_choice == "剪刀" and computer_choice == "布") or \
             (player_choice == "布" and computer_choice == "石头"):
            result_text = "🎉 你赢了！"
            result_color = self.colors["win"]
            self.scores["玩家"] += 1
        else:
            result_text = "🤖 电脑获胜！"
            result_color = self.colors["lose"]
            self.scores["电脑"] += 1

        # 5. 刷新分数和结果文字
        self.result_label.config(text=result_text, fg=result_color)
        self.player_score_label.config(text=f"玩家\n{self.scores['玩家']}")
        self.computer_score_label.config(text=f"电脑\n{self.scores['电脑']}")

    def reset_game(self):
        self.scores["玩家"] = 0
        self.scores["电脑"] = 0
        self.history = []
        self.player_score_label.config(text="玩家\n0")
        self.computer_score_label.config(text="电脑\n0")
        self.result_label.config(text="战绩已重置", fg=self.colors["text"])
        self.player_display.config(text=self.icons["石头"])
        self.computer_display.config(text=self.icons["石头"])

if __name__ == "__main__":
    root = tk.Tk()
    game = RPSGameGUI(root)
    root.mainloop()