summaryrefslogtreecommitdiff
path: root/processbot.py
blob: 15e7de06446d51bf0580ba3802c8c4404ef51d50 (about) (plain)
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
import sys
import subprocess

class BotWrapper(object):
    def __init__(self, process):
        self.process = process
        self.__name__ = process

    def __call__(self, board, (x, y)):
        height = len(board)
        width = len(board[0])

        letter = board[y][x].lower()

        proc = subprocess.Popen(
            [self.process],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
        )

        board = '\n'.join([''.join(row) for row in board])

        print>>proc.stdin, width, height, letter
        print>>proc.stdin, board
        proc.stdin.close()
        proc.wait()

        assert proc.returncode == 0, 'Snake died.'
        output = proc.stdout.read()
        return output.strip()

def process_input(fp=sys.stdin):
    width, height, letter = fp.readline().split()

    width = int(width)
    height = int(height)
    letter = letter.upper()

    position = None

    board = []
    for i in xrange(height):
        row = fp.readline()[:-1]
        assert len(row) == width

        pos = row.find(letter)
        if pos >= 0:
            position = (pos, i)

        board.append(list(row))

    return board, position