Skip to content

Instantly share code, notes, and snippets.

@busterb
Created December 7, 2020 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save busterb/2fcd6f95acc89c0b85ef2d08b89930ae to your computer and use it in GitHub Desktop.
Save busterb/2fcd6f95acc89c0b85ef2d08b89930ae to your computer and use it in GitHub Desktop.
# This is an ASCII space game where you are a ship ^ at the bottom of the field
# dodging ‘rocks’ represented by ‘0’ characters. To play, just connect via ‘nc’
# to the port 5555. You have to move left and right with the arrow keys, pressing
# enter each time, but the game gets too fast to play by hand. To solve it, I
# wrote a simple bot to play the game by moving left and right based on the next
# 2 rocks. It doesn’t account for every possible case, but got lucky enough to
# win the first try. After you win, a new HTTP service starts listening on port
# 7878 (it prints a message), which contains the flag for download.
import socket, sys
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('target', 5555))
def move_left():
sock.send(b"\033[D\n")
def move_right():
sock.send(b"\033[C\n")
while(True):
board = sock.recv(1000)
status = sock.recv(1000)
if len(board) < 100:
continue
lines = board[1:].decode('ascii').split("\r\n")
print("\n".join(lines))
my_pos = lines[-2].find("^")
rock_pos = lines[-4].find("0")
next_rock_pos = lines[-5].find("0")
if my_pos == rock_pos:
if my_pos - 1 != next_rock_pos:
if my_pos != 1:
move_left()
else:
move_right()
elif my_pos + 1 != next_rock_pos:
move_right()
print(rock_pos, next_rock_pos, my_pos)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment