awacke1 commited on
Commit
a61c8c2
·
1 Parent(s): bd783b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -22
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import random
2
  import streamlit as st
3
 
@@ -80,13 +81,13 @@ 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([
84
- player_card, ai_card])
85
  game_state['player_score'] += 2
86
  elif winner == 'ai':
87
  st.write('Dealer won this round!')
88
  game_state['ai_cards'].extend([player_card, ai_card])
89
  game_state['ai_score'] += 2
 
90
  else:
91
  st.write('Tie!')
92
  game_state['player_cards'].append(player_card)
@@ -96,7 +97,9 @@ def game_ui():
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'):
@@ -112,17 +115,26 @@ def game_ui():
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')
@@ -134,11 +146,8 @@ def game_ui():
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
 
@@ -149,15 +158,4 @@ while game_state['rounds_played'] < NUM_ROUNDS and len(game_state['player_cards'
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))
 
1
+ import os
2
  import random
3
  import streamlit as st
4
 
 
81
  winner = determine_winner(player_card, ai_card)
82
  if winner == 'player':
83
  st.write('You won this round!')
84
+ game_state['player_cards'].extend([player_card, ai_card])
 
85
  game_state['player_score'] += 2
86
  elif winner == 'ai':
87
  st.write('Dealer won this round!')
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)
 
97
 
98
  # Save game state to file
99
  with open('game_state.txt', 'w') as f:
100
+ if not os.path.exists('game_state.txt'):
101
+ f.write('player_cards,ai_cards,player_score,ai_score,rounds_played\n')
102
+ f.write(','.join([str(game_state[key]) for key in game_state.keys()]) + '\n')
103
 
104
  st.sidebar.write('---')
105
  if st.sidebar.button('New Game'):
 
115
 
116
  # Save game state to file
117
  with open('game_state.txt', 'w') as f:
118
+ f.write('player_cards,ai_cards,player_score,ai_score,rounds_played\n')
119
+ f.write(','.join([str(game_state[key]) for key in game_state.keys()]) + '\n')
120
 
121
  if st.sidebar.button('Save'):
122
  # Save game state to file
123
  with open('game_state.txt', 'w') as f:
124
+ if not os.path.exists('game_state.txt'):
125
+ f.write('player_cards,ai_cards,player_score,ai_score,rounds_played\n')
126
+ f.write(','.join([str(game_state[key]) for key in game_state.keys()]) + '\n')
127
 
128
  if st.sidebar.button('Reload'):
129
  # Reload game state from file
130
+ game_state = {'player_cards': [], 'ai_cards': [], 'player_score': 0, 'ai_score': 0, 'rounds_played': 0}
131
  with open('game_state.txt', 'r') as f:
132
+ headers = f.readline().strip().split(',')
133
+ data = f.readlines()
134
+ if len(data) > 0:
135
+ last_line = data[-1].strip().split(',')
136
+ for i in range(len(headers)):
137
+ game_state[headers[i]] = eval(last_line[i])
138
 
139
  # Show game history
140
  st.write('# Game History')
 
146
  data = [[cell.strip() for cell in line.strip().split(',')] for line in lines[1:]]
147
  st.write(st.dataframe(data, columns=headers))
148
 
 
 
 
 
149
  # Play the game
150
+ game_state = {'player_cards': [], 'ai_cards': [], 'player_score': 0, 'ai_score': 0, 'rounds_played': 0}
151
  while game_state['rounds_played'] < NUM_ROUNDS and len(game_state['player_cards']) + len(game_state['ai_cards']) == 52:
152
  game_ui()
153
 
 
158
  if winner == 'player':
159
  game_state['player_cards'].extend([player_card, ai_card])
160
  game_state['player_score'] += 2
161
+ elif winner == '