File size: 2,717 Bytes
d74384d
 
d19c6c4
 
 
 
d74384d
d19c6c4
d74384d
d19c6c4
 
 
 
542fb3c
d19c6c4
 
 
 
 
 
 
 
 
 
3a53d9a
 
 
 
 
d74384d
d19c6c4
 
 
 
 
 
 
 
 
d74384d
d19c6c4
d763d82
d19c6c4
d763d82
d19c6c4
d763d82
d19c6c4
 
 
 
 
 
d763d82
d19c6c4
 
3a53d9a
d19c6c4
3a53d9a
d19c6c4
d763d82
d19c6c4
 
 
542fb3c
d19c6c4
3a53d9a
d19c6c4
d763d82
d19c6c4
d763d82
3a53d9a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import gradio as gr

# 全局變數設定
groups = {}  # 儲存每組學生和血量
team_health = {}  # 儲存每組的雞排王當前血量
max_health = 500  # 預設雞排王的最大血量

# 初始化組別
def initialize_groups(group_info, health):
    global groups, team_health, max_health
    max_health = int(health)
    groups.clear()
    team_health.clear()
    
    group_data = group_info.split("\n")
    for idx, group in enumerate(group_data, start=1):
        students = group.split(",")  # 每組學生用逗號分隔
        group_name = f"小隊{idx}"
        groups[group_name] = students
        team_health[group_name] = max_health
        
    return f"已建立 {len(groups)} 組,每組血量為 {max_health}。"

# 輸入攻擊值並扣血
def attack_team(team_name, damage):
    if team_name in team_health:
        team_health[team_name] = max(0, team_health[team_name] - int(damage))
        return f"{team_name} 的血量剩餘 {team_health[team_name]}。"
    return "小隊名稱不存在。"

# 顯示目前所有小隊的血量狀態
def display_teams():
    display = ""
    for team, students in groups.items():
        health = team_health.get(team, 0)
        bar_length = int((health / max_health) * 20)
        health_bar = "█" * bar_length + " " * (20 - bar_length)
        display += f"{team} ({', '.join(students)}): [{health_bar}] {health}/{max_health}\n"
    return display.strip()

# Gradio 介面
with gr.Blocks() as app:
    gr.Markdown("## 雞排王小隊對戰系統")
    
    # 初始化組別與最大血量
    with gr.Row():
        group_input = gr.Textbox(label="輸入組別資訊(每組學生用逗號分隔,換行分組)", placeholder="範例:小明,小華\n小美,小強")
        health_input = gr.Number(label="設定雞排王最大血量", value=500)
        init_button = gr.Button("初始化組別")
    output_init = gr.Textbox(label="系統訊息")
    
    init_button.click(initialize_groups, inputs=[group_input, health_input], outputs=output_init)
    
    # 攻擊輸入區
    with gr.Row():
        team_input = gr.Textbox(label="輸入攻擊小隊名稱", placeholder="例如:小隊1")
        damage_input = gr.Number(label="輸入攻擊值", value=50)
        attack_button = gr.Button("攻擊")
    attack_output = gr.Textbox(label="攻擊結果")
    
    # 更新小隊血量顯示
    display_button = gr.Button("顯示目前血量")
    teams_display = gr.Textbox(label="小隊血量狀態", lines=10)
    
    # 事件綁定
    attack_button.click(attack_team, inputs=[team_input, damage_input], outputs=attack_output)
    display_button.click(display_teams, outputs=teams_display)

# 啟動應用程式
app.launch()