awacke1 commited on
Commit
71fc926
1 Parent(s): 73199d5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +164 -0
app.py ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import csv
4
+ import huggingface_hub
5
+ from PIL import Image
6
+ from components import GamePlay, Player, Dealer, Deck
7
+ from huggingface_hub import Repository, hf_hub_download, upload_file
8
+ from datetime import datetime
9
+
10
+ DATASET_REPO_URL = "https://huggingface.co/datasets/awacke1/Carddata.csv"
11
+ DATASET_REPO_ID = "awacke1/Carddata.csv"
12
+ DATA_FILENAME = "Carddata.csv"
13
+ DATA_FILE = os.path.join("data", DATA_FILENAME)
14
+ HF_TOKEN = os.environ.get("HF_TOKEN")
15
+ number_of_decks = 6
16
+ blackjack_multiplier = 1.5
17
+
18
+ def generate_html() -> str:
19
+ with open(DATA_FILE) as csvfile:
20
+ reader = csv.DictReader(csvfile)
21
+ rows = []
22
+ for row in reader:
23
+ rows.append(row)
24
+ rows.reverse()
25
+ if len(rows) == 0:
26
+ return "no messages yet"
27
+ else:
28
+ html = "<div class='chatbot'>"
29
+ for row in rows:
30
+ html += "<div>"
31
+ html += f"<span>{row['name']}</span>"
32
+ html += f"<span class='message'>{row['message']}</span>"
33
+ html += "</div>"
34
+ html += "</div>"
35
+ return html
36
+
37
+ def store_message(name: str, message: str):
38
+ if name and message:
39
+ with open(DATA_FILE, "a") as csvfile:
40
+ writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
41
+ writer.writerow(
42
+ {"name": name, "message": message, "time": str(datetime.now())}
43
+ )
44
+ commit_url = repo.push_to_hub()
45
+ return generate_html()
46
+
47
+ # Initialize player, dealer, deck and game play. Cache these variables
48
+ @st.cache(allow_output_mutation=True, suppress_st_warning=True)
49
+ def start_game():
50
+ game_deck = Deck(number_of_decks)
51
+ dealer = Dealer()
52
+ player = Player()
53
+ game_play = GamePlay(player, dealer, game_deck, blackjack_multiplier)
54
+ return game_deck, dealer, player, game_play
55
+
56
+ game_deck, dealer, player, game_play = start_game()
57
+
58
+
59
+ def display_pro_tip(player, dealer):
60
+ player_total = sum(card.rank for card in player.cards)
61
+
62
+ if dealer.cards:
63
+ dealer_upcard = dealer.cards[0].rank
64
+ else:
65
+ dealer_upcard = 0
66
+
67
+ if player_total <= 11:
68
+ return "Pro Tip: With a total of 11 or less, it's generally advisable to hit."
69
+ elif player_total == 12 and dealer_upcard <= 3:
70
+ return "Pro Tip: With a total of 12 and the dealer showing 3 or less, consider hitting."
71
+ elif player_total >= 17:
72
+ return "Pro Tip: With a total of 17 or more, it's usually best to stand."
73
+ else:
74
+ return ""
75
+
76
+ def display_betting_strategy(player, dealer):
77
+ player_total = sum(card.rank for card in player.cards)
78
+
79
+ if dealer.cards:
80
+ dealer_upcard = dealer.cards[0].rank
81
+ else:
82
+ dealer_upcard = 0
83
+
84
+ if player_total <= 11:
85
+ return "Betting Strategy: Consider increasing your bet when your total is 11 or less."
86
+ elif player_total >= 17 and dealer_upcard <= 6:
87
+ return "Betting Strategy: Consider increasing your bet when you have a strong hand and the dealer has a weak upcard."
88
+ else:
89
+ return ""
90
+
91
+ def calculate_hand_total(hand):
92
+ total = sum(card.rank for card in hand)
93
+ return total
94
+
95
+ def calculate_probability(player_total, dealer_upcard):
96
+ # Placeholder probability calculation
97
+ # Replace with actual probability calculation based on player's total and dealer's upcard
98
+ probability = (21 - player_total) / (21 - dealer_upcard) * 100
99
+ return probability
100
+
101
+ st.title('🃏Blackjack Simulator AI♠2️⃣1️⃣')
102
+
103
+ if st.button('New hand?'):
104
+ game_play.deal_in()
105
+
106
+ player_stats = st.empty()
107
+ player_images = st.empty()
108
+ player_action = st.empty()
109
+ done_button = st.empty()
110
+ dealer_stats = st.empty()
111
+ dealer_images = st.empty()
112
+ result = st.empty()
113
+ pro_tip = st.empty()
114
+ betting_strategy = st.empty()
115
+ probability_table = st.empty()
116
+
117
+ while True:
118
+ game_play.update()
119
+ player_stats.write(player)
120
+ player_images.image([Image.open(card.image_location) for card in player.cards], width=100)
121
+ dealer_stats.write(dealer)
122
+ dealer_images.image([Image.open(card.image_location) for card in dealer.cards], width=100)
123
+ result.write(game_play)
124
+
125
+ pro_tip.write(display_pro_tip(player, dealer))
126
+ betting_strategy.write(display_betting_strategy(player, dealer))
127
+
128
+ player_total = calculate_hand_total(player.cards)
129
+ dealer_upcard = dealer.cards[0].rank if dealer.cards else 0
130
+
131
+ probability = calculate_probability(player_total, dealer_upcard)
132
+ probability_table.table(
133
+ [["Player's Total", "Dealer's Upcard", "Probability of Winning"]],
134
+ [[str(player_total), str(dealer_upcard), f"{probability:.2f}%"]]
135
+ )
136
+
137
+ if player_total == 21 or not player.possible_actions:
138
+ break
139
+
140
+ if 'Hit' in player.possible_actions or 'Double Down' in player.possible_actions or 'Stand' in player.possible_actions:
141
+ action = player_action.radio("Choose your action", ("👋 Hit", "⏬ Double Down", "🚫 Stand"), key=f"action_{len(player.cards)}")
142
+
143
+ if action == "👋 Hit":
144
+ player.player_hit(game_deck, game_play)
145
+ elif action == "⏬ Double Down":
146
+ player.double_down(game_deck, game_play)
147
+ elif action == "🚫 Stand":
148
+ player.stand(game_play)
149
+ break
150
+
151
+ if done_button.button("✅ Done", key=f"done_{len(player.cards)}"):
152
+ player.stand(game_play)
153
+ break
154
+
155
+ # Dealer's turn
156
+ while calculate_hand_total(dealer.cards) < 17:
157
+ dealer.dealer_hit(game_deck)
158
+
159
+ game_play.update()
160
+ player_stats.write(player)
161
+ player_images.image([Image.open(card.image_location) for card in player.cards], width=100)
162
+ dealer_stats.write(dealer)
163
+ dealer_images.image([Image.open(card.image_location) for card in dealer.cards], width=100)
164
+ result.write(game_play)