Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,89 +1,79 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
groups = {}
|
|
|
|
|
|
|
| 5 |
|
|
|
|
| 6 |
def initialize_groups(group_info, health):
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
groups
|
| 10 |
-
|
| 11 |
-
for idx, line in enumerate(group_lines, 1):
|
| 12 |
-
students = [name.strip() for name in line.split(",")]
|
| 13 |
-
groups[f"小隊{idx}"] = {
|
| 14 |
-
"students": students,
|
| 15 |
-
"health": health,
|
| 16 |
-
"current_health": health
|
| 17 |
-
}
|
| 18 |
-
team_names = list(groups.keys()) # 小隊名稱清單
|
| 19 |
-
return f"已成功建立 {len(groups)} 個小隊,每隊初始血量 {health}。", team_names
|
| 20 |
-
|
| 21 |
-
def attack_team(team_name, attack_value):
|
| 22 |
-
"""對指定小隊進行攻擊"""
|
| 23 |
-
if team_name not in groups:
|
| 24 |
-
return "請先選擇有效的小隊名稱!", get_team_health_status()
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
for team,
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
# Gradio
|
| 43 |
with gr.Blocks() as app:
|
| 44 |
-
gr.Markdown("##
|
| 45 |
|
| 46 |
-
#
|
| 47 |
with gr.Row():
|
| 48 |
-
group_input = gr.Textbox(
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
init_button =
|
| 54 |
-
output_init = gr.Textbox(label="初始化結果")
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
inputs=[group_input, health_input],
|
| 69 |
-
outputs=[output_init, team_dropdown]
|
| 70 |
-
)
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
outputs=[output_attack, team_status]
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
# 初始顯示所有小隊血量狀態
|
| 80 |
-
init_button.click(
|
| 81 |
-
get_team_health_status,
|
| 82 |
-
inputs=None,
|
| 83 |
-
outputs=team_status
|
| 84 |
-
)
|
| 85 |
|
|
|
|
| 86 |
app.launch()
|
| 87 |
|
| 88 |
|
| 89 |
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# 全局變數設定
|
| 4 |
+
groups = {} # 儲存每組學生和血量
|
| 5 |
+
team_health = {} # 儲存每組的雞排王當前血量
|
| 6 |
+
max_health = 500 # 預設雞排王的最大血量
|
| 7 |
|
| 8 |
+
# 初始化組別
|
| 9 |
def initialize_groups(group_info, health):
|
| 10 |
+
global groups, team_health, max_health
|
| 11 |
+
max_health = int(health)
|
| 12 |
+
groups.clear()
|
| 13 |
+
team_health.clear()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
group_data = group_info.split("\n")
|
| 16 |
+
for idx, group in enumerate(group_data, start=1):
|
| 17 |
+
students = group.split(",") # 每組學生用逗號分隔
|
| 18 |
+
group_name = f"小隊{idx}"
|
| 19 |
+
groups[group_name] = students
|
| 20 |
+
team_health[group_name] = max_health
|
| 21 |
+
|
| 22 |
+
return f"已建立 {len(groups)} 組,每組血量為 {max_health}。"
|
| 23 |
+
|
| 24 |
+
# 輸入攻擊值並扣血
|
| 25 |
+
def attack_team(team_name, damage):
|
| 26 |
+
if team_name in team_health:
|
| 27 |
+
team_health[team_name] = max(0, team_health[team_name] - int(damage))
|
| 28 |
+
return f"{team_name} 的血量剩餘 {team_health[team_name]}。"
|
| 29 |
+
return "小隊名稱不存在。"
|
| 30 |
|
| 31 |
+
# 顯示目前所有小隊的血量狀態
|
| 32 |
+
def display_teams():
|
| 33 |
+
display = ""
|
| 34 |
+
for team, students in groups.items():
|
| 35 |
+
health = team_health.get(team, 0)
|
| 36 |
+
bar_length = int((health / max_health) * 20)
|
| 37 |
+
health_bar = "█" * bar_length + " " * (20 - bar_length)
|
| 38 |
+
display += f"{team} ({', '.join(students)}): [{health_bar}] {health}/{max_health}\n"
|
| 39 |
+
return display.strip()
|
| 40 |
|
| 41 |
+
# Gradio 介面
|
| 42 |
with gr.Blocks() as app:
|
| 43 |
+
gr.Markdown("## 雞排王小隊對戰系統")
|
| 44 |
|
| 45 |
+
# 初始化組別與最大血量
|
| 46 |
with gr.Row():
|
| 47 |
+
group_input = gr.Textbox(label="輸入組別資訊(每組學生用逗號分隔,換行分組)", placeholder="範例:小明,小華\n小美,小強")
|
| 48 |
+
health_input = gr.Number(label="設定雞排王最大血量", value=500)
|
| 49 |
+
init_button = gr.Button("初始化組別")
|
| 50 |
+
output_init = gr.Textbox(label="系統訊息")
|
| 51 |
+
|
| 52 |
+
init_button.click(initialize_groups, inputs=[group_input, health_input], outputs=output_init)
|
|
|
|
| 53 |
|
| 54 |
+
# 攻擊輸入區
|
| 55 |
+
with gr.Row():
|
| 56 |
+
team_dropdown = gr.Dropdown(label="選擇攻擊小隊", choices=[], interactive=True)
|
| 57 |
+
damage_input = gr.Number(label="輸入攻擊值", value=50)
|
| 58 |
+
attack_button = gr.Button("攻擊")
|
| 59 |
+
attack_output = gr.Textbox(label="攻擊結果")
|
| 60 |
|
| 61 |
+
# 更新小隊血量顯示
|
| 62 |
+
display_button = gr.Button("顯示目前血量")
|
| 63 |
+
teams_display = gr.Textbox(label="小隊血量狀態", lines=10)
|
| 64 |
|
| 65 |
+
# 事件綁定
|
| 66 |
+
attack_button.click(attack_team, inputs=[team_dropdown, damage_input], outputs=attack_output)
|
| 67 |
+
display_button.click(display_teams, outputs=teams_display)
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
# 更新選單小隊名稱
|
| 70 |
+
def update_team_dropdown():
|
| 71 |
+
return list(groups.keys())
|
| 72 |
+
init_button.click(update_team_dropdown, outputs=team_dropdown)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# 啟動應用程式
|
| 75 |
app.launch()
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
+
|