-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_engine.py
More file actions
95 lines (67 loc) · 2.36 KB
/
game_engine.py
File metadata and controls
95 lines (67 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
module to implement engine based functions
"""
import time
import util
from color import *
from cards import Card, Rank
if __name__ == "__main__":
print(f"{RED}Please start the game by running the main.py file{RESET}")
def buy_in():
print(f"{BLACK_BACKGROUND}Buy-In{RESET}")
return util.scan_chips("How much are you buying in?\n")
def calculate_hand_total(cards):
ace_card = Card(rank=Rank.ACE, suit=None)
non_aces = [card for card in cards if card != ace_card]
aces = [card for card in cards if card == ace_card]
base_sum = sum(c.rank.values[0] for c in non_aces)
all_totals = []
def try_combinations(index=0, current_sum=0):
if index == len(aces):
all_totals.append(base_sum + current_sum)
return
# try this Ace as 1
try_combinations(index + 1, current_sum + 1)
# try this Ace as 11
try_combinations(index + 1, current_sum + 11)
try_combinations()
valid_totals = [t for t in all_totals if t <= 21]
return max(valid_totals) if valid_totals else min(all_totals)
def print_the_board(game, sleep_amount=0.0):
"""
printing the player and the dealers cards:
"""
util.clear_screen()
print(f"{RED}DEALER{RESET}")
for i in game.dealer_cards:
print(i, end="\t")
print(f"\n\n\n{GREEN}{game.bet_amount}${RESET}")
print(f"\n\n{BLUE}PLAYER(YOU){RESET}")
for i in game.player_cards:
print(i, end="\t")
print("\n\n\n.::::::::::::::::::::::::::::::::::::::::::::.\n")
time.sleep(sleep_amount)
def is_blackjack(game):
return calculate_hand_total(game.player_cards) == 21
def handle_blackjack(game):
print(f"{GREEN}BLACKJACK! You win 3:2!{RESET}\n")
winnings = int(game.bet_amount * 1.5)
game.chips += game.bet_amount + winnings # return bet + 3:2
util.ask_to_play_again(game)
def evaluate_game_result(player_total, dealer_total):
if dealer_total > 21:
return "player_win"
if player_total > dealer_total:
return "player_win"
elif player_total == dealer_total:
return "draw"
else:
return "dealer_win"
def dealer_has_soft_17(dealer_cards):
ace_card = Card(rank=Rank.ACE, suit=None)
six_card = Card(rank=Rank.SIX, suit=None)
return (
(len(dealer_cards) == 2)
and (ace_card in dealer_cards)
and (six_card in dealer_cards)
)