-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack.py
175 lines (168 loc) · 4.45 KB
/
blackjack.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import random, sys
class Card:
# A simple class representing a card
value, suite = 0, 0
def __init__(self, v, s):
self.value = v
self.suite = s
def __repr__(self):
return str(self.value) + " of " + self.suite
def __str__(self):
return str(self.value) + " of " + self.suite
class Deck:
# A simple class representing a deck of cards
values = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King', 'Ace']
suites = ['Clubs', 'Spades', 'Hearts', 'Diamonds']
cards = []
# init function. Create the deck
def __init__(self):
for value in self.values:
for suite in self.suites:
self.cards.append(Card(value, suite))
# Shuffle the cards
def shuffle(self):
print "The dealer shuffles the deck\n"
random.shuffle(self.cards)
def draw(self, num):
ret = []
for card in self.cards[:num]:
ret.append(card)
self.cards.remove(card)
return ret
class Person:
hand = []
def receiveCards(self, cards):
for card in cards:
self.hand.append(card)
def hit(self, card):
print "\nHit me!"
self.hand.append(card)
print "\nDealt:"
print card
print "\nYour hand:"
print self
if self.total() > 21:
print "BUSTED"
elif self.total() == 21:
print "21!"
else:
print "Total: %d" % self.total()
def total(self):
total = 0
for card in self.hand:
if(card.value in ('Jack', 'Queen', 'King')):
total += 10
elif card.value == 'Ace':
if(total + 11 > 21):
total = total + 1
else:
total = total + 11
else:
total += card.value
return total
def __repr__(self):
return self.hand
def __str__(self):
return '%s' % ', '.join(map(str, self.hand))
class Dealer(Person):
hand = []
deck = []
# init function. Get a deck
def __init__(self):
self.deck = Deck()
self.deck.shuffle()
def deal(self, players):
for _ in range(2):
for person in players:
person.receiveCards(self.deck.draw(1))
def hit(self, card):
print "\nThe dealer takes a card"
self.hand.append(card)
print "\nDealt:"
print card
print "\nThe dealer's hand:"
print '%s' % ', '.join(map(str, self.hand))
if self.total() > 21:
print "\nOh no! BUSTED"
elif self.total() == 21:
print "\nYES! 21!"
else:
print "Total: %d" % self.total()
def hitPlayer(self, person):
person.hit(self.deck.draw(1)[0])
def revealSecond(self):
return self.hand[1]
def revealAll(self):
return '%s' % ', '.join(map(str, self.hand))
def __repr__(self):
return self.hand
def __str__(self):
return str(self.hand[0])
class Game:
dealer, player = 0, 0
players = []
def __init__(self):
print "===================================="
print " WELCOME TO BLACKJACK "
print "===================================="
print "\n\nThe dealer beckons you over. \"Come play a game!\"\n"
def playGame(self):
self.dealer = Dealer()
self.players.append(self.dealer)
self.player = Person()
self.players.append(self.player)
self.deal()
def deal(self):
print "The dealer deals the deck\n"
self.dealer.deal(self.players)
self.showCards()
def showCards(self):
print "You were dealt:"
print self.player
print "\nThe dealer shows:"
print self.dealer
self.playerMoves()
def playerMoves(self):
move = ""
while self.player.total() < 21 and move not in ("No", "no"):
move = raw_input("\nWould you like to hit? Yes or No: ")
if move in ("Yes", "yes"):
self.dealer.hitPlayer(self.player)
if self.player.total() == 21:
print "21! Hell yeah!"
elif self.player.total() > 21:
print "\nBUSTED! You lost"
sys.exit()
else:
print "\nYour total is %d - let's see what the dealer has!" % self.player.total()
self.dealerMoves()
def dealerMoves(self):
print "\nThe dealer reveals his next card:"
print self.dealer.revealSecond()
print "\nHis hand is:"
print self.dealer.revealAll()
if self.dealer.total() == 21:
print "\nBLACKJACK!\n"
if self.player.total() == 21:
print "It's a tie!"
sys.exit()
else:
print "Dealer wins, try again"
sys.exit()
elif self.dealer.total() < 17:
while self.dealer.total() <= 17:
self.dealer.hitPlayer(self.dealer)
self.results()
def results(self):
if self.dealer.total() == 21:
if self.player.total() == 21:
print "\nIt's a tie!"
else:
print "\nDealer wins with 21, try again"
elif self.dealer.total() > 21 and self.player.total() <= 21:
print "\nDealer BUSTED! You win!"
elif self.dealer.total() < 21 and self.dealer.total() < self.player.total():
print "\nYou were closer to 21! You win!"
sys.exit()
game = Game()
game.playGame()