-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv3Blackjack.py
executable file
·312 lines (268 loc) · 8.72 KB
/
v3Blackjack.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#Blackjack - Third Try!
import random
import time
# .o88b. db .d8b. .d8888. .d8888. d88888b .d8888.
# d8P Y8 88 d8' `8b 88' YP 88' YP 88' 88' YP
# 8P 88 88ooo88 `8bo. `8bo. 88ooooo `8bo.
# 8b 88 88~~~88 `Y8b. `Y8b. 88~~~~~ `Y8b.
# Y8b d8 88booo. 88 88 db 8D db 8D 88. db 8D
# `Y88P' Y88888P YP YP `8888Y' `8888Y' Y88888P `8888Y'
#Classes: Card, Deck, Hand, Player, Game
class Card:
def __init__(self, suite, number):
self.symbols = [u"\u2665", u"\u2663", u"\u2660", u"\u2666"] #Suite Order: Hearts, Clubs, Spades, Diamonds
self.names = ["Joker", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
self.values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
self.symbol = self.symbols[suite]
self.name = self.names[number]
self.value = self.values[number]
def printCard(self):
print self.symbol, self.name, " ",
class Deck():
def __init__(self):
self.cardList = []
self.size = 52
for s in range(4):
for n in range(1,14):
self.cardList.append(Card(s, n))
def printDeck(self):
for card in self.cardList:
card.printCard()
def dealMe(self):
pick = random.randrange(1, self.size)
card = self.cardList[pick]
self.cardList.pop(pick)
self.size = len(self.cardList)
return card
class Hand():
def __init__(self):
self.cardList = []
self.value = 0
self.hasAce = False
self.hasJack = False
def addCard(self, card):
self.cardList.append(card)
self.value = 0
if card.name == "Jack":
self.hasJack = True
elif card.name == "Ace":
self.hasAce = True
for i in self.cardList:
self.value = self.value + i.value
if self.hasAce == True:
if self.value <= 11:
self.value = self.value + 10
def printHand(self):
for card in self.cardList:
card.printCard()
def hasBlackjack(self):
if len(self.cardList) == 2:
if self.hasAce and self.hasJack:
return True
else:
return False
else:
return False
def handFinished(self):
if self.value >= 21:
return True
else:
return False
class Player:
def __init__(self, name, wallet):
self.name = name
self.wallet = wallet
self.bet = 0
self.hand = Hand()
def printStats(self):
print "Welcome " + self.name + "! You have $" + str(self.wallet) + " to gamble away!"
def printMe(self):
print self.name
print self.wallet
self.hand.printHand()
class Game:
def __init__(self):
self.player = Player(raw_input("What is your name? "), input("How much money you got? "))
self.dealer = Player("Dealer", 0)
self.deck = Deck()
self.message = "Welcome to Jampack Blackjack!"
# # .88b d88. d88888b d888888b db db .d88b. d8888b. .d8888.
# # 88'YbdP`88 88' `~~88~~' 88 88 .8P Y8. 88 `8D 88' YP
# # 88 88 88 88ooooo 88 88ooo88 88 88 88 88 `8bo.
# # 88 88 88 88~~~~~ 88 88~~~88 88 88 88 88 `Y8b.
# # 88 88 88 88. 88 88 88 `8b d8' 88 .8D db 8D
# # YP YP YP Y88888P YP YP YP `Y88P' Y8888D' `8888Y'
def clearScreen(self):
for i in range(30):
print "\n",
def showGame(self):
self.clearScreen()
print "-----------------------"
print "***Jampack Blackjack***"
print "-----------------------"
print "\n\n"
print self.message
print "\n\n"
print "Dealer's Hand: " + str(self.dealer.hand.value)
self.dealer.hand.printHand()
print "\n\n\n"
print "Player's Hand: " + str(self.player.hand.value)
self.player.hand.printHand()
print "\n\n"
def printMe(self):
self.player.printStats()
self.player.printMe()
self.dealer.printMe()
print "Current number of cards left in deck: " + str(self.deck.size)
def initialDeal(self):
self.player.hand = Hand()
self.dealer.hand = Hand()
self.deck = Deck()
self.player.bet = input("Place your bet! ")
if self.player.bet > self.player.wallet:
print "Cheater. Try again."
self.player.bet = input("Place your bet!")
self.message = "New Hand"
self.dealer.hand.addCard(game.deck.dealMe())
self.dealer.hand.addCard(game.deck.dealMe())
self.player.hand.addCard(game.deck.dealMe())
self.player.hand.addCard(game.deck.dealMe())
def playerTurn(self):
hit = True
while hit:
choice = raw_input("Hit? y/n: ")
if choice == "y":
self.message = "Player Hits"
self.showGame()
time.sleep(1)
self.player.hand.addCard(game.deck.dealMe())
self.showGame()
if self.player.hand.handFinished():
hit = False
break
else:
hit = False
self.message = "End of Turn"
self.showGame()
def dealerTurn(self):
while self.dealer.hand.value < 17 or self.dealer.hand.value < self.player.hand.value:
if self.dealer.hand.value < self.player.hand.value and self.dealer.hand.handFinished() == False:
time.sleep(1)
self.message = "Dealer Hits"
self.showGame()
time.sleep(1)
self.dealer.hand.addCard(game.deck.dealMe())
self.showGame()
time.sleep(1)
else:
break
def roundOver(self):
if self.player.hand.value > 21:
return True
elif self.dealer.hand.value > 21:
return True
elif self.player.hand.hasBlackjack():
return True
elif self.dealer.hand.hasBlackjack():
return True
else:
return False
def declareOutcome(self):
if self.player.hand.value > 21:
self.player.wallet = self.player.wallet - self.player.bet
self.message = "Player Bust! You lose!"
elif self.dealer.hand.value > 21:
self.player.wallet = self.player.wallet + self.player.bet
self.message = "Dealer Bust! You win!"
elif self.player.hand.value == self.dealer.hand.value:
self.message = "It's a Tie! Weird."
elif self.player.hand.hasBlackjack():
self.player.wallet = self.player.wallet + (self.player.bet)*2
self.message = "You hit Blackjack! You win!!"
elif self.dealer.hand.hasBlackjack():
self.player.wallet = self.player.wallet - self.player.bet
self.message = "Dealer hit Blackjack! You lose!"
elif self.player.hand.value < self.dealer.hand.value:
self.player.wallet = self.player.wallet - self.player.bet
self.message = "You lose!!"
else:
self.message = "Weirdness happened."
def newHand(self):
global answer
print "You have $" + str(self.player.wallet)
answer = raw_input("Would you like to play again? y/n: ")
if answer != "y":
self.player.wallet = 0
def playAgain(self):
global answer
if answer == "y" and self.player.wallet <= 0:
print "You are out of money. This will begin a new game."
self.player.wallet = input("How much money you got? ")
# ooooooooooooo oooooooooooo .oooooo..o ooooooooooooo .oooooo..o
# 8' 888 `8 `888' `8 d8P' `Y8 8' 888 `8 d8P' `Y8
# 888 888 Y88bo. 888 Y88bo.
# 888 888oooo8 `"Y8888o. 888 `"Y8888o.
# 888 888 " `"Y88b 888 `"Y88b
# 888 888 o oo .d8P 888 oo .d8P
# o888o o888ooooood8 8""88888P' o888o 8""88888P'
#
#to test card constructor
# card1 = Card(0, 4)
# cardJack = Card(2, 11)
# cardAce = Card(0, 1)
# cardTen = Card(3, 10)
#to test hand constructor
# hand1 = Hand()
# hand1.addCard(cardTen)
# hand1.addCard(cardAce)
# hand1.printHand()
# print hand1.hasBlackjack()
# hand1.addCard(cardJack)
# hand1.printHand()
# print hand1.hasBlackjack()
#to test player constructor
# player = Player("jamie", 100)
# player.printStats()
# player.hand = blackjackHand
# player.printMe()
#to test deck constructor
# decktest = Deck()
# decktest.printDeck()
#has blackjack test
# blackjackHand = Hand()
# blackjackHand.addCard(decktest.dealMe())
# blackjackHand.addCard(decktest.dealMe())
# print blackjackHand.hasBlackjack()
# blackjackHand.printHand()
# decktest.printDeck()
# blackjackHand.printHand()
# testing the game constructor
# # game.printMe()
# game.player.hand.addCard(cardJack)
# game.player.hand.addCard(cardAce)
# game.player.hand.addCard(cardTen)
# game.dealer.hand.addCard(cardAce)
# # game.printMe()
# game.clearScreen()
# game.initialDeal()
# game.showGame()
# game.playerTurn()
game = Game()
answer = "y"
while game.player.wallet > 0:
game.player.printStats()
game.initialDeal()
game.showGame()
time.sleep(1)
if not game.roundOver():
game.playerTurn()
if not game.roundOver():
game.dealerTurn()
game.declareOutcome()
game.showGame()
time.sleep(1)
game.newHand()
game.playAgain()
#hit 21 or went over test
# print game.player.hand.handFinished()
# print game.dealer.hand.handFinished()