awacke1's picture
Create app.py
58f61cd
# 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.
import streamlit as st
import pandas as pd
import random
# Set the session state
st.set_page_config(page_title="Deal or No Deal")
st.set_option('deprecation.showPyplotGlobalUse', False)
# Create the scoreboard
scoreboard = pd.DataFrame(columns=['Name', 'Score'])
# Create the game page
def game_page():
# Get the player's name
name = st.text_input('What is your name?')
# Create the game
st.header('Welcome to Deal or No Deal!')
st.write('Your goal is to select boxes with dollar amounts and try to get the highest score. Good luck!')
# Set the initial values
num_rounds = 5
round_num = 0
total_score = 0
boxes = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
box_values = [1,5,10,25,50,75,100,150,200,250,500,750,1000,1500,2000,2500,5000,7500,10000,25000]
random.shuffle(box_values)
st.write('There are ' + str(num_rounds) + ' rounds. You will pick ' + str(len(boxes)) + ' boxes.')
st.write('Round ' + str(round_num + 1) + ': Select a box to open')
# Create the boxes
for i in range(len(boxes)):
if boxes[i] in boxes:
st.write('Box ' + str(boxes[i]) + ' : $' + str(box_values[i]))
else:
boxes.remove(i)
# Get the player's selection
selection = st.selectbox('Which box would you like to open?', boxes)
box_value = box_values[boxes.index(selection)]
st.write('You have selected Box ' + str(selection) + ', which contains $' + str(box_value) + '.')
# Get the player's decision
decision = st.radio('Do you want to keep the box or trade it for the banker\'s offer?', ('Keep', 'Trade'))
# Calculate the score
if decision == 'Keep':
total_score += box_value
st.write('You have kept the box and earned $' + str(box_value) + '.')
else:
offer = random.randint(1,100)
st.write('The banker has offered you $' + str(offer) + '.')
decision = st.radio('Do you want to accept the offer or reject it?', ('Accept', 'Reject'))
if decision == 'Accept':
total_score += offer
st.write('You have accepted the offer and earned $' + str(offer) + '.')
else:
total_score += box_value
st.write('You have rejected the offer and kept your box, earning $' + str(box_value) + '.')
# Update the round number
round_num += 1
# Check if the game is over
if round_num < num_rounds:
st.write('Your total score is $' + str(total_score) + '.')
st.write('Round ' + str(round_num + 1) + ': Select a box to open')
game_page()
else:
st.write('Congratulations, you have finished the game with a total score of $' + str(total_score) + '!')
# Save the player's score to the scoreboard
scoreboard.loc[len(scoreboard)] = [name, total_score]
# Save the scoreboard to a CSV file
st.write('Saving your score...')
scoreboard.to_csv('scoreboard.csv', index=False)
st.write('Score saved!')
# Create the main page
def main():
st.title('Deal or No Deal')
# Show the scoreboard
st.header('Scoreboard')
st.dataframe(scoreboard)
# Show the game page
st.header('Game')
if st.button('Start Game'):
game_page()
main()