-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
252 lines (198 loc) · 6.93 KB
/
main.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
import numpy as np
from PIL import ImageGrab, Image
from pynput.mouse import Button, Controller
from pynput.keyboard import Key
from pynput.keyboard import Controller as KeyboardController
import time
import math
import cv2
import random
import json
# GLOBAL VARIABLES --------------------------------------------------------------------------------
log_to_console = True
offset_x = 0
offset_y = -4
game_coords = [0, 0, 0, 0]
player_number = 0
player_coords = {
0: [194*2, 140*2],
1: [630*2, 143*2],
2: [769*2, 332*2],
3: [624*2, 522*2],
4: [202*2, 523*2],
5: [54*2, 331*2]
}
river_coords = [(292*2)+offset_x,(311*2)+offset_y]
# idle, on-turn,
current_state = 'idle'
RAISE_BET_COORDS = [796*2, 775*2]
RAISE_BET_COORDS_BOX = [796*2, 775*2]
FOLD_COORDS = [480*2, 775*2]
CALL_COORDS = [635*2, 775*2]
# -------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------
# GLOBAL OBJECTS ----------------------------------------------------------------------------------
mouse = Controller()
keyboard = KeyboardController()
# -------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------
# HELPER FUNCTIONS --------------------------------------------------------------------------------
def debug(string):
if log_to_console:
print(string)
def current_game_state():
coords_red_button = [FOLD_COORDS[0], FOLD_COORDS[1], FOLD_COORDS[0] + 20, FOLD_COORDS[1] + 20]
button_red_img = ImageGrab.grab(bbox=coords_red_button)
#button_red_img.save("testRed.png")
pass_condition = True
for pixel in button_red_img.getdata():
if (pixel[0] >= 180 and pixel[0] < 215) and (pixel[1] >= 70 and pixel[1] < 100) and (pixel[2] >= 40 and pixel[2] < 100):
continue
else:
pass_condition = False
break
if pass_condition:
return 'on-turn'
else:
return 'idle'
# Will return current pot, current drawn cards, current bet
def get_game_info():
game_cards = []
my_cards = []
y = 0
bounding_box = [river_coords[0], river_coords[1], river_coords[0]+30, river_coords[1]+76]
for x in range(0,5):
card_image = ImageGrab.grab(bbox=bounding_box)
card = get_card_value(card_image)
y += 132
bounding_box = [river_coords[0]+y, river_coords[1], river_coords[0]+30+y, river_coords[1]+76]
if card != '' and card not in game_cards:
game_cards.append(card)
my_cards_coords = player_coords[player_number]
y = 0
bounding_box = [my_cards_coords[0], my_cards_coords[1], my_cards_coords[0]+30, my_cards_coords[1]+72]
for x in range(0,2):
card_image = ImageGrab.grab(bbox=bounding_box)
card_image.save(f'my_card_{x}.png')
card = get_card_value(card_image)
y += 62
bounding_box = [my_cards_coords[0]+y, my_cards_coords[1], my_cards_coords[0]+30+y, my_cards_coords[1]+72]
if card != '' and card not in game_cards:
my_cards.append(card)
return {
'my_cards' : my_cards,
'game_cards': game_cards
}
def suggestion_action(game_info):
print(game_info)
my_cards = game_info['my_cards']
game_cards = game_info['game_cards']
if len(game_cards) == 0:
# My first turn
debug('No river detected...')
else:
pass
return random.choice(['raise', 'fold', 'check', 'call'])
def get_card_value(card_image):
blackV = False
white = 0
red = 0
black = 0
otherPixel = 0
for pixel in card_image.getdata():
if pixel[0] == 0 and pixel[1] == 0 and pixel[2] == 0 and pixel[3] == 255:
blackV = True
for pixel in card_image.getdata():
if (pixel[0] >= 245 and pixel[0]) <= 255 and (pixel[1] >= 245 and pixel[1] and (pixel[2] >= 245 and pixel[2]) and (pixel[3] >= 245 and pixel[3])):
white += 1
else:
otherPixel += 1
if blackV:
black = otherPixel
else:
red = otherPixel
card_pixel_values_file = open('card_pixel_values.json','r+')
card_pixel_values = {}
str_json = card_pixel_values_file.read()
if str_json != "":
card_pixel_values = json.loads(str_json)
for k,v in card_pixel_values.items():
if k.find("D") != -1 or k.find("H") != -1:
if red == v["red"]:
return k
else:
if black == v["black"]:
return k
return ""
# -------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------
# GAME ACTIONS ------------------------------------------------------------------------------------
def raise_bet(bet):
mouse.position = (0, 0)
mouse.move(RAISE_BET_COORDS[0]/2, RAISE_BET_COORDS[1]/2)
time.sleep(0.1)
mouse.click(Button.left, 1)
mouse.position = (0, 0)
time.sleep(0.1)
# Then click the bet box and raise X amount
mouse.move(RAISE_BET_COORDS_BOX[0]/2, RAISE_BET_COORDS_BOX[1]/2)
bet_string = str(bet)
for l in bet_string:
keyboard.press(l)
keyboard.release(l)
time.sleep(0.1)
mouse.position = (0, 0)
def fold():
mouse.position = (0, 0)
mouse.move(FOLD_COORDS[0]/2, FOLD_COORDS[1]/2)
time.sleep(0.1)
mouse.click(Button.left, 1)
mouse.position = (0, 0)
def check():
mouse.position = (0, 0)
mouse.move(CALL_COORDS[0]/2, CALL_COORDS[1]/2)
time.sleep(0.1)
mouse.click(Button.left, 1)
mouse.position = (0, 0)
def call():
mouse.position = (0, 0)
mouse.move(CALL_COORDS[0]/2, CALL_COORDS[1]/2)
time.sleep(0.1)
mouse.click(Button.left, 1)
mouse.position = (0, 0)
# -------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------
def main():
#player_number_string = input('Please select player number (0-5):')
#player_number = int(player_number_string)
while True:
# Frame speed
time.sleep(1)
# Get current game state
current_state = current_game_state()
debug('Grabed new frame...')
debug(current_state)
if current_state == 'idle':
game_info = get_game_info()
pass
elif current_state == 'on-turn':
game_info = get_game_info()
action = suggestion_action(game_info)
debug(f'Suggested action: {action}')
if action == 'raise':
raise_bet(10)
elif action == 'fold':
fold()
elif action == 'check':
check()
elif action == 'call':
call()
current_state = 'idle'
# -------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------
main()