-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlackJack.py
242 lines (209 loc) · 7.94 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
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
import random
from XOR_task import *
from enum import Enum
import matplotlib.pyplot as plt
import time
class Actions(Enum):
Stand = 'S'
Hit = 'H'
Split = 'SP'
Double = 'DD'
class BlackJack:
DECK = []
def __init__(self):
self.deckCount = 8
self.drawnCards = []
BlackJack.DECK = BlackJack.getStaticDeck()
self.bank = 200
self.deck = BlackJack.DECK * self.deckCount
random.shuffle(self.deck)
plt.close()
plt.ion()
plt.show()
self.bankHistory = []
self.bet = 1
def run(self, agent, plot=True):
self.bank -= self.bet
if len(self.deck) < (len(BlackJack.DECK) * self.deckCount)/2:
self.deck = BlackJack.DECK * self.deckCount
random.shuffle(self.deck)
# print("New Deck . . .")
dealer_cards = []
agent_cards = []
# Initial Turn
agent_cards.append(self.draw(self.deck))
dealer_cards.append(self.draw(self.deck))
agent_cards.append(self.draw(self.deck))
dealer_cards.append(self.draw(self.deck))
# print(f'Bank: {self.bank}')
# print(f'Player hand: {agent_cards}')
# print(f'Dealer hand: {dealer_cards}')
# Player Dealing
if BlackJack.countCards(agent_cards) == 21 and BlackJack.countCards(dealer_cards) == 21:
self.bank += self.bet
# print("Push Black Jack.")
return
choice = agent.input(agent_cards, dealer_cards[0])
# print(f'Player chose to: {choice}')
hand1 = hand2 = None
if choice == Actions.Hit:
while (BlackJack.countCards(agent_cards) < 21):
agent_cards.append(self.draw(self.deck))
# print(f'Player hand: {agent_cards}')
if BlackJack.countCards(agent_cards) > 21:
break
choice = agent.input(agent_cards, dealer_cards[0])
# print(f'Player chose to: {choice}')
if (choice != Actions.Hit):
break
elif choice == Actions.Double:
agent_cards.append(self.draw(self.deck))
self.bank -= self.bet
# print(f'Player hand: {agent_cards}')
elif choice == Actions.Split and agent_cards[0][0] == agent_cards[1][0]:
hand1 = [agent_cards[0], self.draw(self.deck)]
hand2 = [agent_cards[1], self.draw(self.deck)]
self.bank -= self.bet
# print(f'Player hand1: {hand1}')
choice = agent.input(hand1, dealer_cards[0])
# print(f'Player chose to: {choice}')
if choice == Actions.Hit:
while (BlackJack.countCards(hand1) < 21):
hand1.append(self.draw(self.deck))
# print(f'Player hand1: {hand1}')
if BlackJack.countCards(hand1) > 21:
break
choice = agent.input(hand1, dealer_cards[0])
# print(f'Player chose to: {choice}')
if (choice != Actions.Hit):
break
# print(f'Player hand2: {hand2}')
choice = agent.input(hand2, dealer_cards[0])
# print(f'Player chose to: {choice}')
if choice == Actions.Hit:
while (BlackJack.countCards(hand2) < 21):
hand2.append(self.draw(self.deck))
# print(f'Player hand2: {hand2}')
if BlackJack.countCards(hand2) > 21:
break
choice = agent.input(hand2, dealer_cards[0])
# print(f'Player chose to: {choice}')
if (choice != Actions.Hit):
break
# DealerDealing
while BlackJack.countCards(dealer_cards) < 17:
dealer_cards.append(self.draw(self.deck))
# print(f'Dealer hand: {dealer_cards}')
# Final Count
for hand in [agent_cards, hand1, hand2]:
if hand == None:
continue
if len(hand) == 2 and hand1 == None and BlackJack.countCards(hand) == 21:
# print("BLACK JACK !!!!")
self.bank += self.bet*2.5
elif BlackJack.countCards(hand) == BlackJack.countCards(dealer_cards):
if choice == Actions.Double:
self.bank += self.bet
self.bank += self.bet
elif BlackJack.countCards(hand) > 21:
()
elif BlackJack.countCards(hand) > BlackJack.countCards(dealer_cards):
if choice == Actions.Double:
self.bank += self.bet*2
self.bank += self.bet*2
elif BlackJack.countCards(dealer_cards) > 21:
if choice == Actions.Double:
self.bank += self.bet*2
self.bank += self.bet*2
# print(f'Final bank: {self.bank}')
self.bankHistory.append(self.bank)
if plot and len(self.bankHistory)%10 == 0:
plt.xlim(0, max(300, len(self.bankHistory)+(1-((len(self.bankHistory)%300)/300))*300))
plt.ylim(0, 300)
plt.grid(True)
plt.xlabel("matches played")
plt.ylabel("bank")
plt.plot(self.bankHistory, color='blue')
plt.show()
plt.pause(0.001)
def countCards(cards):
count = 0
aces = 0
for card in cards:
if card[0] in "KQJT":
count += 10
elif card[0] in "23456789":
count += int(card[0])
elif card[0] == "A":
aces += 1
for i in range(aces):
if count + 11 > 21:
count += 1
else:
count += 11
return count
def draw(self, deck):
card = deck.pop(0)
self.drawnCards.append(card)
return card
def getStaticDeck():
deck = []
for card in "A23456789TJQK":
for sign in "SCDH":
deck.append(card + sign)
return deck
class BasicAgent:
def __init__(self) -> None:
pass
def input(self, agent_cards, dealer_card):
# Table Strat
for i in range(len(X)):
c1 = BlackJack.countCards(agent_cards[0])
c2 = BlackJack.countCards(agent_cards[1])
row = BlackJack.countCards(dealer_card)
if c1 == 11:
c1 = 1
if c2 == 11:
c2 = 1
if row == 11:
row = 1
if X[i][0] == c1 and X[i][1] == c2 and X[i][3] == row:
if y[i] == [1, 0, 0, 0]:
return Actions.Stand
elif y[i] == [0, 1, 0, 0]:
return Actions.Hit
elif y[i] == [0, 0, 1, 0]:
return Actions.Split
elif y[i] == [0, 0, 0, 1]:
return Actions.Double
else:
assert False
# print(f'Agent Cards: {agent_cards}')
# print(f'Dealer Card: {dealer_card}')
assert False
# Dealer Strat
#
# if BlackJack.countCards(agent_cards) < 17:
# return Actions.Hit
# else:
# return Actions.Stand
# Manual Mode
#
# choice = input('>')
# if choice == 'H':
# return Actions.Hit
# elif choice == 'S':
# return Actions.Stand
# elif choice == 'SP':
# return Actions.Split
# elif choice == 'DD':
# return Actions.Double
# else:
# print(f'Action "{choice}" not found.')
# return Actions.Stand
if __name__ == '__main__':
bj = BlackJack()
agent = BasicAgent()
while bj.bank > 0:
bj.run(agent)
plt.pause(0)