-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlackJack.py
173 lines (117 loc) · 4.89 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
import Deck
class controller:
def __init__(self):
self.cards = list()
def giveCard(self, cardid: int):
print("You are dealt a " + str(Deck.deck.pointValue(cardid)))
self.cards.append(cardid)
def handSize(self):
return len(self.cards)
def returnGameResult(self, result: int): #1 if win, 0 if loss
if(result == 1):
print("congratulations, you Win!")
else:
print("mmmm, I'm sorry, you failed.")
def getHand(self):
return self.cards
def requestDecision(self, dealerCard: int):
return input("'hit' or 'stand'?")
class humanController(controller):
def __init__(self):
self.cards = list()
def returnGameResult(self, result: int):
if(result == 1):
print("congratulations, you Win!")
else:
print("mmmm, I'm sorry, you failed.")
#class aiController(controller):
class cardGame:
def evaluateHand(self, hand):
aces = 0
baseValue = 0
for card in hand:
cardVal = Deck.deck.pointValue(card)
if(cardVal == 1):
aces += 1
else:
baseValue += cardVal
#Solve for max viable hand value
maxNonBust = baseValue + (11 * aces)
maxedAceCount = aces
#Aces default at 11, so reduce them to 1 until they are out or bust
while (maxedAceCount > 0 and maxNonBust > 21):
maxedAceCount += -1
maxNonBust += -10
#Evaluate Bust
busted = False
if(maxNonBust > 21):
busted = True
return baseValue, aces, maxNonBust, busted
def playRound(self):
#Intro
print("Welcome to Round " + str(11 - self.roundCounter) + "!")
self.roundCounter += -1
#Handle Dealer
dealerHandValue, dealerAces, dealerMax, dealerBust = self.evaluateHand(self.dealerHand)
#print("The dealers hand value is: " + str(dealerHandValue) + " baseVal, " + str(dealerAces) + " Aces, " + str(dealerMax) + " maxVal, bust=" + str(dealerBust))
if(dealerMax < 17):
self.dealerHand.append(self.theDeck.draw())
print("The dealer draws a card.")
else:
print("The dealer stands")
#Handle Player
playerHandValue, playerAces, playerMax, playerBust = self.evaluateHand(self.gamblerController.getHand())
print("Your hand value is: " + str(playerHandValue) + " baseVal, " + str(playerAces) + " Aces, " + str(playerMax) + " maxVal, bust=" + str(playerBust))
if(self.gamblerController.requestDecision(self.publicDealerCard) == 'hit'):
print("You choose to draw.")
newCard = self.theDeck.draw()
self.gamblerController.giveCard(newCard)
else:
print("You choose to Stand")
self.inProgress = False
#Evaluate Round
playerHandValue, playerAces, playerMax, playerBust = self.evaluateHand(self.gamblerController.getHand())
dealerHandValue, dealerAces, dealerMax, dealerBust = self.evaluateHand(self.dealerHand)
if(playerBust):
self.inProgress = False
print("You are BUSTED!")
if(self.inProgress):
self.playRound()
else:
#Dealer Draws till hand is valid
while (dealerMax < 17):
self.dealerHand.append(self.theDeck.draw())
print("The dealer draws a card.")
dealerHandValue, dealerAces, dealerMax, dealerBust = self.evaluateHand(self.dealerHand)
if(dealerMax >= 17):
print("The dealer Stands.")
print("GAME OVER: Dealer Value of " + str(dealerMax) + ", Player Value of " + str(playerMax)) #END GAME
outcome = 0
if((playerBust) or (playerMax < dealerMax and not dealerBust)):
outcome = 0
print("You Lose!")
else:
outcome = 1
print("You Win!")
self.gamblerController.returnGameResult(outcome)
def __init__(self):
#Setup Game
self.inProgress = True
self.roundCounter = 11
#Determine Controller Type
self.gamblerController = humanController()
self.dealerHand = []
#Deal Cards
self.theDeck = Deck.deck()
self.theDeck.shuffle()
self.dealerHand.append(self.theDeck.draw())
self.dealerHand.append(self.theDeck.draw())
self.gamblerController.giveCard(self.theDeck.draw())
self.gamblerController.giveCard(self.theDeck.draw())
#Start Game
self.publicDealerCard = self.dealerHand[0]
print("The dealer is showing: " + str(Deck.deck.pointValue(self.publicDealerCard)))
self.playRound() #start game
#evaluates a hand, returns: basicValue, AceCount
#MAIN
theGame = cardGame()