Created
September 10, 2011 23:55
-
-
Save nooodl/1208967 to your computer and use it in GitHub Desktop.
rfkbot for acehack.sovrappensero.info
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 pexpect, ANSI | |
import sys | |
from random import choice | |
NAME = 'rfkbot' | |
PASS = 'kitten' | |
HARD_MODE = True | |
login = 'l{0}\n{1}\n{2} '.format(NAME, PASS, 'R' if HARD_MODE else 'r') | |
while True: | |
# Stupid server stuff | |
child = pexpect.spawn('telnet acehack.sovrappensiero.info') | |
child.expect('=>') | |
child.send(login) | |
child.expect(pexpect.TIMEOUT, timeout=0.4) | |
term = ANSI.ANSI() | |
term.write(child.before.replace('\r', '\n')) | |
screen = term.get_region(3, 1, 24, 80) | |
# We did the stupid server stuff, now let's read the screen | |
robot = None | |
objs = [] | |
for y, row in enumerate(screen): | |
assert len(row) == 80 | |
for x, ch in enumerate(row): | |
if ch == '#': | |
robot = x, y | |
elif ch != ' ': | |
objs.append((x, y)) | |
vikeys = { | |
(-1, 0): 'h', ( 0, 1): 'j', ( 0, -1): 'k', ( 1, 0): 'l', | |
(-1, -1): 'y', ( 1, -1): 'u', (-1, 1): 'b', ( 1, 1): 'n', | |
} | |
# 1. Try to move towards the next object | |
# 2. If blocked, move in a random direction instead | |
# 3. After hitting the object, pop it off, find the next one | |
moves = [] | |
while objs: | |
dx = cmp(objs[0][0], robot[0]) | |
dy = cmp(objs[0][1], robot[1]) | |
while (robot[0] + dx, robot[1] + dy) in objs[1:]: | |
dx, dy = choice(vikeys.keys()) | |
if (robot[0] + dx, robot[1] + dy) == objs[0]: | |
objs.pop(0) | |
robot = robot[0] + dx, robot[1] + dy | |
moves.append(vikeys[dx, dy]) | |
child.send(''.join(moves) + 'qq') | |
child.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment