-
Notifications
You must be signed in to change notification settings - Fork 5
/
blackjack.py
292 lines (273 loc) · 11.9 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
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
from time import sleep
from numpy import where as np_where
from numpy import sqrt as np_sqrt
import pyautogui
from pyscreeze import screenshot
from cv2 import resize, matchTemplate, TM_CCOEFF_NORMED, imread, minMaxLoc
from PyQt5.QtCore import QThread, pyqtSignal
from constant import (
NUMBER,
COLOR,
CHEAT_SHEET,
OP_POS,
WINDOW_WIDTH,
WINDOW_HEIGHT,
BUTTON_WIDTH,
BUTTON_HEIGHT,
FIRST_HAND_X,
SECOND_HAND_X,
SPLIT_FIRST_GROUP_FIRST_HAND_X,
SPLIT_FIRST_GROUP_SECOND_HAND_X,
SPLIT_SECOND_GROUP_FIRST_HAND_X,
SPLIT_SECOND_GROUP_SECOND_HAND_X,
)
def is_close(pt1, pt2, threshold=8):
return np_sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2) < threshold
def card_num_from_card_name(card_name):
if card_name[1] in ["t", "j", "q", "k"]:
return 10
elif card_name[1] == "a":
return 11
else:
return int(card_name[1])
def card_num_str_from_card_name(card_name):
if card_name[1] in ["t", "j", "q", "k"]:
return "10"
elif card_name[1] == "a":
return "A"
else:
return card_name[1]
def card_suite_from_two_card_num(card_num1: int, card_num2: int) -> str:
if card_num1 == card_num2:
if card_num1 == 11 and card_num2 == 11:
return "A,A"
return str(card_num1) + "," + str(card_num2)
elif card_num1 == 11 or card_num2 == 11:
if card_num1 == 11 and card_num2 == 11:
return "A,A"
elif card_num1 == 11:
return "A," + str(card_num2)
else:
return "A," + str(card_num1)
else:
return str(card_num1 + card_num2)
class ProgramThread(QThread):
statUpdated = pyqtSignal(float, str)
roundInformUpdated = pyqtSignal(str, str, str)
def __init__(self, bet_amount, language):
super().__init__()
self.bet_amount = bet_amount
self.language = language
self.running = True
self.card_images = {}
self.image_prefix = "image/" + self.language + "/"
for num in NUMBER:
for col in COLOR:
card_name = col + num
card_image = imread(r"image/card/" + card_name + ".png", 0)
self.card_images[card_name] = card_image
def compare(self, target, screen):
res = matchTemplate(screen, target, TM_CCOEFF_NORMED)
_, val, _, loc = minMaxLoc(res)
return loc if val >= 0.9 else None
def clickz(self, top_left):
x = top_left[0] + BUTTON_WIDTH / 2
y = top_left[1] + BUTTON_HEIGHT / 2
pyautogui.click(x, y, button="left", duration=0.25)
pyautogui.moveTo(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, duration=0.25)
def run(self):
is_doubled = False
split_round = 0
win = imread(self.image_prefix + "win.png", 0)
lose = imread(self.image_prefix + "lose.png", 0)
bust = imread(self.image_prefix + "bust.png", 0)
draw = imread(self.image_prefix + "draw.png", 0)
double = imread(self.image_prefix + "double.png", 0)
stand = imread(self.image_prefix + "stand.png", 0)
blackjack = imread(self.image_prefix + "blackjack.png", 0)
bet = imread(r"image/bet/bet" + self.bet_amount + ".png", 0)
while self.running:
screenshot("image/screen.png")
screen = imread(r"image/screen.png", 0)
screen = resize(screen, (WINDOW_WIDTH, WINDOW_HEIGHT))
if self.compare(win, screen):
amount_rate = 1
if is_doubled:
amount_rate = 2
is_doubled = False
split_round = 0
self.statUpdated.emit(amount_rate, "win")
sleep(2)
elif self.compare(lose, screen):
amount_rate = 1
if is_doubled:
amount_rate = 2
is_doubled = False
split_round = 0
self.statUpdated.emit(amount_rate, "lose")
sleep(2)
elif self.compare(bust, screen):
_, y = self.compare(bust, screen)
if y < WINDOW_HEIGHT / 2:
continue
amount_rate = 1
if is_doubled:
amount_rate = 2
is_doubled = False
split_round = 0
self.statUpdated.emit(amount_rate, "lose")
sleep(2)
elif self.compare(draw, screen):
amount_rate = 1
if is_doubled:
amount_rate = 2
is_doubled = False
split_round = 0
self.statUpdated.emit(amount_rate, "draw")
sleep(2)
elif self.compare(blackjack, screen):
_, y = self.compare(blackjack, screen)
if y < WINDOW_HEIGHT / 2:
continue
amount_rate = 1.5
is_doubled = False
split_round = 0
self.statUpdated.emit(amount_rate, "win")
sleep(2)
elif self.compare(double, screen):
# It's the first round
first_card = ""
second_card = ""
dealer_card = ""
for card_name, card_image in self.card_images.items():
res = matchTemplate(screen, card_image, TM_CCOEFF_NORMED)
loc = np_where(res >= 0.95)
for x, y in zip(*loc[::-1]):
if y < WINDOW_HEIGHT / 2:
dealer_card = card_name
else:
if split_round:
if split_round == 1:
first_minn_x, first_maxx_x = (
SPLIT_SECOND_GROUP_FIRST_HAND_X
)
second_minn_x, second_maxx_x = (
SPLIT_SECOND_GROUP_SECOND_HAND_X
)
if first_minn_x < x < first_maxx_x:
first_card = card_name
elif second_minn_x < x < second_maxx_x:
second_card = card_name
elif split_round == 2:
first_minn_x, first_maxx_x = (
SPLIT_FIRST_GROUP_FIRST_HAND_X
)
second_minn_x, second_maxx_x = (
SPLIT_FIRST_GROUP_SECOND_HAND_X
)
if first_minn_x < x < first_maxx_x:
first_card = card_name
elif second_minn_x < x < second_maxx_x:
second_card = card_name
else:
first_minn_x, first_maxx_x = FIRST_HAND_X
second_minn_x, second_maxx_x = SECOND_HAND_X
if first_minn_x < x < first_maxx_x:
first_card = card_name
elif second_minn_x < x < second_maxx_x:
second_card = card_name
if dealer_card and first_card and second_card:
break
if "" in [first_card, second_card, dealer_card]:
continue
card_num1, card_num2 = card_num_from_card_name(
first_card
), card_num_from_card_name(second_card)
if card_num1 + card_num2 == 21:
self.roundInformUpdated.emit(
dealer_card, first_card + "," + second_card, "stand"
)
continue
dealer_card_num_str = card_num_str_from_card_name(dealer_card)
strategy = CHEAT_SHEET[
(
card_suite_from_two_card_num(card_num1, card_num2),
dealer_card_num_str,
)
]
if strategy == "double":
is_doubled = True
if strategy == "split":
if split_round > 0:
# we can't split again
if card_num1 + card_num2 < 12:
strategy = "hit"
else:
strategy = "stand"
if split_round == 1:
split_round = 2
else:
split_round = 0
else:
split_round = 1
elif split_round == 1:
split_round = 2
else:
split_round = 0
self.roundInformUpdated.emit(
dealer_card, first_card + "," + second_card, strategy
)
self.clickz(OP_POS[strategy])
elif self.compare(stand, screen):
# which means it's the second round and could have mulitple cards
dealer_card = ""
total_points = 0
detected_cards = []
for card_name, card_image in self.card_images.items():
res = matchTemplate(screen, card_image, TM_CCOEFF_NORMED)
loc = np_where(res >= 0.95)
for x, y in zip(*loc[::-1]):
already_detected = False
for detected_card in detected_cards:
detect_card_name, detect_card_pos = detected_card
if detect_card_name == card_name and is_close(
detect_card_pos, (x, y)
):
already_detected = True
break
if not already_detected:
if y < WINDOW_HEIGHT / 2:
dealer_card = card_name
else:
total_points += card_num_from_card_name(card_name)
detected_cards.append((card_name, (x, y)))
cards = [name for name, pt in detected_cards if name != dealer_card]
if dealer_card == "" or total_points == 0 or len(cards) < 2:
continue
if total_points >= 21:
if total_points % 10 == 1:
self.roundInformUpdated.emit(
dealer_card, ",".join(cards), "stand"
)
continue
elif total_points % 10 == 0:
total_points = 20
else:
total_points = total_points % 10 + 10
dealer_card_num_str = card_num_str_from_card_name(dealer_card)
strategy = CHEAT_SHEET[
(
str(total_points),
dealer_card_num_str,
)
]
if strategy == "double":
strategy = "hit"
self.roundInformUpdated.emit(dealer_card, ",".join(cards), strategy)
self.clickz(OP_POS[strategy])
elif self.compare(bet, screen) is not None:
loc = self.compare(bet, screen)
self.clickz(loc)
def stop(self):
self.terminate()
self.wait()