Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# write a python streamlit game with the rules of deal or no deal. Use session state to save values and a scoreboard to a CSV file. Allow users to save the game.
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
import pandas as pd
|
5 |
+
import random
|
6 |
+
|
7 |
+
# Set the session state
|
8 |
+
st.set_page_config(page_title="Deal or No Deal")
|
9 |
+
st.set_option('deprecation.showPyplotGlobalUse', False)
|
10 |
+
|
11 |
+
# Create the scoreboard
|
12 |
+
scoreboard = pd.DataFrame(columns=['Name', 'Score'])
|
13 |
+
|
14 |
+
# Create the game page
|
15 |
+
def game_page():
|
16 |
+
# Get the player's name
|
17 |
+
name = st.text_input('What is your name?')
|
18 |
+
|
19 |
+
# Create the game
|
20 |
+
st.header('Welcome to Deal or No Deal!')
|
21 |
+
st.write('Your goal is to select boxes with dollar amounts and try to get the highest score. Good luck!')
|
22 |
+
|
23 |
+
# Set the initial values
|
24 |
+
num_rounds = 5
|
25 |
+
round_num = 0
|
26 |
+
total_score = 0
|
27 |
+
boxes = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
|
28 |
+
box_values = [1,5,10,25,50,75,100,150,200,250,500,750,1000,1500,2000,2500,5000,7500,10000,25000]
|
29 |
+
random.shuffle(box_values)
|
30 |
+
st.write('There are ' + str(num_rounds) + ' rounds. You will pick ' + str(len(boxes)) + ' boxes.')
|
31 |
+
st.write('Round ' + str(round_num + 1) + ': Select a box to open')
|
32 |
+
|
33 |
+
# Create the boxes
|
34 |
+
for i in range(len(boxes)):
|
35 |
+
if boxes[i] in boxes:
|
36 |
+
st.write('Box ' + str(boxes[i]) + ' : $' + str(box_values[i]))
|
37 |
+
else:
|
38 |
+
boxes.remove(i)
|
39 |
+
|
40 |
+
# Get the player's selection
|
41 |
+
selection = st.selectbox('Which box would you like to open?', boxes)
|
42 |
+
box_value = box_values[boxes.index(selection)]
|
43 |
+
st.write('You have selected Box ' + str(selection) + ', which contains $' + str(box_value) + '.')
|
44 |
+
|
45 |
+
# Get the player's decision
|
46 |
+
decision = st.radio('Do you want to keep the box or trade it for the banker\'s offer?', ('Keep', 'Trade'))
|
47 |
+
|
48 |
+
# Calculate the score
|
49 |
+
if decision == 'Keep':
|
50 |
+
total_score += box_value
|
51 |
+
st.write('You have kept the box and earned $' + str(box_value) + '.')
|
52 |
+
else:
|
53 |
+
offer = random.randint(1,100)
|
54 |
+
st.write('The banker has offered you $' + str(offer) + '.')
|
55 |
+
decision = st.radio('Do you want to accept the offer or reject it?', ('Accept', 'Reject'))
|
56 |
+
if decision == 'Accept':
|
57 |
+
total_score += offer
|
58 |
+
st.write('You have accepted the offer and earned $' + str(offer) + '.')
|
59 |
+
else:
|
60 |
+
total_score += box_value
|
61 |
+
st.write('You have rejected the offer and kept your box, earning $' + str(box_value) + '.')
|
62 |
+
|
63 |
+
# Update the round number
|
64 |
+
round_num += 1
|
65 |
+
|
66 |
+
# Check if the game is over
|
67 |
+
if round_num < num_rounds:
|
68 |
+
st.write('Your total score is $' + str(total_score) + '.')
|
69 |
+
st.write('Round ' + str(round_num + 1) + ': Select a box to open')
|
70 |
+
game_page()
|
71 |
+
else:
|
72 |
+
st.write('Congratulations, you have finished the game with a total score of $' + str(total_score) + '!')
|
73 |
+
|
74 |
+
# Save the player's score to the scoreboard
|
75 |
+
scoreboard.loc[len(scoreboard)] = [name, total_score]
|
76 |
+
|
77 |
+
# Save the scoreboard to a CSV file
|
78 |
+
st.write('Saving your score...')
|
79 |
+
scoreboard.to_csv('scoreboard.csv', index=False)
|
80 |
+
st.write('Score saved!')
|
81 |
+
|
82 |
+
# Create the main page
|
83 |
+
def main():
|
84 |
+
st.title('Deal or No Deal')
|
85 |
+
# Show the scoreboard
|
86 |
+
st.header('Scoreboard')
|
87 |
+
st.dataframe(scoreboard)
|
88 |
+
|
89 |
+
# Show the game page
|
90 |
+
st.header('Game')
|
91 |
+
if st.button('Start Game'):
|
92 |
+
game_page()
|
93 |
+
|
94 |
+
main()
|