Lashtw commited on
Commit
542fb3c
·
verified ·
1 Parent(s): d74384d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -46
app.py CHANGED
@@ -1,74 +1,88 @@
1
  import gradio as gr
2
 
3
- # 模擬資料,方便執行
4
  groups = {}
5
 
6
  def initialize_groups(group_info, health):
 
7
  global groups
8
- groups = {}
9
- group_lines = group_info.split("\n")
10
  for idx, line in enumerate(group_lines, 1):
 
11
  groups[f"小隊{idx}"] = {
12
- "students": line.split(","),
13
  "health": health,
14
  "current_health": health
15
  }
16
- return f"已建立 {len(groups)} 個小隊,每隊初始血量 {health}。"
 
17
 
18
- def attack_team(team_name, damage):
 
19
  if team_name not in groups:
20
- return "錯誤:小隊不存在!"
21
- groups[team_name]["current_health"] -= damage
22
- if groups[team_name]["current_health"] < 0:
23
- groups[team_name]["current_health"] = 0
24
- return f"{team_name} 受到 {damage} 點攻擊!剩餘血量:{groups[team_name]['current_health']}"
 
 
 
25
 
26
- def display_teams():
27
- result = ""
28
- for team_name, data in groups.items():
29
- result += f"{team_name}:{data['current_health']}/{data['health']}\n"
30
- return result.strip()
 
 
 
31
 
32
- # Gradio 介面
33
  with gr.Blocks() as app:
34
- gr.Markdown("## 雞排王小隊對戰系統")
35
 
36
- # 初始化組別與最大血量
37
  with gr.Row():
38
- group_input = gr.Textbox(label="輸入組別資訊(每組學生用逗號分隔,換行分組)", placeholder="範例:小明,小華\n小美,小強")
39
- health_input = gr.Number(label="設定雞排王最大血量", value=500)
40
- init_button = gr.Button("初始化組別")
41
- output_init = gr.Textbox(label="系統訊息")
 
 
 
42
 
43
- # 攻擊輸入區
44
- with gr.Row():
45
- team_dropdown = gr.Dropdown(label="選擇攻擊小隊", choices=[], interactive=True)
46
- damage_input = gr.Number(label="輸入攻擊值", value=50)
47
- attack_button = gr.Button("攻擊")
48
- attack_output = gr.Textbox(label="攻擊結果")
49
 
50
- # 更新小隊血量顯示
51
- display_button = gr.Button("顯示目前血量")
52
- teams_display = gr.Textbox(label="小隊血量狀態", lines=10)
53
-
54
- # 初始化組別並更新 Dropdown 選項
55
- def initialize_groups_and_update_dropdown(group_info, health):
56
- message = initialize_groups(group_info, health) # 呼叫初始化函式
57
- dropdown_choices = list(groups.keys()) # 取得小隊名稱列表
58
- return message, dropdown_choices
59
-
60
- # 綁定事件:初始化按鈕
61
  init_button.click(
62
- initialize_groups_and_update_dropdown,
63
- inputs=[group_input, health_input],
64
  outputs=[output_init, team_dropdown]
65
  )
 
 
 
 
 
 
 
66
 
67
- # 攻擊按鈕綁定
68
- attack_button.click(attack_team, inputs=[team_dropdown, damage_input], outputs=attack_output)
69
- display_button.click(display_teams, outputs=teams_display)
 
 
 
70
 
71
- # 啟動應用程式
72
  app.launch()
73
 
74
 
 
1
  import gradio as gr
2
 
3
+ # 全域變數儲存小隊資料
4
  groups = {}
5
 
6
  def initialize_groups(group_info, health):
7
+ """初始化小隊資料"""
8
  global groups
9
+ groups = {} # 清空現有小隊資料
10
+ group_lines = group_info.strip().split("\n") # 逐行解析輸入
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
+ groups[team_name]["current_health"] = max(
28
+ groups[team_name]["current_health"] - attack_value, 0
29
+ )
30
+ result = f"{team_name} 受到 {attack_value} 點傷害,剩餘血量 {groups[team_name]['current_health']}/{groups[team_name]['health']}。"
31
+ return result, get_team_health_status()
32
 
33
+ def get_team_health_status():
34
+ """回傳所有小隊的血量狀態"""
35
+ status = []
36
+ for team, data in groups.items():
37
+ current = data["current_health"]
38
+ total = data["health"]
39
+ status.append(f"{team}: {current}/{total}")
40
+ return "\n".join(status)
41
 
42
+ # Gradio 介面設置
43
  with gr.Blocks() as app:
44
+ gr.Markdown("## 小隊攻擊系統")
45
 
46
+ # 初始化小隊部分
47
  with gr.Row():
48
+ group_input = gr.Textbox(
49
+ placeholder="請輸入小隊資訊,例如:1號, 2號, 3號, 4號\n5號, 6號, 7號, 8號",
50
+ label="輸入小隊成員 (每行一個小隊)"
51
+ )
52
+ health_input = gr.Number(value=600, label="初始血量")
53
+ init_button = gr.Button("初始化")
54
+ output_init = gr.Textbox(label="初始化結果")
55
 
56
+ # 攻擊小隊部分
57
+ team_dropdown = gr.Dropdown(choices=[], label="選擇攻擊小隊")
58
+ attack_input = gr.Number(value=200, label="輸入攻擊值")
59
+ attack_button = gr.Button("攻擊")
60
+ output_attack = gr.Textbox(label="攻擊結果")
 
61
 
62
+ # 小隊血量顯示
63
+ team_status = gr.Textbox(label="小隊血量狀態", interactive=False)
64
+
65
+ # 初始化按鈕事件
 
 
 
 
 
 
 
66
  init_button.click(
67
+ initialize_groups,
68
+ inputs=[group_input, health_input],
69
  outputs=[output_init, team_dropdown]
70
  )
71
+
72
+ # 攻擊按鈕事件
73
+ attack_button.click(
74
+ attack_team,
75
+ inputs=[team_dropdown, attack_input],
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