Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -70,7 +70,7 @@ def start_game():
|
|
70 |
return game_state
|
71 |
|
72 |
# Define the game UI
|
73 |
-
def game_ui():
|
74 |
"""Displays the game UI and updates the game state."""
|
75 |
player_cards = game_state['player_cards']
|
76 |
ai_cards = game_state['ai_cards']
|
@@ -87,31 +87,6 @@ def game_ui():
|
|
87 |
|
88 |
st.write('**Dealer**')
|
89 |
st.write('Cards: ', ' '.join([f"🂠" if len(ai_cards) == 1 else f"{card[0]}{card[1]}" for card in ai_cards]))
|
90 |
-
st.write
|
91 |
-
st.write('Score: ', game_state['ai_score'])
|
92 |
-
st.write('---')
|
93 |
-
|
94 |
-
if st.button('Play'):
|
95 |
-
if player_card is None:
|
96 |
-
st.write('Out of cards!')
|
97 |
-
return
|
98 |
-
|
99 |
-
winner = determine_winner(player_card, ai_card)
|
100 |
-
|
101 |
-
if winner == 'player':
|
102 |
-
st.write('Player wins!')
|
103 |
-
game_state['player_cards'].extend([player_card, ai_card])
|
104 |
-
game_state['player_score'] += 2
|
105 |
-
elif winner == 'ai':
|
106 |
-
st.write('Dealer wins!')
|
107 |
-
game_state['ai_cards'].extend([player_card, ai_card])
|
108 |
-
game_state['ai_score'] += 2
|
109 |
-
else:
|
110 |
-
st.write('Tie!')
|
111 |
-
game_state['player_cards'].append(player_card)
|
112 |
-
game_state['ai_cards'].append(ai_card)
|
113 |
-
|
114 |
-
game_state['rounds_played']
|
115 |
st.write('Score: ', game_state['ai_score'])
|
116 |
st.write('---')
|
117 |
|
@@ -186,17 +161,32 @@ def game_ui():
|
|
186 |
|
187 |
# Show game history
|
188 |
st.write('# Game History')
|
189 |
-
if not st.checkbox('Show game history')
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
|
|
|
|
197 |
|
198 |
# Add download button for game history
|
199 |
if st.sidebar.button('Download Game History'):
|
200 |
st.sidebar.markdown(create_download_link('game_state.txt'), unsafe_allow_html=True)
|
201 |
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
return game_state
|
71 |
|
72 |
# Define the game UI
|
73 |
+
def game_ui(game_state):
|
74 |
"""Displays the game UI and updates the game state."""
|
75 |
player_cards = game_state['player_cards']
|
76 |
ai_cards = game_state['ai_cards']
|
|
|
87 |
|
88 |
st.write('**Dealer**')
|
89 |
st.write('Cards: ', ' '.join([f"🂠" if len(ai_cards) == 1 else f"{card[0]}{card[1]}" for card in ai_cards]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
st.write('Score: ', game_state['ai_score'])
|
91 |
st.write('---')
|
92 |
|
|
|
161 |
|
162 |
# Show game history
|
163 |
st.write('# Game History')
|
164 |
+
if not st.checkbox('Show game history')
|
165 |
+
if checkbox:
|
166 |
+
with open('game_state.txt', 'r') as f:
|
167 |
+
lines = f.readlines()
|
168 |
+
headers = [header.strip() for header in lines[0].strip().split(',')]
|
169 |
+
data = [
|
170 |
+
[eval(cell) if cell.isdigit() else cell for cell in line.strip().split(',')]
|
171 |
+
for line in lines[1:]
|
172 |
+
]
|
173 |
+
st.dataframe(data, columns=headers)
|
174 |
|
175 |
# Add download button for game history
|
176 |
if st.sidebar.button('Download Game History'):
|
177 |
st.sidebar.markdown(create_download_link('game_state.txt'), unsafe_allow_html=True)
|
178 |
|
179 |
+
# Load game state from file or start new game
|
180 |
+
if os.path.exists('game_state.txt'):
|
181 |
+
game_state = {'player_cards': [], 'ai_cards': [], 'player_score': 0, 'ai_score': 0, 'rounds_played': 0}
|
182 |
+
with open('game_state.txt', 'r') as f:
|
183 |
+
headers = f.readline().strip().split(',')
|
184 |
+
data = f.readlines()
|
185 |
+
if len(data) > 0:
|
186 |
+
last_line = data[-1].strip().split(',')
|
187 |
+
for i in range(len(headers)):
|
188 |
+
game_state[headers[i]] = eval(last_line[i])
|
189 |
+
else:
|
190 |
+
game_state = start_game()
|
191 |
+
|
192 |
+
game_ui(game_state)
|