Dice-Roll-Fractals-STEM-Math / Backup-app-pyBeforeDownload.py
awacke1's picture
Update Backup-app-pyBeforeDownload.py
0837158
raw
history blame
2.86 kB
import streamlit as st
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from datetime import datetime
from base64 import b64encode
# Define emoji list
EMOJI_LIST = {4: "πŸŽ‚", 6: "πŸ€", 8: "πŸ„", 10: "🍁", 12: "πŸ‚", 20: "πŸƒ", 50: "πŸ’", 100: "🌟"}
# Define the dice types and default number of rolls
DICE_TYPES = [4, 6, 8, 10, 12, 20, 50, 100]
DEFAULT_ROLLS = 3
# Define a function to roll dice
def roll_dice(num_rolls, dice_type):
rolls = np.random.randint(1, dice_type + 1, size=num_rolls)
return rolls
# Define a function to plot tokens
def plot_tokens(health_tokens, coin_tokens):
fig = go.Figure()
fig.add_trace(go.Scatter(x=list(range(1, len(health_tokens) + 1)), y=health_tokens, name="Health"))
fig.add_trace(go.Scatter(x=list(range(1, len(coin_tokens) + 1)), y=coin_tokens, name="Coins"))
fig.update_layout(title="Token Accumulation", xaxis_title="Rolls", yaxis_title="Tokens")
st.plotly_chart(fig)
# Define the app
st.title("Dice Rolling Game")
# Get username and number of rolls
username = st.text_input("Enter your username:")
num_rolls = st.slider("Choose the number of rolls:", 1, 100, DEFAULT_ROLLS)
# Roll dice for each type and accumulate high rolls and tokens
history = {"health_tokens": [0], "coin_tokens": [0]}
for dice_type in DICE_TYPES:
rolls = roll_dice(num_rolls, dice_type)
highest_rolls = sum(roll == dice_type for roll in rolls)
st.write(f"Results for {dice_type}-sided dice:")
for roll in rolls:
st.write(f"{EMOJI_LIST[dice_type]} {roll}")
if roll == dice_type:
st.write("Congratulations! You rolled the highest value!")
if dice_type == 100:
st.write("Adding 10 coins for rolling over 90 on 100-sided dice.")
history["coin_tokens"].append(history["coin_tokens"][-1] + 10)
history[f"{dice_type}-sided dice high rolls"] = highest_rolls
history["roll_history"] = {**history.get("roll_history", {}), dice_type: rolls}
history["health_tokens"].append(history.get("20-sided dice high rolls", 0))
history["coin_tokens"].append(history.get("100-sided dice high rolls", 0))
# Plot token accumulation
st.write("Token Accumulation:")
plot_tokens(history["health_tokens"], history["coin_tokens"])
# Create DataFrame and save to CSV file
df = pd.concat([pd.DataFrame(history["roll_history"]), pd.DataFrame(history["health_tokens"], columns=["Health Tokens"]), pd.DataFrame(history["coin_tokens"], columns=["Coin Tokens"])], axis=1)
timestamp = datetime.now().strftime("%m-%d-%Y-%H-%M-%S")
filename = f"{username}_{timestamp}.csv"
df.to_csv(filename, index=False)
# Show download link for CSV file
st.markdown(f'<a href="data:file/csv;base64,{b64encode(open(filename, "rb").read()).decode()}" download="{filename}">Download CSV File</a>', unsafe_allow_html=True)