awacke1 commited on
Commit
67774b5
·
verified ·
1 Parent(s): 778539c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -40
app.py CHANGED
@@ -1,12 +1,11 @@
1
- import streamlit as st
2
- from PIL import Image
3
- from components import GamePlay, Player, Dealer, Deck
4
 
5
- # PersistDataset -----
 
6
  import os
7
  import csv
8
-
9
  import huggingface_hub
 
 
10
  from huggingface_hub import Repository, hf_hub_download, upload_file
11
  from datetime import datetime
12
 
@@ -15,7 +14,8 @@ DATASET_REPO_ID = "awacke1/Carddata.csv"
15
  DATA_FILENAME = "Carddata.csv"
16
  DATA_FILE = os.path.join("data", DATA_FILENAME)
17
  HF_TOKEN = os.environ.get("HF_TOKEN")
18
- # overriding/appending to the gradio template
 
19
 
20
  def generate_html() -> str:
21
  with open(DATA_FILE) as csvfile:
@@ -45,10 +45,6 @@ def store_message(name: str, message: str):
45
  )
46
  commit_url = repo.push_to_hub()
47
  return generate_html()
48
-
49
- # Game settings
50
- number_of_decks = 6
51
- blackjack_multiplier = 1.5
52
 
53
  # Initialize player, dealer, deck and game play. Cache these variables
54
  @st.cache(allow_output_mutation=True, suppress_st_warning=True)
@@ -61,6 +57,42 @@ def start_game():
61
 
62
  game_deck, dealer, player, game_play = start_game()
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  st.title('🃏Blackjack Simulator AI♠2️⃣1️⃣')
65
 
66
  if st.button('New hand?'):
@@ -68,39 +100,42 @@ if st.button('New hand?'):
68
 
69
  player_stats = st.empty()
70
  player_images = st.empty()
71
- player_hit_option = st.empty()
72
- player_double_down_option = st.empty()
73
- player_stand_option = st.empty()
74
  dealer_stats = st.empty()
75
  dealer_images = st.empty()
76
  result = st.empty()
 
 
 
 
 
 
 
 
 
 
77
 
 
 
78
 
79
- if 'Hit' in player.possible_actions:
80
- if player_hit_option.button('Hit'):
81
- player.player_hit(game_deck, game_play)
82
- if 'Hit' not in player.possible_actions:
83
- player_hit_option.empty()
84
- if 'Double Down' in player.possible_actions:
85
- if player_double_down_option.button('Double Down'):
86
- player.double_down(game_deck, game_play)
87
- player_double_down_option.empty()
88
- player_hit_option.empty()
89
- player_stand_option.empty()
90
- if 'Stand' in player.possible_actions:
91
- if player_stand_option.button('Stand'):
 
 
 
 
92
  player.stand(game_play)
93
- player_hit_option.empty()
94
- player_double_down_option.empty()
95
- player_stand_option.empty()
96
-
97
-
98
- game_play.update()
99
- player_stats.write(player)
100
- player_images.image([Image.open(card.image_location)
101
- for card in player.cards], width=100)
102
- dealer_stats.write(dealer)
103
- dealer_images.image([Image.open(card.image_location)
104
- for card in dealer.cards], width=100)
105
-
106
- result.write(game_play)
 
 
 
 
1
 
2
+
3
+ import streamlit as st
4
  import os
5
  import csv
 
6
  import huggingface_hub
7
+ from PIL import Image
8
+ from components import GamePlay, Player, Dealer, Deck
9
  from huggingface_hub import Repository, hf_hub_download, upload_file
10
  from datetime import datetime
11
 
 
14
  DATA_FILENAME = "Carddata.csv"
15
  DATA_FILE = os.path.join("data", DATA_FILENAME)
16
  HF_TOKEN = os.environ.get("HF_TOKEN")
17
+ number_of_decks = 6
18
+ blackjack_multiplier = 1.5
19
 
20
  def generate_html() -> str:
21
  with open(DATA_FILE) as csvfile:
 
45
  )
46
  commit_url = repo.push_to_hub()
47
  return generate_html()
 
 
 
 
48
 
49
  # Initialize player, dealer, deck and game play. Cache these variables
50
  @st.cache(allow_output_mutation=True, suppress_st_warning=True)
 
57
 
58
  game_deck, dealer, player, game_play = start_game()
59
 
60
+ def display_pro_tip(player, dealer):
61
+ player_total = sum(card.rank for card in player.cards)
62
+
63
+ if dealer.cards:
64
+ dealer_upcard = dealer.cards[0].rank
65
+ else:
66
+ dealer_upcard = 0
67
+
68
+ if player_total <= 11:
69
+ return "Pro Tip: With a total of 11 or less, it's generally advisable to hit."
70
+ elif player_total == 12 and dealer_upcard <= 3:
71
+ return "Pro Tip: With a total of 12 and the dealer showing 3 or less, consider hitting."
72
+ elif player_total >= 17:
73
+ return "Pro Tip: With a total of 17 or more, it's usually best to stand."
74
+ else:
75
+ return ""
76
+
77
+ def display_betting_strategy(player, dealer):
78
+ player_total = sum(card.rank for card in player.cards)
79
+
80
+ if dealer.cards:
81
+ dealer_upcard = dealer.cards[0].rank
82
+ else:
83
+ dealer_upcard = 0
84
+
85
+ if player_total <= 11:
86
+ return "Betting Strategy: Consider increasing your bet when your total is 11 or less."
87
+ elif player_total >= 17 and dealer_upcard <= 6:
88
+ return "Betting Strategy: Consider increasing your bet when you have a strong hand and the dealer has a weak upcard."
89
+ else:
90
+ return ""
91
+
92
+ def calculate_hand_total(hand):
93
+ total = sum(card.rank for card in hand)
94
+ return total
95
+
96
  st.title('🃏Blackjack Simulator AI♠2️⃣1️⃣')
97
 
98
  if st.button('New hand?'):
 
100
 
101
  player_stats = st.empty()
102
  player_images = st.empty()
103
+ player_action = st.empty()
104
+ done_button = st.empty()
 
105
  dealer_stats = st.empty()
106
  dealer_images = st.empty()
107
  result = st.empty()
108
+ pro_tip = st.empty()
109
+ betting_strategy = st.empty()
110
+
111
+ while True:
112
+ game_play.update()
113
+ player_stats.write(player)
114
+ player_images.image([Image.open(card.image_location) for card in player.cards], width=100)
115
+ dealer_stats.write(dealer)
116
+ dealer_images.image([Image.open(card.image_location) for card in dealer.cards], width=100)
117
+ result.write(game_play)
118
 
119
+ pro_tip.write(display_pro_tip(player, dealer))
120
+ betting_strategy.write(display_betting_strategy(player, dealer))
121
 
122
+ player_total = calculate_hand_total(player.cards)
123
+ dealer_total = calculate_hand_total(dealer.cards)
124
+
125
+ if player_total == 21 or dealer_total == 21 or not player.possible_actions:
126
+ break
127
+
128
+ if 'Hit' in player.possible_actions or 'Double Down' in player.possible_actions:
129
+ action = player_action.radio("Choose your action", ("👋 Hit", "⏬ Double Down"), key=f"action_{len(player.cards)}")
130
+
131
+ if action == "👋 Hit":
132
+ player.player_hit(game_deck, game_play)
133
+ elif action == "⏬ Double Down":
134
+ player.double_down(game_deck, game_play)
135
+
136
+ if done_button.button("✅ Done", key=f"done_{len(player.cards)}"):
137
+ player.stand(game_play)
138
+ else:
139
  player.stand(game_play)
140
+
141
+