Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import os
|
2 |
import random
|
3 |
import streamlit as st
|
|
|
4 |
|
5 |
# Define the game rules
|
6 |
NUM_ROUNDS = 26
|
@@ -53,6 +54,21 @@ def determine_winner(player_card, ai_card):
|
|
53 |
else:
|
54 |
return compare_cards(player_card, ai_card)
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
# Define the game UI
|
57 |
def game_ui():
|
58 |
"""Displays the game UI and updates the game state."""
|
@@ -71,23 +87,49 @@ def game_ui():
|
|
71 |
|
72 |
st.write('**Dealer**')
|
73 |
st.write('Cards: ', ' '.join([f"🂠" if len(ai_cards) == 1 else f"{card[0]}{card[1]}" for card in ai_cards]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
st.write('Score: ', game_state['ai_score'])
|
75 |
st.write('---')
|
76 |
|
77 |
-
if st.button('
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
81 |
winner = determine_winner(player_card, ai_card)
|
|
|
82 |
if winner == 'player':
|
83 |
-
st.write('
|
84 |
game_state['player_cards'].extend([player_card, ai_card])
|
85 |
game_state['player_score'] += 2
|
86 |
elif winner == 'ai':
|
87 |
-
st.write('Dealer
|
88 |
game_state['ai_cards'].extend([player_card, ai_card])
|
89 |
game_state['ai_score'] += 2
|
90 |
-
|
91 |
else:
|
92 |
st.write('Tie!')
|
93 |
game_state['player_cards'].append(player_card)
|
@@ -104,14 +146,20 @@ def game_ui():
|
|
104 |
st.sidebar.write('---')
|
105 |
if st.sidebar.button('New Game'):
|
106 |
# Reset game state
|
107 |
-
game_state
|
108 |
-
|
109 |
-
|
110 |
-
game_state
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
# Save game state to file
|
117 |
with open('game_state.txt', 'w') as f:
|
@@ -143,44 +191,15 @@ def game_ui():
|
|
143 |
with open('game_state.txt', 'r') as f:
|
144 |
lines = f.readlines()
|
145 |
headers = [header.strip() for header in lines[0].strip().split(',')]
|
146 |
-
data =
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
if winner == 'player':
|
159 |
-
game_state['player_cards'].extend([player_card, ai_card])
|
160 |
-
game_state['player_score'] += 2
|
161 |
-
else:
|
162 |
-
game_state['player_cards'].append(player_card)
|
163 |
-
game_state['ai_cards'].append(ai_card)
|
164 |
-
|
165 |
-
game_state['rounds_played'] += 1
|
166 |
-
|
167 |
-
# Save game state to file
|
168 |
-
with open('game_state.txt', 'w') as f:
|
169 |
-
if not os.path.exists('game_state.txt'):
|
170 |
-
f.write('player_cards,ai_cards,player_score,ai_score,rounds_played\n')
|
171 |
-
f.write(','.join([str(game_state[key]) for key in game_state.keys()]) + '\n')
|
172 |
-
|
173 |
-
import base64
|
174 |
-
|
175 |
-
def create_download_link(filename):
|
176 |
-
with open(filename, 'r') as f:
|
177 |
-
text = f.read()
|
178 |
-
b64 = base64.b64encode(text.encode()).decode()
|
179 |
-
href = f'<a href="data:file/txt;base64,{b64}" download="{filename}">Download {filename}</a>'
|
180 |
-
return href
|
181 |
-
|
182 |
-
if st.sidebar.button('Download Game State'):
|
183 |
-
st.sidebar.markdown(create_download_link('game_state.txt'), unsafe_allow_html=True)
|
184 |
-
|
185 |
-
|
186 |
-
|
|
|
1 |
import os
|
2 |
import random
|
3 |
import streamlit as st
|
4 |
+
import base64
|
5 |
|
6 |
# Define the game rules
|
7 |
NUM_ROUNDS = 26
|
|
|
54 |
else:
|
55 |
return compare_cards(player_card, ai_card)
|
56 |
|
57 |
+
def create_download_link(filename):
|
58 |
+
with open(filename, 'r') as f:
|
59 |
+
text = f.read()
|
60 |
+
b64 = base64.b64encode(text.encode()).decode()
|
61 |
+
href = f'<a href="data:file/txt;base64,{b64}" download="{filename}">Download {filename}</a>'
|
62 |
+
return href
|
63 |
+
|
64 |
+
def start_game():
|
65 |
+
"""Initializes the game state and starts the game."""
|
66 |
+
game_state = {'player_cards': [], 'ai_cards': [], 'player_score': 0, 'ai_score': 0, 'rounds_played': 0}
|
67 |
+
deck = shuffle_deck()
|
68 |
+
game_state['player_cards'] = deck[:26]
|
69 |
+
game_state['ai_cards'] = deck[26:]
|
70 |
+
return game_state
|
71 |
+
|
72 |
# Define the game UI
|
73 |
def game_ui():
|
74 |
"""Displays the game UI and updates the game state."""
|
|
|
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 |
|
118 |
+
if st.button('Play'):
|
119 |
+
if player_card is None:
|
120 |
+
st.write('Out of cards!')
|
121 |
+
return
|
122 |
+
|
123 |
winner = determine_winner(player_card, ai_card)
|
124 |
+
|
125 |
if winner == 'player':
|
126 |
+
st.write('Player wins!')
|
127 |
game_state['player_cards'].extend([player_card, ai_card])
|
128 |
game_state['player_score'] += 2
|
129 |
elif winner == 'ai':
|
130 |
+
st.write('Dealer wins!')
|
131 |
game_state['ai_cards'].extend([player_card, ai_card])
|
132 |
game_state['ai_score'] += 2
|
|
|
133 |
else:
|
134 |
st.write('Tie!')
|
135 |
game_state['player_cards'].append(player_card)
|
|
|
146 |
st.sidebar.write('---')
|
147 |
if st.sidebar.button('New Game'):
|
148 |
# Reset game state
|
149 |
+
game_state = start_game()
|
150 |
+
|
151 |
+
# Save game state to file
|
152 |
+
with open('game_state.txt', 'w') as f:
|
153 |
+
f.write('player_cards,ai_cards,player_score,ai_score,rounds_played\n')
|
154 |
+
f.write(','.join([str(game_state[key]) for key in game_state.keys()]) + '\n')
|
155 |
+
|
156 |
+
if st.sidebar.button('Reset Game'):
|
157 |
+
# Reset game state
|
158 |
+
game_state = start_game()
|
159 |
+
|
160 |
+
# Truncate game_state.txt file by deleting it and reloading it
|
161 |
+
os.remove('game_state.txt')
|
162 |
+
open('game_state.txt', 'w').close()
|
163 |
|
164 |
# Save game state to file
|
165 |
with open('game_state.txt', 'w') as f:
|
|
|
191 |
with open('game_state.txt', 'r') as f:
|
192 |
lines = f.readlines()
|
193 |
headers = [header.strip() for header in lines[0].strip().split(',')]
|
194 |
+
data =
|
195 |
+
[
|
196 |
+
[eval(cell) if cell.isdigit() else cell for cell in line.strip().split(',')]
|
197 |
+
for line in lines[1:]
|
198 |
+
]
|
199 |
+
st.dataframe(data, columns=headers)
|
200 |
+
|
201 |
+
# Add download button for game history
|
202 |
+
if st.sidebar.button('Download Game History'):
|
203 |
+
st.sidebar.markdown(create_download_link('game_state.txt'), unsafe_allow_html=True)
|
204 |
+
|
205 |
+
game_ui()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|