-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlackJack.py
More file actions
132 lines (106 loc) · 3.47 KB
/
BlackJack.py
File metadata and controls
132 lines (106 loc) · 3.47 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
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
import random
class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
def __str__(self):
return f'{self.rank["rank"]} of {self.suit}'
class Deck:
def __init__(self):
self.cards = []
suits = ['spades', 'clubs', 'hearts', 'diamonds']
ranks = [
{"rank": "A", "value": 11},
{"rank": "2", "value": 2},
{"rank": "3", "value": 3},
{"rank": "4", "value": 4},
{"rank": "5", "value": 5},
{"rank": "6", "value": 6},
{"rank": "7", "value": 7},
{"rank": "8", "value": 8},
{"rank": "9", "value": 9},
{"rank": "10", "value": 10},
{"rank": "J", "value": 10},
{"rank": "Q", "value": 10},
{"rank": "K", "value": 10},
]
for suit in suits:
for rank in ranks:
self.cards.append(Card(suit, rank))
def shuffle(self):
if len(self.cards) > 1:
random.shuffle(self.cards)
def deal(self, number):
cards_dealt = []
for x in range(number):
if len(self.cards) > 0:
card = self.cards.pop()
cards_dealt.append(card)
return cards_dealt
class Hand:
def __init__(self, dealer=False):
self.cards = []
self.value = 0
self.dealer = dealer
def add_card(self, card_list):
self.cards.extend(card_list)
def calculate_value(self):
self.value = 0
has_ace = False
for card in self.cards:
card_value = int(card.rank['value'])
self.value += card_value
if card.rank['rank'] == "A":
has_ace = True
if has_ace and self.value > 21:
self.value -= 10
def get_value(self):
self.calculate_value()
return self.value
def is_blackjack(self):
return self.getvalue() == 21
def display(self, show_all_dealer_cards = False):
print(f'''{"Dealer's" if self.dealer else "Your"} hand:''')
for index, card in enumerate(self.cards):
if index == 0 and self.dealer \
and not show_all_dealer_cards and not self.is_blackjack:
print('hidden')
else:
print(card)
if not self.dealer:
print("Value:", self.get_value())
print()
class Game:
def play(self):
game_number = 0
games_to_play = 0
while games_to_play <= 0:
try:
games_to_play = int(input("How many games do you want to play? "))
except:
print("You must enter a number.")
while game_number < games_to_play:
game_number += 1
deck = Deck()
deck.shuffle
player_hand = Hand()
dealer_hand = Hand(dealer=True)
for i in range(2):
player_hand.add_card(deck.deal(1))
dealer_hand.add_card(deck.deal(1))
print()
print("*" *30)
print(f"Game {game_number} of {games_to_play}")
def check_winner(self, player_hand, dealer_hand):
if player_hand.get_value() > 21:
print("You busted. Dealer wins!")
return True
elif dealer_hand.getvalue() > 21:
print("Dealer busted. You win!")
elif dealer_hand.is_blackjack() and player_hand.is_blackjack():
print("You have blackjack. You win!")
elif dealer_hand.is_blackjack():
print("Dealer has blackjack. Dealer wins!")
return False
g = Game()
g.play