import tkinter as tk
from tkinter import messagebox
import random

# 主窗口配置
root = tk.Tk()
root.title("石头剪刀布小游戏")
root.geometry("450x320")
root.resizable(False, False)

# 计分变量
user_score = 0
computer_score = 0

# 出拳对应字典
choice_dict = {0: "石头", 1: "剪刀", 2: "布"}


def judge_result(user_choice):
    """判断胜负逻辑"""
    global user_score, computer_score
    computer_choice = random.randint(0, 2)

    # 更新出招显示
    lab_user_play.config(text=f"你的选择：{choice_dict[user_choice]}")
    lab_com_play.config(text=f"电脑选择：{choice_dict[computer_choice]}")

    # 判定输赢
    if user_choice == computer_choice:
        res = "平局！再来一局"
    elif (user_choice == 0 and computer_choice == 1) or \
            (user_choice == 1 and computer_choice == 2) or \
            (user_choice == 2 and computer_choice == 0):
        user_score += 1
        res = "恭喜，你赢啦！"
    else:
        computer_score += 1
        res = "很遗憾，电脑获胜"

    # 刷新分数和结果
    lab_result.config(text=res)
    lab_score.config(text=f"玩家分数：{user_score}  | 电脑分数：{computer_score}")


def reset_game():
    """重置所有数据"""
    global user_score, computer_score
    user_score = 0
    computer_score = 0
    lab_user_play.config(text="你的选择：无")
    lab_com_play.config(text="电脑选择：无")
    lab_result.config(text="等待出拳...")
    lab_score.config(text=f"玩家分数：{user_score}  | 电脑分数：{computer_score}")


# 界面布局
# 标题
tk.Label(root, text="石头剪刀布", font=("黑体", 20, "bold")).pack(pady=12)

# 分数栏
lab_score = tk.Label(root, text=f"玩家分数：0  | 电脑分数：0", font=("微软雅黑", 12))
lab_score.pack()

# 出招展示区
frame_show = tk.Frame(root)
frame_show.pack(pady=15)
lab_user_play = tk.Label(frame_show, text="你的选择：无", font=("微软雅黑", 11), width=15)
lab_com_play = tk.Label(frame_show, text="电脑选择：无", font=("微软雅黑", 11), width=15)
lab_user_play.grid(row=0, column=0, padx=10)
lab_com_play.grid(row=0, column=1, padx=10)

# 胜负结果
lab_result = tk.Label(root, text="等待出拳...", font=("黑体", 14), fg="#d02020")
lab_result.pack(pady=8)

# 按钮区域
frame_btn = tk.Frame(root)
frame_btn.pack(pady=10)
# 石头、剪刀、布按钮
tk.Button(frame_btn, text="石头", width=8, height=2, font=("微软雅黑", 11),
          command=lambda: judge_result(0)).grid(row=0, column=0, padx=6)
tk.Button(frame_btn, text="剪刀", width=8, height=2, font=("微软雅黑", 11),
          command=lambda: judge_result(1)).grid(row=0, column=1, padx=6)
tk.Button(frame_btn, text="布", width=8, height=2, font=("微软雅黑", 11),
          command=lambda: judge_result(2)).grid(row=0, column=2, padx=6)

# 重置按钮
tk.Button(root, text="重置游戏", font=("微软雅黑", 10), bg="#eeeeee", command=reset_game).pack(pady=6)

# 窗口循环
root.mainloop()