import streamlit as st import numpy as np import pandas as pd import pygraphviz as pgv from datetime import datetime from base64 import b64encode FOOD_LIST = {4: "🍔", 6: "🍟", 8: "🌮", 10: "🍕", 12: "🍩", 20: "🥗", 50: "🍣", 100: "🍾"} DICE_TYPES = [4, 6, 8, 10, 12, 20, 50, 100] DEFAULT_ROLLS = 3 def roll_dice(num_rolls, dice_type): rolls = np.random.randint(1, dice_type + 1, size=num_rolls) return rolls def plot_tokens(health_tokens, coin_tokens): dot = pgv.AGraph(strict=False, directed=True) dot.node_attr['shape'] = 'plaintext' dot.edge_attr['arrowhead'] = 'none' health_foods = [FOOD_LIST[i] for i in health_tokens] coin_foods = [FOOD_LIST[i] for i in coin_tokens] health_tokens_str = " | ".join(health_foods) coin_tokens_str = " | ".join(coin_foods) dot.add_edge('Health', health_tokens_str) dot.add_edge('Coins', coin_tokens_str) st.graphviz_chart(dot.string()) st.title("🎲 Slot Machine Game") username = st.text_input("👤 Enter your username:") num_rolls = st.slider("🔢 Choose the number of rolls:", 1, 100, DEFAULT_ROLLS) 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) coin_tokens_added = 0 dice_results = [f"{FOOD_LIST[dice_type]} {roll}" for roll in rolls] st.write(f"🎰 Results for {dice_type}-sided slot machine: {' | '.join(dice_results)}") for roll in rolls: if roll == dice_type: st.write(f"🎉 Congratulations! You got the {FOOD_LIST[dice_type]} jackpot! 💰 Adding 3 coins.") coin_tokens_added += 3 if roll == max(rolls): st.write(f"🎉 Congratulations! You got the {FOOD_LIST[dice_type]} maximum value! 💖 Adding 10 health tokens.") if dice_type == 100: history["health_tokens"].append(history["health_tokens"][-1] + 10) history[f"{dice_type}-sided slot machine jackpots"] = highest_rolls history["roll_history"] = {**history.get("roll_history", {}), dice_type: rolls} history["coin_tokens"].append(history["coin_tokens"][-1] + coin_tokens_added) st.write("💰💖 Token Accumulation:") plot_tokens(history["health_tokens"], history["coin_tokens"]) 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) st.markdown(f'Download CSV File', unsafe_allow_html=True)