Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# ... (same as before)
|
20 |
+
|
21 |
+
def store_message(name: str, message: str):
|
22 |
+
# ... (same as before)
|
23 |
+
|
24 |
+
# Initialize player, dealer, deck and game play. Cache these variables
|
25 |
+
@st.cache(allow_output_mutation=True, suppress_st_warning=True)
|
26 |
+
def start_game():
|
27 |
+
game_deck = Deck(number_of_decks)
|
28 |
+
dealer = Dealer()
|
29 |
+
player = Player()
|
30 |
+
game_play = GamePlay(player, dealer, game_deck, blackjack_multiplier)
|
31 |
+
return game_deck, dealer, player, game_play
|
32 |
+
|
33 |
+
game_deck, dealer, player, game_play = start_game()
|
34 |
+
|
35 |
+
def display_pro_tip(player, dealer):
|
36 |
+
player_total = sum(card.value for card in player.cards)
|
37 |
+
dealer_upcard = dealer.cards[0].value
|
38 |
+
|
39 |
+
if player_total <= 11:
|
40 |
+
return "Pro Tip: With a total of 11 or less, it's generally advisable to hit."
|
41 |
+
elif player_total == 12 and dealer_upcard <= 3:
|
42 |
+
return "Pro Tip: With a total of 12 and the dealer showing 3 or less, consider hitting."
|
43 |
+
elif player_total >= 17:
|
44 |
+
return "Pro Tip: With a total of 17 or more, it's usually best to stand."
|
45 |
+
else:
|
46 |
+
return ""
|
47 |
+
|
48 |
+
def display_betting_strategy(player, dealer):
|
49 |
+
player_total = sum(card.value for card in player.cards)
|
50 |
+
dealer_upcard = dealer.cards[0].value
|
51 |
+
|
52 |
+
if player_total <= 11:
|
53 |
+
return "Betting Strategy: Consider increasing your bet when your total is 11 or less."
|
54 |
+
elif player_total >= 17 and dealer_upcard <= 6:
|
55 |
+
return "Betting Strategy: Consider increasing your bet when you have a strong hand and the dealer has a weak upcard."
|
56 |
+
else:
|
57 |
+
return ""
|
58 |
+
|
59 |
+
st.title('🃏Blackjack Simulator AI♠2️⃣1️⃣')
|
60 |
+
|
61 |
+
if st.button('New hand?'):
|
62 |
+
game_play.deal_in()
|
63 |
+
|
64 |
+
player_stats = st.empty()
|
65 |
+
player_images = st.empty()
|
66 |
+
player_hit_option = st.empty()
|
67 |
+
player_double_down_option = st.empty()
|
68 |
+
player_stand_option = st.empty()
|
69 |
+
dealer_stats = st.empty()
|
70 |
+
dealer_images = st.empty()
|
71 |
+
result = st.empty()
|
72 |
+
pro_tip = st.empty()
|
73 |
+
betting_strategy = st.empty()
|
74 |
+
|
75 |
+
if 'Hit' in player.possible_actions:
|
76 |
+
if player_hit_option.button('Hit'):
|
77 |
+
player.player_hit(game_deck, game_play)
|
78 |
+
if 'Hit' not in player.possible_actions:
|
79 |
+
player_hit_option.empty()
|
80 |
+
|
81 |
+
if 'Double Down' in player.possible_actions:
|
82 |
+
if player_double_down_option.button('Double Down'):
|
83 |
+
player.double_down(game_deck, game_play)
|
84 |
+
player_double_down_option.empty()
|
85 |
+
player_hit_option.empty()
|
86 |
+
player_stand_option.empty()
|
87 |
+
|
88 |
+
if 'Stand' in player.possible_actions:
|
89 |
+
if player_stand_option.button('Stand'):
|
90 |
+
player.stand(game_play)
|
91 |
+
player_hit_option.empty()
|
92 |
+
player_double_down_option.empty()
|
93 |
+
player_stand_option.empty()
|
94 |
+
|
95 |
+
game_play.update()
|
96 |
+
player_stats.write(player)
|
97 |
+
player_images.image([Image.open(card.image_location) for card in player.cards], width=100)
|
98 |
+
dealer_stats.write(dealer)
|
99 |
+
dealer_images.image([Image.open(card.image_location) for card in dealer.cards], width=100)
|
100 |
+
result.write(game_play)
|
101 |
+
|
102 |
+
pro_tip.write(display_pro_tip(player, dealer))
|
103 |
+
betting_strategy.write(display_betting_strategy(player, dealer))
|