awacke1 commited on
Commit
9f66be1
ยท
1 Parent(s): d501fab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -20
app.py CHANGED
@@ -1,39 +1,44 @@
1
  import streamlit as st
2
  import numpy as np
3
  import pandas as pd
4
- import pygraphviz as pgv
5
  from datetime import datetime
6
  from base64 import b64encode
7
 
 
8
  FOOD_LIST = {4: "๐Ÿ”", 6: "๐ŸŸ", 8: "๐ŸŒฎ", 10: "๐Ÿ•", 12: "๐Ÿฉ", 20: "๐Ÿฅ—", 50: "๐Ÿฃ", 100: "๐Ÿพ"}
9
- DICE_TYPES = [4, 6, 8, 10, 12, 20, 50, 100]
10
- DEFAULT_ROLLS = 3
11
 
12
  def roll_dice(num_rolls, dice_type):
13
  rolls = np.random.randint(1, dice_type + 1, size=num_rolls)
14
  return rolls
15
 
16
  def plot_tokens(health_tokens, coin_tokens):
17
- dot = pgv.AGraph(strict=False, directed=True)
18
- dot.node_attr['shape'] = 'plaintext'
19
- dot.edge_attr['arrowhead'] = 'none'
20
-
21
- health_foods = [FOOD_LIST[i] for i in health_tokens]
22
- coin_foods = [FOOD_LIST[i] for i in coin_tokens]
23
-
24
- health_tokens_str = " | ".join(health_foods)
25
- coin_tokens_str = " | ".join(coin_foods)
26
-
27
- dot.add_edge('Health', health_tokens_str)
28
- dot.add_edge('Coins', coin_tokens_str)
29
-
30
- st.graphviz_chart(dot.string())
31
 
 
 
32
  st.title("๐ŸŽฒ Slot Machine Game")
33
- username = st.text_input("๐Ÿ‘ค Enter your username:")
34
- num_rolls = st.slider("๐Ÿ”ข Choose the number of rolls:", 1, 100, DEFAULT_ROLLS)
35
 
 
 
 
 
 
 
36
  history = {"health_tokens": [0], "coin_tokens": [0]}
 
37
  for dice_type in DICE_TYPES:
38
  rolls = roll_dice(num_rolls, dice_type)
39
  highest_rolls = sum(roll == dice_type for roll in rolls)
@@ -55,10 +60,10 @@ for dice_type in DICE_TYPES:
55
  history["roll_history"] = {**history.get("roll_history", {}), dice_type: rolls}
56
  history["coin_tokens"].append(history["coin_tokens"][-1] + coin_tokens_added)
57
 
58
- st.write("๐Ÿ’ฐ๐Ÿ’– Token Accumulation:")
59
  plot_tokens(history["health_tokens"], history["coin_tokens"])
60
 
61
  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)
 
62
  timestamp = datetime.now().strftime("%m-%d-%Y-%H-%M-%S")
63
  filename = f"{username}_{timestamp}.csv"
64
  df.to_csv(filename, index=False)
 
1
  import streamlit as st
2
  import numpy as np
3
  import pandas as pd
4
+ import plotly.graph_objects as go
5
  from datetime import datetime
6
  from base64 import b64encode
7
 
8
+ # Define general functions
9
  FOOD_LIST = {4: "๐Ÿ”", 6: "๐ŸŸ", 8: "๐ŸŒฎ", 10: "๐Ÿ•", 12: "๐Ÿฉ", 20: "๐Ÿฅ—", 50: "๐Ÿฃ", 100: "๐Ÿพ"}
 
 
10
 
11
  def roll_dice(num_rolls, dice_type):
12
  rolls = np.random.randint(1, dice_type + 1, size=num_rolls)
13
  return rolls
14
 
15
  def plot_tokens(health_tokens, coin_tokens):
16
+ fig = go.Figure()
17
+ fig.add_trace(go.Sankey(
18
+ node = {
19
+ "label": ["Health", "Coins"] + [FOOD_LIST[i] for i in DICE_TYPES],
20
+ "pad": 15
21
+ },
22
+ link = {
23
+ "source": [0, 1] + list(range(2, len(DICE_TYPES) + 2)),
24
+ "target": [2] * len(DICE_TYPES) + [3 + i for i in range(len(DICE_TYPES))],
25
+ "value": health_tokens + coin_tokens
26
+ },
27
+ ))
28
+ st.plotly_chart(fig)
 
29
 
30
+ # Define Streamlit app
31
+ st.set_page_config(page_title="Slot Machine Game", page_icon=":game_die:")
32
  st.title("๐ŸŽฒ Slot Machine Game")
 
 
33
 
34
+ # Sidebar
35
+ username = st.sidebar.text_input("๐Ÿ‘ค Enter your username:")
36
+ num_rolls = st.sidebar.slider("๐Ÿ”ข Choose the number of rolls:", 1, 100, 3)
37
+
38
+ # Main content
39
+ DICE_TYPES = [4, 6, 8, 10, 12, 20, 50, 100]
40
  history = {"health_tokens": [0], "coin_tokens": [0]}
41
+
42
  for dice_type in DICE_TYPES:
43
  rolls = roll_dice(num_rolls, dice_type)
44
  highest_rolls = sum(roll == dice_type for roll in rolls)
 
60
  history["roll_history"] = {**history.get("roll_history", {}), dice_type: rolls}
61
  history["coin_tokens"].append(history["coin_tokens"][-1] + coin_tokens_added)
62
 
 
63
  plot_tokens(history["health_tokens"], history["coin_tokens"])
64
 
65
  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)
66
+
67
  timestamp = datetime.now().strftime("%m-%d-%Y-%H-%M-%S")
68
  filename = f"{username}_{timestamp}.csv"
69
  df.to_csv(filename, index=False)