blackjack.py
3.57 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
from random import randrange
SUITS = (':clubs:', ':spades:', ':hearts:', ':diamonds:')
CARDS = ('A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K')
CARD_VALUES = {'A':11, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10, 'K':10}
class Hand:
def __init__(self):
self.cards = []
def add_cards(self, cards):
if isinstance(cards, list):
for card in cards:
self.cards.append(card)
else:
self.cards.append(cards)
def remove_card(self, card):
self.cards.remove(card)
def get_cards(self):
return self.cards
def get_points(self):
value = 0
number_of_aces = 0
for card in self.cards:
value = value + CARD_VALUES[card[0]]
if card[0] == 'A':
number_of_aces = number_of_aces + 1
while value > 21 and number_of_aces > 0:
value = value - 10
number_of_aces = number_of_aces - 1
return value
def is_blackjack(self):
if self.get_points() == 21 and len(self.cards) == 2:
return True
else:
return False
class Deck:
def __init__(self, number_of_decks=1):
self.shuffle(number_of_decks)
def shuffle(self, number_of_decks):
self.cards = []
for d in range(number_of_decks):
for suit in SUITS:
for card in CARDS:
self.cards.append(card + suit)
def draw(self,number_of_cards=1):
drawn_cards = []
for n in range(number_of_cards):
choice = randrange(0,len(self.cards))
drawn_cards.append(self.cards.pop(choice))
return drawn_cards
def cards_left(self):
return len(self.cards)
class Blackjack:
def __init__(self, bet):
self.bet = bet
self.deck = Deck()
self.player_hand = Hand()
self.player_hand.add_cards(self.deck.draw(2))
#print(player_hand.get_cards())
self.player_points = self.player_hand.get_points()
#print("Player: {}".format(player_points,))
self.dealer_hand = Hand()
self.dealer_hand.add_cards(self.deck.draw(2))
#print(dealer_hand.get_cards())
self.dealer_points = self.dealer_hand.get_points()
#print("Dealer: {}".format(dealer_points,))
def is_busted(self):
if self.player_points > 21:
return 1
elif self.dealer_points > 21:
return 2
else:
return 0
def is_win(self):
# TODO: Check for blackjack Ace + 10 pt
if self.player_hand.is_blackjack() and not self.dealer_hand.is_blackjack():
return (self.bet + self.bet * 1.5, 'Blackjack, you win: {}')
elif self.dealer_hand.is_blackjack():
return (-self.bet, 'You lose: {}')
elif self.player_points > self.dealer_points:
return (self.bet * 2, 'You win: {}')
elif self.player_points == self.dealer_points:
return (self.bet, 'push your bet is returned: {}')
else:
return (-self.bet, 'You lose: {}')
def print_hand(self):
dealers = ' '.join(self.dealer_hand.get_cards()[1:])
print("Dealer's Hand: {}".format(dealers))
print("Player's Hand: {} for {} points".format(' '.join(self.player_hand.get_cards()), self.player_points))
bet = 10
bj = Blackjack(bet)
bj.print_hand()
busted = bj.is_busted()
if busted == 1:
print("YOU BUST")
elif busted == 2:
print("Dealer Busts, YOU WIN!")
else:
win, response = bj.is_win()
print(response.format(win,))