awacke1 commited on
Commit
2c6b833
·
1 Parent(s): 784add3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -37
app.py CHANGED
@@ -80,7 +80,8 @@ def game_ui():
80
  winner = determine_winner(player_card, ai_card)
81
  if winner == 'player':
82
  st.write('You won this round!')
83
- game_state['player_cards'].extend([player_card, ai_card])
 
84
  game_state['player_score'] += 2
85
  elif winner == 'ai':
86
  st.write('Dealer won this round!')
@@ -93,6 +94,10 @@ def game_ui():
93
 
94
  game_state['rounds_played'] += 1
95
 
 
 
 
 
96
  st.sidebar.write('---')
97
  if st.sidebar.button('New Game'):
98
  # Reset game state
@@ -105,51 +110,54 @@ def game_ui():
105
  game_state['player_cards'] = deck[:26]
106
  game_state['ai_cards'] = deck[26:]
107
 
 
 
 
 
108
  if st.sidebar.button('Save'):
 
109
  with open('game_state.txt', 'w') as f:
110
  f.write(str(game_state))
111
 
112
  if st.sidebar.button('Reload'):
 
113
  with open('game_state.txt', 'r') as f:
114
  game_state = eval(f.read())
115
 
116
- # Define the game state and load from file if available
117
- game_state = {
118
- 'player_cards': [],
119
- 'ai_cards': [],
120
- 'player_score': 0,
121
- 'ai_score': 0,
122
- 'rounds_played': 0,
123
- }
124
- if st.sidebar.button('Reload'):
 
 
125
  with open('game_state.txt', 'r') as f:
126
  game_state = eval(f.read())
127
-
128
- # Play the game
129
- if len(game_state['player_cards']) == 0 and len(game_state['ai_cards']) == 0:
130
- deck = shuffle_deck()
131
- game_state['player_cards'] = deck[:26]
132
- game_state['ai_cards'] = deck[26:]
133
-
134
- while game_state['rounds_played'] < NUM_ROUNDS and len(game_state['player_cards']) + len(game_state['ai_cards']) == 52:
135
- game_ui()
136
-
137
- if st.sidebar.button('Save'):
 
 
 
 
 
 
 
 
 
 
 
138
  with open('game_state.txt', 'w') as f:
139
  f.write(str(game_state))
140
-
141
- player_card = draw_card(game_state['player_cards'])
142
- ai_card = draw_card(game_state['ai_cards'])
143
- winner = determine_winner(player_card, ai_card)
144
-
145
- if winner == 'player':
146
- game_state['player_cards'].extend([player_card, ai_card])
147
- game_state['player_score'] += 2
148
- elif winner == 'ai':
149
- game_state['ai_cards'].extend([player_card, ai_card])
150
- game_state['ai_score'] += 2
151
- else:
152
- game_state['player_cards'].append(player_card)
153
- game_state['ai_cards'].append(ai_card)
154
-
155
- game_state['rounds_played'] += 1
 
80
  winner = determine_winner(player_card, ai_card)
81
  if winner == 'player':
82
  st.write('You won this round!')
83
+ game_state['player_cards'].extend([
84
+ player_card, ai_card])
85
  game_state['player_score'] += 2
86
  elif winner == 'ai':
87
  st.write('Dealer won this round!')
 
94
 
95
  game_state['rounds_played'] += 1
96
 
97
+ # Save game state to file
98
+ with open('game_state.txt', 'w') as f:
99
+ f.write(str(game_state))
100
+
101
  st.sidebar.write('---')
102
  if st.sidebar.button('New Game'):
103
  # Reset game state
 
110
  game_state['player_cards'] = deck[:26]
111
  game_state['ai_cards'] = deck[26:]
112
 
113
+ # Save game state to file
114
+ with open('game_state.txt', 'w') as f:
115
+ f.write(str(game_state))
116
+
117
  if st.sidebar.button('Save'):
118
+ # Save game state to file
119
  with open('game_state.txt', 'w') as f:
120
  f.write(str(game_state))
121
 
122
  if st.sidebar.button('Reload'):
123
+ # Reload game state from file
124
  with open('game_state.txt', 'r') as f:
125
  game_state = eval(f.read())
126
 
127
+ # Show game history
128
+ st.write('# Game History')
129
+ if not st.checkbox('Show game history'):
130
+ return
131
+ with open('game_state.txt', 'r') as f:
132
+ lines = f.readlines()
133
+ headers = [header.strip() for header in lines[0].strip().split(',')]
134
+ data = [[cell.strip() for cell in line.strip().split(',')] for line in lines[1:]]
135
+ st.write(st.dataframe(data, columns=headers))
136
+
137
+ # Load game state from file
138
  with open('game_state.txt', 'r') as f:
139
  game_state = eval(f.read())
140
+
141
+ # Play the game
142
+ while game_state['rounds_played'] < NUM_ROUNDS and len(game_state['player_cards']) + len(game_state['ai_cards']) == 52:
143
+ game_ui()
144
+
145
+ player_card = draw_card(game_state['player_cards'])
146
+ ai_card = draw_card(game_state['ai_cards'])
147
+ winner = determine_winner(player_card, ai_card)
148
+
149
+ if winner == 'player':
150
+ game_state['player_cards'].extend([player_card, ai_card])
151
+ game_state['player_score'] += 2
152
+ elif winner == 'ai':
153
+ game_state['ai_cards'].extend([player_card, ai_card])
154
+ game_state['ai_score'] += 2
155
+ else:
156
+ game_state['player_cards'].append(player_card)
157
+ game_state['ai_cards'].append(ai_card)
158
+
159
+ game_state['rounds_played'] += 1
160
+
161
+ # Save game state to file
162
  with open('game_state.txt', 'w') as f:
163
  f.write(str(game_state))