Created
August 24, 2014 00:19
-
-
Save theepicsnail/dc1dcddb0051ab9e5f16 to your computer and use it in GitHub Desktop.
Flux2 solver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
#figure this stuff out automatically? | |
left,top = 58, 478 | |
right,bottom = 1020, 1440 | |
cell_size = 186 | |
height = (bottom - top) | |
width = (right - left) | |
gap = (width % cell_size)/(width/cell_size - 1) | |
def getScreenshot(): | |
subprocess.call(["adb", "shell", "screencap", "-p", "/sdcard/screen.png"]) | |
subprocess.call(["adb", "pull", "/sdcard/screen.png"]) | |
#subprocess.call(["adb", "shell", "screencap", "-p", "/sdcard/screen.png"]) | |
def gotoNext(): | |
subprocess.call(['adb', 'shell', 'input', 'tap', '1000', '1500']) | |
def rotate(location, direction): | |
start = map(int,( | |
left + cell_size * (location[1] + .5), | |
top + cell_size * (location[0] + .5))) | |
end = ( | |
start[0] + direction[1] * cell_size, | |
start[1] + direction[0] * cell_size | |
) | |
subprocess.call(map(str,['adb', 'shell', 'input', 'swipe', start[0], start[1], end[0], end[1], | |
"100"])) | |
def getField(): | |
getScreenshot() | |
from PIL import Image | |
im = Image.open("screen.png") | |
field = im.crop((left,top,right,bottom)) | |
field.save("field.png") | |
return field | |
def generateTiles(): | |
field = getField(); | |
tiles = [] | |
tile_size = cell_size + gap | |
for y in range(0, height, tile_size): | |
row = [] | |
tiles.append(row) | |
for x in range(0, width, tile_size): | |
tile = field.crop((x,y,x+cell_size,y+cell_size)) | |
tile.save("%s-%s.png" % (y/tile_size, x/tile_size)) | |
row.append(tile) | |
return tiles | |
def buildMap(): | |
world = [] | |
for row in generateTiles(): | |
world_row = [] | |
world.append(world_row) | |
for tile in row: | |
tile_color = 'o' | |
s = sum(tile.getpixel((93,45))) | |
# 0 = red (orange) | |
# 1 = green | |
# 3 = blue | |
# 4 = purple | |
color = 0 | |
char = s | |
if s == 714: # orange | |
color = 0 | |
elif s == 631: | |
color = 3 | |
elif s == 755: | |
color = 4 | |
elif s == 671: | |
color = 1 | |
else: | |
raise "Unexpected color code:" + s | |
if sum(tile.getpixel((93,93-25))) == 1020: # detects up arrow | |
char = "^" | |
elif sum(tile.getpixel((93,93+25))) == 1020: # detects down arrow | |
char = "v" | |
elif sum(tile.getpixel((93-25,93))) == 1020: # detects left arrow | |
char = "<" | |
elif sum(tile.getpixel((93+25,93))) == 1020: # detects right arrow | |
char = ">" | |
elif sum(tile.getpixel((93,93))) == 1020: # detects X | |
char = "X" | |
else: # it's the goal. | |
char = 'O' | |
print "\033[0;" + str(31 +color%10 ) +"m", | |
print "%s" %char, | |
world_row.append((char, color)) | |
print "\033[0;0m" | |
return world | |
def solve_path(world): | |
# find start/end | |
end = None | |
start = None | |
for row_num, row in enumerate(world): | |
for col_num, cell in enumerate(row): | |
if cell[1] == 4: # purple | |
if cell[0] == 'O': | |
end = (row_num, col_num) | |
else: | |
start = (row_num, col_num) | |
print start, '-->', end | |
def at(row, col): | |
if row <0 or col < 0 or row > 4 or col > 4: | |
return ('X', 3) | |
return world[row][col] | |
def cost(ndir, nval): | |
#ndir = (drow, dcol) | |
#nval = arrow neighbor has | |
cost = {(0,1):{'>':2, '<':0}, | |
(1,0):{'v':2, '^':0}} | |
if ndir in cost: | |
if nval in cost[ndir]: | |
return cost[ndir][nval] | |
else: | |
ndir = -1*ndir[0], -1*ndir[1] | |
if nval in cost[ndir]: | |
return 2-cost[ndir][nval] | |
return 1 | |
def check((dist, pos, _), direction): | |
nrow = pos[0] + direction[0] | |
ncol = pos[1] + direction[1] | |
down = at(nrow, ncol) | |
if down[0] in "XO": | |
return | |
c = cost(direction, down[0]) | |
search.put((dist + c, (nrow, ncol), direction)) | |
# bfs | |
from Queue import PriorityQueue as Queue | |
best = {} | |
search = Queue() | |
search.put((0, end, None)) | |
while not search.empty(): | |
node = search.get() | |
if node[1] in best: | |
continue | |
best[node[1]] = node | |
pos = node[1] | |
cell = world[pos[0]][pos[1]] | |
if cell[1] == 4 and cell[0] != 'O': | |
break | |
check(node, (1,0)) | |
check(node, (0,1)) | |
check(node, (0,-1)) | |
check(node, (-1,0)) | |
path = [] | |
cur = start | |
while cur != end: | |
path.append(cur) | |
choice = best[tuple(cur)] | |
r,c = cur | |
r -= choice[2][0] | |
c -= choice[2][1] | |
cur = (r,c) | |
path.append(cur) | |
return path | |
def solve(): | |
world = buildMap() | |
path = solve_path(world) | |
def performMove(start, end): | |
cell = world[start[0]][start[1]] | |
neededDir = end[0]-start[0], end[1]-start[1] | |
curDir = {'^':(-1,0), 'v':(1,0), '<':(0,-1), '>':(0,1)}[cell[0]] | |
if curDir == neededDir: | |
return | |
if curDir[0]*-1 == neededDir[0] and curDir[1]*-1 == neededDir[1]: | |
rotate(start, (neededDir[1],neededDir[0])) | |
rotate(start, neededDir) | |
move_start = path[0] | |
for move_next in path[1:]: | |
performMove(move_start, move_next) | |
move_start= move_next | |
print world[path[0][0]][path[0][1]] | |
import time | |
while True: | |
solve() | |
time.sleep(1) | |
gotoNext() | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment