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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -70
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
- 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
 
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
+