Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,104 +2,96 @@
|
|
2 |
import streamlit as st
|
3 |
import random
|
4 |
import time
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
with open("
|
|
|
|
|
13 |
f.write("")
|
14 |
|
15 |
# Read game state
|
16 |
-
def read_game_state():
|
17 |
-
with open("
|
18 |
state = f.readline().split(',')
|
19 |
-
|
20 |
-
|
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(
|
26 |
-
|
27 |
-
|
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 |
-
|
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
|
42 |
-
with open("
|
43 |
-
|
|
|
44 |
|
45 |
# Update game based on actions
|
46 |
-
if turn ==
|
47 |
-
if
|
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 |
-
|
58 |
-
elif
|
59 |
heal = random.randint(5, 10)
|
60 |
-
|
61 |
-
turn = "Player 1"
|
62 |
|
63 |
# Write updated game state
|
64 |
-
with open("
|
65 |
-
f.write(f"{
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
# Streamlit Interface
|
68 |
def app():
|
69 |
st.title("Text Battle!")
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
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 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
# Run the Streamlit app
|
105 |
app()
|
|
|
2 |
import streamlit as st
|
3 |
import random
|
4 |
import time
|
5 |
+
from datetime import datetime
|
6 |
|
7 |
+
# Constants
|
8 |
+
HISTORY_LENGTH = 5 # Number of previous actions to display
|
9 |
+
|
10 |
+
# Initialize game state
|
11 |
+
def initialize_game(player_name):
|
12 |
+
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
13 |
+
with open(f"{timestamp}_{player_name}_state.txt", "w") as f:
|
14 |
+
f.write(f"{player_name}:100,None,Player 1")
|
15 |
+
with open(f"{timestamp}_{player_name}_actions.txt", "w") as f:
|
16 |
f.write("")
|
17 |
|
18 |
# Read game state
|
19 |
+
def read_game_state(player_name, timestamp):
|
20 |
+
with open(f"{timestamp}_{player_name}_state.txt", "r") as f:
|
21 |
state = f.readline().split(',')
|
22 |
+
player_name, hp, last_action, turn = state
|
23 |
+
return player_name, int(hp), last_action, turn
|
|
|
|
|
24 |
|
25 |
# Write player action
|
26 |
+
def write_action(player_name, action, timestamp):
|
27 |
+
with open(f"{timestamp}_{player_name}_actions.txt", "a") as f:
|
28 |
+
f.write(f"{action}\n")
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# Update game state based on actions
|
31 |
+
def update_game_state(player_name, timestamp):
|
32 |
+
_, hp, _, turn = read_game_state(player_name, timestamp)
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
# Read Player's action
|
35 |
+
with open(f"{timestamp}_{player_name}_actions.txt", "r") as f:
|
36 |
+
actions = f.readlines()
|
37 |
+
last_action = actions[-1].strip() if actions else None
|
38 |
|
39 |
# Update game based on actions
|
40 |
+
if turn == player_name and last_action:
|
41 |
+
if last_action == "Attack":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
damage = random.randint(5, 15)
|
43 |
+
hp -= damage
|
44 |
+
elif last_action == "Heal":
|
45 |
heal = random.randint(5, 10)
|
46 |
+
hp += heal
|
|
|
47 |
|
48 |
# Write updated game state
|
49 |
+
with open(f"{timestamp}_{player_name}_state.txt", "w") as f:
|
50 |
+
f.write(f"{player_name}:{hp},{last_action},Player 1")
|
51 |
+
|
52 |
+
# Get the last few actions
|
53 |
+
def get_recent_actions(player_name, timestamp):
|
54 |
+
with open(f"{timestamp}_{player_name}_actions.txt", "r") as f:
|
55 |
+
actions = f.readlines()
|
56 |
+
return actions[-HISTORY_LENGTH:]
|
57 |
|
58 |
# Streamlit Interface
|
59 |
def app():
|
60 |
st.title("Text Battle!")
|
61 |
|
62 |
+
if "player_name" not in st.session_state:
|
63 |
+
st.session_state.player_name = st.text_input("Enter your name:")
|
64 |
+
if st.session_state.player_name:
|
65 |
+
st.session_state.timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
66 |
+
initialize_game(st.session_state.player_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
if "player_name" in st.session_state:
|
69 |
+
player_name, hp, last_action, turn = read_game_state(st.session_state.player_name, st.session_state.timestamp)
|
70 |
+
|
71 |
+
st.write(f"{player_name} HP: {hp}")
|
72 |
+
st.write(f"Last Action: {last_action}")
|
73 |
+
|
74 |
+
if hp <= 0:
|
75 |
+
st.write(f"{player_name} has been defeated!")
|
76 |
+
del st.session_state.player_name
|
77 |
+
st.stop()
|
78 |
+
|
79 |
+
# Display recent actions
|
80 |
+
st.write("Recent Actions:")
|
81 |
+
for action in get_recent_actions(st.session_state.player_name, st.session_state.timestamp):
|
82 |
+
st.write(action.strip())
|
83 |
+
|
84 |
+
# Player actions
|
85 |
+
if turn == st.session_state.player_name:
|
86 |
+
action = st.selectbox(f"{st.session_state.player_name}, choose your action:", ["", "Attack", "Heal", "Pass"])
|
87 |
+
if st.button("Submit"):
|
88 |
+
write_action(st.session_state.player_name, action, st.session_state.timestamp)
|
89 |
+
update_game_state(st.session_state.player_name, st.session_state.timestamp)
|
90 |
+
|
91 |
+
# Display timer and refresh game state every 10 seconds
|
92 |
+
st.write("Refreshing in 10 seconds...")
|
93 |
+
time.sleep(10)
|
94 |
+
st.experimental_rerun()
|
95 |
|
96 |
# Run the Streamlit app
|
97 |
app()
|