awacke1 commited on
Commit
fce2dad
·
1 Parent(s): d5be61a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import streamlit as st
3
+ import random
4
+ import time
5
+
6
+ # Initialize game state and player names
7
+ def initialize_game():
8
+ with open("game_state.txt", "w") as f:
9
+ f.write("Player 1:100,Player 2:100,Player 1")
10
+ with open("player1_actions.txt", "w") as f:
11
+ f.write("")
12
+ with open("player2_actions.txt", "w") as f:
13
+ f.write("")
14
+
15
+ # Read game state
16
+ def read_game_state():
17
+ with open("game_state.txt", "r") as f:
18
+ state = f.readline().split(',')
19
+ p1_name, p1_hp = state[0].split(':')
20
+ p2_name, p2_hp = state[1].split(':')
21
+ turn = state[2]
22
+ return p1_name, int(p1_hp), p2_name, int(p2_hp), turn
23
+
24
+ # Write player action
25
+ def write_action(player, action):
26
+ if player == "Player 1":
27
+ with open("player1_actions.txt", "w") as f:
28
+ f.write(action)
29
+ else:
30
+ with open("player2_actions.txt", "w") as f:
31
+ f.write(action)
32
+
33
+ # Update game state based on actions
34
+ def update_game_state():
35
+ p1_name, p1_hp, p2_name, p2_hp, turn = read_game_state()
36
+
37
+ # Read Player 1's action
38
+ with open("player1_actions.txt", "r") as f:
39
+ p1_action = f.readline().strip()
40
+
41
+ # Read Player 2's action
42
+ with open("player2_actions.txt", "r") as f:
43
+ p2_action = f.readline().strip()
44
+
45
+ # Update game based on actions
46
+ if turn == "Player 1" and p1_action:
47
+ if p1_action == "Attack":
48
+ damage = random.randint(5, 15)
49
+ p2_hp -= damage
50
+ elif p1_action == "Heal":
51
+ heal = random.randint(5, 10)
52
+ p1_hp += heal
53
+ turn = "Player 2"
54
+ elif turn == "Player 2" and p2_action:
55
+ if p2_action == "Attack":
56
+ damage = random.randint(5, 15)
57
+ p1_hp -= damage
58
+ elif p2_action == "Heal":
59
+ heal = random.randint(5, 10)
60
+ p2_hp += heal
61
+ turn = "Player 1"
62
+
63
+ # Write updated game state
64
+ with open("game_state.txt", "w") as f:
65
+ f.write(f"{p1_name}:{p1_hp},{p2_name}:{p2_hp},{turn}")
66
+
67
+ # Streamlit Interface
68
+ def app():
69
+ st.title("Text Battle!")
70
+
71
+ # Initialize game if not already initialized
72
+ if not st.session_state.get("initialized"):
73
+ initialize_game()
74
+ st.session_state.initialized = True
75
+
76
+ p1_name, p1_hp, p2_name, p2_hp, turn = read_game_state()
77
+
78
+ st.write(f"{p1_name} HP: {p1_hp}")
79
+ st.write(f"{p2_name} HP: {p2_hp}")
80
+
81
+ if p1_hp <= 0:
82
+ st.write(f"{p2_name} Wins!")
83
+ st.stop()
84
+ elif p2_hp <= 0:
85
+ st.write(f"{p1_name} Wins!")
86
+ st.stop()
87
+
88
+ # Player actions
89
+ if turn == "Player 1":
90
+ action = st.selectbox("Player 1, choose your action:", ["", "Attack", "Heal", "Pass"])
91
+ if st.button("Submit"):
92
+ write_action("Player 1", action)
93
+ update_game_state()
94
+ else:
95
+ action = st.selectbox("Player 2, choose your action:", ["", "Attack", "Heal", "Pass"])
96
+ if st.button("Submit"):
97
+ write_action("Player 2", action)
98
+ update_game_state()
99
+
100
+ # Refresh game state every 3 seconds
101
+ time.sleep(3)
102
+ st.experimental_rerun()
103
+
104
+ # Run the Streamlit app
105
+ app()