1+ import random
2+ import time
3+
4+ from casino .card_assets import assign_card_art
5+ from casino .types import Card
6+ from casino .utils import clear_screen , cprint , cinput , display_topbar
7+
8+ SLOTS_HEADER = """
9+ ┌───────────────────────────────┐
10+ │ ♠ S L O T S ♠ │
11+ └───────────────────────────────┘
12+ """
13+ HEADER_OPTIONS = {"header" : SLOTS_HEADER ,
14+ "margin" : 1 ,}
15+
16+ ITEMS = {"all" : "abcd" , "low_val" : "abc" , "high_val" : "d" }
17+ BET_PROMPT = """
18+ How much would you like to bet?
19+ Matching a | b | c : x1.5
20+ Matching d : x5.0
21+ """
22+ INVALID_BET_MSG = "That's not a valid bet."
23+ MIN_BET_AMT = 10
24+ MIN_BET_MSG = f"Each pay line requires at least ${ MIN_BET_AMT } ."
25+
26+ SEC_BTWN_SPIN = 0.1
27+ TOTAL_SPINS = 10
28+ WIN_PROB = 0.2
29+ HI_VAL_PROB = 0.05
30+
31+ # Currently 1 pay line, goal is to have several and:
32+ # - implement pattern patching for wins across lines
33+ # - be able to bet across lines independently
34+ # then maybe special lines
35+
36+ def play_slots (account ) -> None :
37+ take_new_bet = True
38+ bet_amount = 0
39+ while True :
40+ clear_screen ()
41+ display_topbar (account , ** HEADER_OPTIONS )
42+ if take_new_bet or bet_amount > account .balance :
43+ bet_amount = get_bet_amount (account )
44+ take_new_bet = False
45+
46+ spin_animation (account )
47+ clear_screen ()
48+ display_topbar (account , ** HEADER_OPTIONS )
49+
50+ # Display final spin result
51+ rng = random .random ()
52+ if rng <= WIN_PROB :
53+ money_gain = 0
54+ if rng <= HI_VAL_PROB :
55+ win_item = ITEMS ["high_val" ][random .randint (0 , len (ITEMS ["high_val" ]) - 1 )]
56+ money_gain = bet_amount * 5
57+ else :
58+ win_item = ITEMS ["low_val" ][random .randint (0 , len (ITEMS ["low_val" ]) - 1 )]
59+ money_gain = bet_amount * 1.5
60+
61+ account .deposit (money_gain )
62+ clear_screen ()
63+ display_topbar (account , ** HEADER_OPTIONS )
64+ print_spin (win_item , win_item , win_item )
65+ cprint (f"MATCH: +${ money_gain } " )
66+ else :
67+ final_items = (get_rand_item (), get_rand_item (), get_rand_item ())
68+ while len (set (final_items )) == 1 :
69+ final_items = (get_rand_item (), get_rand_item (), get_rand_item ())
70+ clear_screen ()
71+ account .withdraw (bet_amount )
72+ display_topbar (account , ** HEADER_OPTIONS )
73+ print_spin (* final_items )
74+ cprint (f"NO MATCH: -${ bet_amount } " )
75+
76+ # Choose what to do after spin
77+ choice = get_player_choice ()
78+ if choice ["quit" ]:
79+ return
80+ if choice ["change_bet" ]:
81+ take_new_bet = True
82+
83+ def get_bet_amount (account ):
84+ if account .balance < MIN_BET_AMT :
85+ raise RuntimeError ("PLAYER OUT OF MONEY IDK WHAT TO DO" )
86+
87+ while True :
88+ bet_str = cinput (BET_PROMPT ).strip ()
89+ try :
90+ bet = int (bet_str )
91+ if bet < MIN_BET_AMT :
92+ clear_screen ()
93+ display_topbar (account , ** HEADER_OPTIONS )
94+ cprint (MIN_BET_MSG )
95+ continue
96+ return bet
97+ except ValueError :
98+ clear_screen ()
99+ display_topbar (account , ** HEADER_OPTIONS )
100+ cprint (INVALID_BET_MSG )
101+
102+ def get_player_choice ():
103+ player_input = ""
104+ while player_input not in "rRqQcC" or player_input == "" :
105+ player_input = cinput ("[R]espin [C]hange Bet [Q]uit" )
106+
107+ if player_input in "qQ" :
108+ clear_screen ()
109+ return {"quit" : True }
110+ elif player_input in "rR" :
111+ return {"quit" : False , "change_bet" : False }
112+ else :
113+ return {"quit" : False , "change_bet" : True }
114+
115+ def spin_animation (account , total_spins = TOTAL_SPINS , sec_btwn_spins = SEC_BTWN_SPIN ) -> None :
116+ for _ in range (total_spins ):
117+ clear_screen ()
118+ display_topbar (account , ** HEADER_OPTIONS )
119+ print_spin (get_rand_item (), get_rand_item (), get_rand_item ())
120+ time .sleep (sec_btwn_spins )
121+
122+ def get_rand_item () -> str :
123+ return ITEMS ["all" ][random .randint (0 , len (ITEMS ["all" ]) - 1 )];
124+
125+ def print_spin (item1 , item2 , item3 ) -> None :
126+ cprint (f"""
127+ ┌────────────────────────┐
128+ | |
129+ | |
130+ | |
131+ |{ item1 :^8} { item2 :^8} { item3 :^8} |
132+ | |
133+ | |
134+ | |
135+ └────────────────────────┘
136+ """ )
0 commit comments