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s', 'Diamonds')
|
4 |
+
|
5 |
+
|
6 |
+
class Card:
|
7 |
+
def __init__(self, rank, suit):
|
8 |
+
self.rank = rank
|
9 |
+
self.suit = suit
|
10 |
+
if self.rank == 1:
|
11 |
+
self.card_scores = [1, 11]
|
12 |
+
elif self.rank >= 11 and self.rank <= 13:
|
13 |
+
self.card_scores = [10, 10]
|
14 |
+
else:
|
15 |
+
self.card_scores = [self.rank, self.rank]
|
16 |
+
|
17 |
+
if self.rank == 1:
|
18 |
+
self.short_rank = 'A'
|
19 |
+
elif self.rank == 11:
|
20 |
+
self.short_rank = 'J'
|
21 |
+
elif self.rank == 12:
|
22 |
+
self.short_rank = 'Q'
|
23 |
+
elif self.rank == 13:
|
24 |
+
self.short_rank = 'K'
|
25 |
+
else:
|
26 |
+
self.short_rank = str(self.rank)
|
27 |
+
|
28 |
+
if self.suit == 'Spades':
|
29 |
+
self.short_suit = 'S'
|
30 |
+
elif self.suit == 'Hearts':
|
31 |
+
self.short_suit = 'H'
|
32 |
+
elif self.suit == 'Clubs':
|
33 |
+
self.short_suit = 'C'
|
34 |
+
else:
|
35 |
+
self.short_suit = 'D'
|
36 |
+
|
37 |
+
self.image_location = 'static/images/{}{}.png'.format(
|
38 |
+
self.short_rank, self.short_suit)
|
39 |
+
|
40 |
+
def __repr__(self):
|
41 |
+
if self.rank == 1:
|
42 |
+
true_rank = 'Ace'
|
43 |
+
elif self.rank == 11:
|
44 |
+
true_rank = 'Jack'
|
45 |
+
elif self.rank == 12:
|
46 |
+
true_rank = 'Queen'
|
47 |
+
elif self.rank == 13:
|
48 |
+
true_rank = 'King'
|
49 |
+
else:
|
50 |
+
true_rank = str(self.rank)
|
51 |
+
return '{} of {}'.format(true_rank, self.suit)
|
52 |
+
|
53 |
+
|
54 |
+
class Deck:
|
55 |
+
def __init__(self, number_of_decks):
|
56 |
+
self.number_of_decks = number_of_decks
|
57 |
+
self.cards = []
|
58 |
+
self.create(self.number_of_decks)
|
59 |
+
|
60 |
+
def __repr__(self):
|
61 |
+
return 'Game deck has {} cards remaining'.format(len(self.cards))
|
62 |
+
|
63 |
+
def create(self, number_of_decks):
|
64 |
+
decks = [Card(rank, suit) for suit in suits for rank in range(1, 14)
|
65 |
+
for deck in range(number_of_decks)]
|
66 |
+
decks = random.sample(decks, len(decks))
|
67 |
+
self.cards.extend(decks)
|
68 |
+
|
69 |
+
def draw(self):
|
70 |
+
drawn_card = self.cards[0]
|
71 |
+
self.cards.remove(self.cards[0])
|
72 |
+
return drawn_card
|
73 |
+
|
74 |
+
def reset(self):
|
75 |
+
self.cards = []
|
76 |
+
self.create(self.number_of_decks)
|
77 |
+
|
78 |
+
|
79 |
+
class Dealer:
|
80 |
+
def __init__(self):
|
81 |
+
self.cards = []
|
82 |
+
self.hand_scores = [0, 0]
|
83 |
+
self.best_outcome = 'Awaiting deal'
|
84 |
+
|
85 |
+
def __repr__(self):
|
86 |
+
return 'Dealer Hand: {}, Scores: {}, Best Outcome: {}'.format(self.cards, list(set(self.hand_scores)), self.best_outcome)
|
87 |
+
|
88 |
+
def hit(self, game_deck):
|
89 |
+
draw_card = game_deck.draw()
|
90 |
+
self.cards.append(draw_card)
|
91 |
+
card_scores = draw_card.card_scores
|
92 |
+
self.hand_scores = [a + b for a,
|
93 |
+
b in zip(self.hand_scores, card_scores)]
|
94 |
+
if len(self.cards) <= 1:
|
95 |
+
self.best_outcome = 'Awaiting Deal'
|
96 |
+
elif 21 in self.hand_scores and len(self.cards) == 2:
|
97 |
+
self.best_outcome = 'Blackjack'
|
98 |
+
elif self.hand_scores[0] > 21 and self.hand_scores[1] > 21:
|
99 |
+
self.best_outcome = 'Bust'
|
100 |
+
else:
|
101 |
+
self.best_outcome = max([i for i in self.hand_scores if i <= 21])
|
102 |
+
|
103 |
+
def reset(self):
|
104 |
+
self.cards.clear()
|
105 |
+
self.hand_scores = [0, 0]
|
106 |
+
self.best_outcome = 'Awaiting deal'
|
107 |
+
|
108 |
+
|
109 |
+
class Player(Dealer):
|
110 |
+
def __init__(self):
|
111 |
+
self.cards = []
|
112 |
+
self.hand_scores = [0, 0]
|
113 |
+
self.best_outcome = 'Awaiting deal'
|
114 |
+
self.possible_actions = ['No deal yet']
|
115 |
+
|
116 |
+
def __repr__(self):
|
117 |
+
return 'Player Hand: {}, Scores: {}, Best Outcome: {}'.format(self.cards, list(set(self.hand_scores)), self.best_outcome)
|
118 |
+
|
119 |
+
def stand(self, game_play):
|
120 |
+
self.possible_actions = []
|
121 |
+
game_play.commentary.append('Player is standing')
|
122 |
+
|
123 |
+
def double_down(self, game_deck, game_play):
|
124 |
+
self.hit(game_deck)
|
125 |
+
game_play.commentary.append('Player is doubling down')
|
126 |
+
self.possible_actions = []
|
127 |
+
|
128 |
+
def player_hit(self, game_deck, game_play):
|
129 |
+
self.hit(game_deck)
|
130 |
+
game_play.commentary.append('Player has hit')
|
131 |
+
self.get_possibilities(game_play)
|
132 |
+
|
133 |
+
def get_possibilities(self, game_play):
|
134 |
+
if self.best_outcome in ['Blackjack', 'Bust', 21]:
|
135 |
+
self.possible_actions = []
|
136 |
+
game_play.commentary.append('Player has no options')
|
137 |
+
elif len(self.cards) == 2:
|
138 |
+
self.possible_actions = ['Hit', 'Stand', 'Double Down']
|
139 |
+
game_play.commentary.append(
|
140 |
+
'Player can still hit, double down or stand')
|
141 |
+
else:
|
142 |
+
self.possible_actions = ['Hit', 'Stand']
|
143 |
+
game_play.commentary.append('Player can still hit or stand')
|
144 |
+
|
145 |
+
def reset(self):
|
146 |
+
self.cards = []
|
147 |
+
self.hand_scores = [0, 0]
|
148 |
+
self.best_outcome = 'Awaiting deal'
|
149 |
+
self.possible_actions = []
|
150 |
+
self.has_doubled_down = False
|
151 |
+
|
152 |
+
|
153 |
+
class GamePlay:
|
154 |
+
def __init__(self, player, dealer, game_deck, blackjack_multiplier):
|
155 |
+
self.player = player
|
156 |
+
self.dealer = dealer
|
157 |
+
self.game_deck = game_deck
|
158 |
+
self.blackjack_multiplier = blackjack_multiplier
|
159 |
+
self.commentary = []
|
160 |
+
|
161 |
+
def __repr__(self):
|
162 |
+
return "Commentary: {}".format(self.commentary)
|
163 |
+
|
164 |
+
def dealer_turn(self):
|
165 |
+
self.dealer.hit(self.game_deck)
|
166 |
+
if self.dealer.best_outcome == 'Blackjack':
|
167 |
+
self.commentary.append('Dealer hit Blackjack')
|
168 |
+
elif self.dealer.best_outcome == 'Bust':
|
169 |
+
self.commentary.append('Dealer went Bust')
|
170 |
+
elif int(self.dealer.best_outcome) < 17:
|
171 |
+
self.commentary.append(
|
172 |
+
'Dealer has {}, Dealer has to hit'.format(self.dealer.best_outcome))
|
173 |
+
self.dealer_turn()
|
174 |
+
elif int(self.dealer.best_outcome) == 17 and 1 in [card.rank for card in self.dealer.cards]:
|
175 |
+
self.commentary.append('Dealer has a soft 17, Dealer has to hit')
|
176 |
+
self.dealer_turn()
|
177 |
+
else:
|
178 |
+
self.commentary.append(
|
179 |
+
'Dealer is proceeding with {}'.format(self.dealer.best_outcome))
|
180 |
+
|
181 |
+
def update(self):
|
182 |
+
if len(self.player.possible_actions) == 0:
|
183 |
+
if self.player.best_outcome == 'Bust':
|
184 |
+
self.commentary.append(
|
185 |
+
"Player busted. No need for Dealer to go. Player loses their initial bet")
|
186 |
+
elif self.player.best_outcome == 'Blackjack' and self.dealer.cards[0].rank not in [1, 10]:
|
187 |
+
self.commentary.append("Player has Blackjack. Dealer has no chance to hit Blackjack. Player wins {} times their initial bet".format(
|
188 |
+
str(self.blackjack_multiplier)))
|
189 |
+
else:
|
190 |
+
self.commentary.append("Dealer turn can proceed as normal")
|
191 |
+
self.dealer_turn()
|
192 |
+
if self.dealer.best_outcome == 'Bust':
|
193 |
+
self.commentary.append(
|
194 |
+
"Dealer busted. Player wins their initial bet")
|
195 |
+
elif self.dealer.best_outcome == 'Blackjack' and self.player.best_outcome == 'Blackjack':
|
196 |
+
self.commentary.append(
|
197 |
+
"Dealer and Player both have Blackjack. Player retains their initial bet")
|
198 |
+
elif self.dealer.best_outcome == 'Blackjack' and self.player.best_outcome != 'Blackjack':
|
199 |
+
self.commentary.append(
|
200 |
+
"Dealer has Blackjack. Player loses their initial bet")
|
201 |
+
elif self.dealer.best_outcome != 'Blackjack' and self.player.best_outcome == 'Blackjack':
|
202 |
+
self.commentary.append("Player has Blackjack. Player wins {} times their initial bet".format(
|
203 |
+
str(self.blackjack_multiplier)))
|
204 |
+
elif int(self.dealer.best_outcome) == int(self.player.best_outcome):
|
205 |
+
self.commentary.append(
|
206 |
+
"Dealer and Player have same score. Player retains their initial bet")
|
207 |
+
elif int(self.dealer.best_outcome) > int(self.player.best_outcome):
|
208 |
+
self.commentary.append("Dealer has {} whereas Player has {}. Player loses their initial bet".format(
|
209 |
+
str(self.dealer.best_outcome), str(self.player.best_outcome)))
|
210 |
+
else:
|
211 |
+
self.commentary.append("Dealer has {} whereas Player has {}. Player wins their initial bet".format(
|
212 |
+
str(self.dealer.best_outcome), str(self.player.best_outcome)))
|
213 |
+
else:
|
214 |
+
pass
|
215 |
+
|
216 |
+
def reset(self):
|
217 |
+
self.commentary = []
|
218 |
+
|
219 |
+
def deal_in(self):
|
220 |
+
self.dealer.reset()
|
221 |
+
self.player.reset()
|
222 |
+
self.game_deck.reset()
|
223 |
+
self.reset()
|
224 |
+
self.player.hit(self.game_deck)
|
225 |
+
self.dealer.hit(self.game_deck)
|
226 |
+
self.player.hit(self.game_deck)
|
227 |
+
self.player.get_possibilities(self)
|