Skip to content

Commit b0b6c84

Browse files
Merge pull request #28 from Kokiix/main
feat: Slots game
2 parents 4527558 + 16f6922 commit b0b6c84

File tree

5 files changed

+167
-2
lines changed

5 files changed

+167
-2
lines changed

casino/games/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from . import blackjack
1+
from . import blackjack, slots
22

3-
__all__ = ["blackjack"]
3+
__all__ = ["blackjack", "slots"]

casino/games/slots/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .slots import play_slots
2+
3+
__all__ = ["play_slots"]

casino/games/slots/slots.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
""")

casino/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from casino.accounts import Account
55
from casino.games.blackjack import play_blackjack
66
from casino.utils import cprint, cinput, clear_screen, display_topbar
7+
from casino.games.slots import play_slots
8+
from casino.utils import cprint, cinput, clear_screen
79

810
CASINO_HEADER = """
911
┌──────────────────────────────────────┐
@@ -24,6 +26,7 @@
2426
# To add a new game, just add a handler function to GAME_HANDLERS
2527
GAME_HANDLERS: dict[str, Callable[[Account], None]] = {
2628
"blackjack": play_blackjack,
29+
"slots": play_slots,
2730
}
2831
games = list(GAME_HANDLERS.keys())
2932

casino/main3.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from casino.games.blackjack import play_blackjack
2+
from casino.games.slots import play_slots
3+
from casino.utils import cprint, cinput, clear_screen
4+
5+
CASINO_HEADER = """
6+
┌──────────────────────────────────────┐
7+
│ ♦ T E R M I N A L C A S I N O ♦ │
8+
└──────────────────────────────────────┘
9+
"""
10+
11+
ENTER_OR_QUIT_PROMPT = "[E]nter [Q]uit: \n"
12+
INVALID_CHOICE_PROMPT = "\nInvalid input. Please try again. \n\n"
13+
GAME_CHOICE_PROMPT = "Please choose a game to play:\n"
14+
15+
games = ["blackjack", "slots"]
16+
17+
18+
def main():
19+
play_slots()
20+
21+
22+
if __name__ == "__main__":
23+
main()

0 commit comments

Comments
 (0)