Spaces:
Runtime error
Runtime error
File size: 6,044 Bytes
88bab31 ecc7544 88bab31 39bd597 1cc2da1 39bd597 88bab31 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
import os
import streamlit as st
import numpy as np
import pandas as pd
import plotly.express as px
st.set_page_config(layout='wide')
st.title('๐ฒ Random Dice Game')
dice_types = [{'name': 'Six-sided Dice', 'sides': 6, 'emoji': '๐ฒ'},
{'name': 'Twenty-sided Dice', 'sides': 20, 'emoji': '๐ฏ'},
{'name': 'Thirty-sided Dice', 'sides': 30, 'emoji': '๐ฏ'},
{'name': 'One Hundred-sided Dice', 'sides': 100, 'emoji': '๐ฏ'}]
if 'username' not in st.session_state:
st.session_state.username = ''
if 'dice_roll_history' not in st.session_state:
st.session_state.dice_roll_history = pd.DataFrame()
def username_input():
st.text_input('๐ค Username', key='username')
def display_dice_roll_distribution():
dice_type = st.selectbox('๐ฒ Choose a type of dice', dice_types, format_func=lambda d: f"{d['name']} {d['emoji']}")
num_rolls = st.slider('๐ How many times do you want to roll the dice?', 1, 1000000, 1000)
rolls = np.random.randint(1, dice_type['sides'] + 1, num_rolls, dtype=np.uint64)
roll_counts = pd.Series(rolls).value_counts().sort_index()
fig = px.sunburst(names=[f'Roll {i}' for i in roll_counts.index],
parents=['Dice Rolls'] * dice_type['sides'],
values=roll_counts.values,
color=[f'Roll {i}' for i in roll_counts.index],
color_discrete_sequence=px.colors.qualitative.Dark24,
maxdepth=2)
fig.update_layout(title='๐ Dice Roll Distribution', margin=dict(l=20, r=20, t=40, b=20), width=800, height=600)
show_labels = st.checkbox('๐ท๏ธ Show Labels', value=True)
if not show_labels:
fig.update_traces(textinfo='none')
fig.show()
return dice_type, rolls
def display_bonus_match(bonus_match, bonus_dice_type, bonus_dice_emoji):
if bonus_match:
st.success(f'๐ You rolled a {bonus_dice_type} {bonus_dice_emoji} on the first roll!')
def update_dice_roll_history(dice_type, rolls):
history = st.session_state.dice_roll_history
count = st.session_state.count
dice_name = dice_type['name']
dice_emoji = dice_type['emoji']
bonus_match = None
# Check if the count matches the number of sides of the dice
if count > 0 and count % dice_type['sides'] == 0:
bonus_match = dice_type['name']
bonus_emoji = dice_type['emoji']
# Update the dice roll history
if history is None:
history = pd.DataFrame({'Roll': rolls, 'Count': count, 'DiceNumberOfSides': dice_type['sides']})
else:
new_history = pd.DataFrame({'Roll': rolls, 'Count': count, 'DiceNumberOfSides': dice_type['sides']})
history = history.append(new_history, ignore_index=True)
if bonus_match is not None:
history['BonusMatchToDiceName'] = bonus_match
history['BonusMatchToDiceEmoji'] = bonus_emoji
# Add username to history
history['Username'] = st.session_state.username
st.session_state.dice_roll_history = history
return history
def display_dice_roll_results():
download_history = st.checkbox('๐ฅ Download Roll History', value=True)
if download_history:
csv_filename = f'dice_roll_history_{st.session_state.username}.csv'
st.markdown(f'๐ฅ [Download roll history]({csv_filename})')
dice_roll_history = st.session_state.dice_roll_history[['Roll', 'Count', 'DiceNumberOfSides']]
if st.checkbox('๐ Show Bonus Matches', value=True):
dice_roll_history = dice_roll_history.join(
st.session_state.dice_roll_history[['BonusMatchToDiceName', 'BonusMatchToDiceEmoji']])
if st.checkbox('๐ค Show Username', value=True):
dice_roll_history['Username'] = st.session_state.dice_roll_history['Username']
dice_roll_history.to_csv(csv_filename, index=False)
dice_roll_history_files = [f for f in os.listdir() if f.startswith('dice_roll_history_') and f.endswith('.csv')]
if len(dice_roll_history_files) > 0:
st.write('๐ Roll History Files:')
for history_file in dice_roll_history_files:
st.markdown(f'[Download {history_file}]({history_file})')
st.sidebar.title('๐ง Settings')
username_input()
if st.button('๐ฒ Roll Dice'):
dice_type, rolls = display_dice_roll_distribution()
display_bonus_match(update_dice_roll_history(dice_type, rolls), dice_type['name'], dice_type['emoji'])
display_dice_roll_results()
st.write("""
๐ Bread, ๐ฅ Croissant, ๐ฅ Baguette Bread, ๐ซ Flatbread, ๐ฅจ Pretzel, ๐ฅฏ Bagel, ๐ฅ Pancakes, ๐ง Waffle, ๐ง Cheese Wedge, ๐ Meat on Bone, ๐ Poultry Leg, ๐ฅฉ Cut of Meat, ๐ฅ Bacon, ๐ Hamburger, ๐ French Fries, ๐ Pizza, ๐ญ Hot Dog, ๐ฅช Sandwich, ๐ฎ Taco, ๐ฏ Burrito, ๐ซ Tamale, ๐ฅ Stuffed Flatbread, ๐ง Falafel, ๐ฅ Egg, ๐ณ Cooking, ๐ฅ Shallow Pan of Food, ๐ฒ Pot of Food, ๐ซ Fondue, ๐ฅฃ Bowl with Spoon, ๐ฅ Green Salad, ๐ฟ Popcorn, ๐ง Butter, ๐ง Salt, ๐ฅซ Canned Food, ๐ฑ Bento Box, ๐ Rice Cracker, ๐ Rice Ball, ๐ Cooked Rice, ๐ Curry Rice, ๐ Steaming Bowl, ๐ Spaghetti, ๐ Roasted Sweet Potato, ๐ข Oden, ๐ฃ Sushi, ๐ค Fried Shrimp, ๐ฅ Fish Cake with Swirl, ๐ฅฎ Moon Cake, ๐ก Dango, ๐ฅ Dumpling, ๐ฅ Fortune Cookie, ๐ฅก Takeout Box, ๐ฆช Oyster, ๐ฆ Soft Ice Cream, ๐ง Shaved Ice, ๐จ Ice Cream, ๐ฉ Doughnut, ๐ช Cookie, ๐ Birthday Cake, ๐ฐ Shortcake, ๐ง Cupcake, ๐ฅง Pie, ๐ซ Chocolate Bar, ๐ฌ Candy, ๐ญ Lollipop, ๐ฎ Custard, ๐ฏ Honey Pot, ๐ผ Baby Bottle, ๐ฅ Glass of Milk, โ Hot Beverage, ๐ซ Teapot, ๐ต Teacup Without Handle, ๐ถ Sake, ๐พ Bottle with Popping Cork, ๐ท Wine Glass, ๐ธ Cocktail Glass, ๐น Tropical Drink, ๐บ Beer Mug, ๐ป Clinking Beer Mugs, ๐ฅ Clinking Glasses, ๐ฅ Tumbler Glass, ๐ซ Pouring Liquid, ๐ฅค Cup with Straw, ๐ง Bubble Tea, ๐ง Beverage Box, ๐ง Mate, ๐ง Ice, ๐ฅข Chopsticks, ๐ฝ๏ธ Fork and Knife with Plate, ๐ด Fork and Knife, ๐ฅ Spoon, ๐ซ Jar")
""")
|