-
Notifications
You must be signed in to change notification settings - Fork 0
/
FullBlackjackMultiplayer.py
147 lines (125 loc) · 4.23 KB
/
FullBlackjackMultiplayer.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from random import randint
def print_card_name(card_rank):
if card_rank == 1:
card_name = 'Ace'
elif card_rank == 11:
card_name = 'Jack'
elif card_rank == 12:
card_name = 'Queen'
elif card_rank == 13:
card_name = 'King'
else:
card_name = card_rank
if card_rank == 8 or card_rank == 1:
print('Drew an ' + str(card_name))
elif card_rank < 1 or card_rank > 13:
print('BAD CARD')
else:
print('Drew a ' + str(card_name))
def draw_card():
card_rank = randint(1, 13)
print_card_name(card_rank)
if card_rank == 11 or card_rank == 12 or card_rank == 13:
card_value = 10
elif card_rank == 1:
card_value = 11
else:
card_value = card_rank
return card_value
def print_header(message):
print('-----------')
print(message)
print('-----------')
def draw_starting_hand(name):
print_header(name.upper() + ' TURN')
return draw_card() + draw_card()
def print_end_turn_status(hand_value):
print('Final hand: ' + str(hand_value) + '.')
if hand_value == 21:
print('BLACKJACK!')
elif hand_value > 21:
print('BUST.')
def get_end_game_status(player_hand, dealer_hand):
if player_hand <= 21 and (player_hand > dealer_hand or dealer_hand > 21):
return "win"
elif player_hand > 21 or (dealer_hand <= 21 and dealer_hand > player_hand):
return "lose"
else:
return "push"
def get_player_hand(player_hand):
should_hit = 'y'
while player_hand < 21:
should_hit = input("You have {}. Hit (y/n)? ".format(player_hand))
if should_hit == 'n':
break
elif should_hit != 'y':
print("Sorry I didn't get that.")
else:
player_hand = player_hand + draw_card()
print_end_turn_status(player_hand)
return player_hand
def get_dealer_hand():
dealer_hand = draw_starting_hand("DEALER")
while dealer_hand < 17:
print("Dealer has {}.".format(dealer_hand))
dealer_hand = dealer_hand + draw_card()
print_end_turn_status(dealer_hand)
return dealer_hand
def check_elimination(player, score_board, player_board):
index = player_board.index(player)
if score_board[index] <= 0:
return player # Return the player to be eliminated
# Start the game
player_board = []
score_board = []
player_hand_table = []
num_players = int(input("Welcome to Blackjack! How many players? "))
i = 1
while i <= num_players:
player = input(f"What is player {i}'s name? ")
player_board.append(player)
score_board.append(3)
i += 1
while score_board:
# Players' turns
player_hand_table = []
for player in player_board:
player_hand = draw_starting_hand(f"{player}'S")
player_hand = get_player_hand(player_hand)
player_hand_table.append(player_hand)
d_hand = get_dealer_hand()
print_header('GAME RESULT')
# Evaluate results and update scores
eliminations = []
for player in player_board:
index = player_board.index(player)
p_hand = player_hand_table[index]
result = get_end_game_status(p_hand, d_hand)
if result == "win":
score_board[index] += 1
print(f"{player} wins! Score: {score_board[index]}")
elif result == "lose":
score_board[index] -= 1
print(f"{player} loses! Score: {score_board[index]}")
elif result == "push":
print(f"{player} pushes! Score: {score_board[index]}")
# Check for eliminations after the scores are updated
check = check_elimination(player, score_board, player_board)
if check:
eliminations.append(check)
# Remove eliminated players
for eliminated_player in eliminations:
index = player_board.index(eliminated_player)
print(f"{eliminated_player} eliminated!")
player_board.pop(index)
score_board.pop(index)
# Check if all players are eliminated
if not player_board:
print("All players eliminated!")
break
# Ask if they want to play another round
play_hand = input("Do you want to play another hand (y/n)? ")
if play_hand == "n":
break
elif play_hand != "y":
print("Sorry, I didn't get that.")