Spaces:
Runtime error
Runtime error
import streamlit as st | |
import numpy as np | |
import plotly.graph_objects as go | |
# Define emoji list | |
EMOJI_LIST = { | |
4: "π", | |
6: "π", | |
8: "π", | |
10: "π", | |
12: "π", | |
20: "π", | |
50: "π", | |
100: "π" | |
} | |
# Define the dice types | |
DICE_TYPES = [4, 6, 8, 10, 12, 20, 50, 100] | |
# Define the default number of rolls | |
DEFAULT_ROLLS = 100 | |
# Define session state | |
if "username" not in st.session_state: | |
st.session_state.username = "" | |
if "rolls" not in st.session_state: | |
st.session_state.rolls = DEFAULT_ROLLS | |
if "history" not in st.session_state: | |
st.session_state.history = {} | |
# 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 | |
st.write("Enter your username:") | |
st.session_state.username = st.text_input("Username", st.session_state.username) | |
# Get number of rolls | |
st.write("Choose the number of rolls:") | |
st.session_state.rolls = st.slider("Number of Rolls", 1, 1000000, st.session_state.rolls) | |
# Get dice types and roll dice | |
for dice_type in DICE_TYPES: | |
rolls = roll_dice(st.session_state.rolls, dice_type) | |
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.") | |
if "coin_tokens" not in st.session_state.history: | |
st.session_state.history["coin_tokens"] = [0] | |
st.session_state.history["coin_tokens"].append(st.session_state.history["coin_tokens"][-1] + 10) | |
if "roll_history" not in st.session_state.history: | |
st.session_state.history["roll_history"] = {} | |
st.session_state.history["roll_history"][dice_type] = rolls | |
# Plot tokens | |
if "health_tokens" not in st.session_state.history: | |
st.session_state.history["health_tokens"] = [0] | |
if "coin_tokens" not in st.session_state.history: | |
st.session_state.history["coin_tokens"] = [0] | |
st.write("Token Accumulation:") | |
plot_tokens(st.session_state.history["health_tokens"], st | |