play 0hh1.com with pyautogui
import pyautogui | |
# constants | |
O, R, B = -1, 1, 0 | |
width = 50 | |
# global variables | |
topy, topx = None, None | |
mat = [] | |
def setup(): | |
global topy, topx | |
x_pos = pyautogui.locateOnScreen("x.png") | |
if x_pos is None: | |
print 'Cannot find "x" on screen. Exit.' | |
exit() | |
topy, topx = x_pos[0] - 226, x_pos[1] + 70 | |
im = pyautogui.screenshot(region=(topy, topx, 10 * width, 10 * width)) | |
for i in xrange(10): | |
mat.append([]) | |
for j in xrange(10): | |
rgb = im.getpixel((j * width + width / 2, i * width + width / 2)) | |
mat[-1].append(R if rgb[0] >= 180 else | |
B if rgb[2] >= 180 else O) | |
def click(i, j): | |
pyautogui.click(topy + j * width + width / 2, topx + i * width + width / 2, | |
clicks=2 - mat[i][j], interval=0.05) | |
def scan_end(): | |
found = False | |
for i in xrange(10): | |
for j in xrange(10): | |
if mat[i][j] == O: | |
continue | |
if j + 1 < 10 and mat[i][j] == mat[i][j + 1]: | |
if j > 0 and mat[i][j - 1] == O: | |
found = True | |
mat[i][j - 1] = 1 - mat[i][j] | |
click(i, j - 1) | |
if j + 2 < 10 and mat[i][j + 2] == O: | |
found = True | |
mat[i][j + 2] = 1 - mat[i][j] | |
click(i, j + 2) | |
if i + 1 < 10 and mat[i][j] == mat[i + 1][j]: | |
if i > 0 and mat[i - 1][j] == O: | |
found = True | |
mat[i - 1][j] = 1 - mat[i][j] | |
click(i - 1, j) | |
if i + 2 < 10 and mat[i + 2][j] == O: | |
found = True | |
mat[i + 2][j] = 1 - mat[i][j] | |
click(i + 2, j) | |
return found | |
def scan_middle(): | |
found = False | |
for i in xrange(10): | |
for j in xrange(10): | |
if mat[i][j] == O: | |
continue | |
if j + 2 < 10 and mat[i][j] == mat[i][j + 2]: | |
if mat[i][j + 1] == O: | |
found = True | |
mat[i][j + 1] = 1 - mat[i][j] | |
click(i, j + 1) | |
if i + 2 < 10 and mat[i][j] == mat[i + 2][j]: | |
if mat[i + 1][j] == O: | |
found = True | |
mat[i + 1][j] = 1 - mat[i][j] | |
click(i + 1, j) | |
return found | |
def scan_blance(): | |
found = False | |
for i in xrange(10): | |
cnt = [0, 0, 0] | |
for j in xrange(10): | |
cnt[mat[i][j]] += 1 | |
color = B if cnt[R] == 5 else R if cnt[B] == 5 else O | |
if color == O: | |
continue | |
for j in xrange(10): | |
if mat[i][j] == O: | |
found = True | |
mat[i][j] = color | |
click(i, j) | |
for j in xrange(10): | |
cnt = [0, 0, 0] | |
for i in xrange(10): | |
cnt[mat[i][j]] += 1 | |
color = B if cnt[R] == 5 else R if cnt[B] == 5 else O | |
if color == O: | |
continue | |
for i in xrange(10): | |
if mat[i][j] == O: | |
found = True | |
mat[i][j] = color | |
click(i, j) | |
return found | |
def scan_conflicts(): | |
found = False | |
for i1 in xrange(10): | |
jj = [j for j in xrange(10) if mat[i1][j] == O] | |
if len(jj) != 2: | |
continue | |
for i2 in xrange(10): | |
if all(mat[i2][j] != O for j in jj) and\ | |
all(mat[i1][j] == mat[i2][j] | |
for j in xrange(10) if j not in jj): | |
found = True | |
mat[i1][jj[0]] = mat[i2][jj[1]] | |
mat[i1][jj[1]] = mat[i2][jj[0]] | |
click(i1, jj[0]) | |
click(i1, jj[1]) | |
break | |
for j1 in xrange(10): | |
ii = [i for i in xrange(10) if mat[i][j1] == O] | |
if len(ii) != 2: | |
continue | |
for j2 in xrange(10): | |
if all(mat[i][j2] != O for i in ii) and\ | |
all(mat[i][j1] == mat[i][j2] | |
for i in xrange(10) if i not in ii): | |
found = True | |
mat[ii[0]][j1] = mat[ii[1]][j2] | |
mat[ii[1]][j1] = mat[ii[0]][j2] | |
click(ii[0], j1) | |
click(ii[1], j1) | |
break | |
return found | |
def main(): | |
setup() | |
pyautogui.click(topy - 10, topx - 10) | |
while scan_end() or scan_middle() or\ | |
scan_blance() or scan_conflicts(): | |
pass | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment