import copy
import sys

PLAYER1, PLAYER2, EMPTY, BLOCKED = [0, 1, 2, 3]
S_PLAYER1, S_PLAYER2, S_EMPTY, S_BLOCKED, = ['0', '1', '.', 'x']

CHARTABLE = [(PLAYER1, S_PLAYER1), (PLAYER2, S_PLAYER2), (EMPTY, S_EMPTY), (BLOCKED, S_BLOCKED)]

DIRS = [
    ((-1, 0), "up"),
    ((1, 0), "down"),
    ((0, 1), "right"),
    ((0, -1), "left")
]

#the information of the whole grid
class Board:

    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.cell = [[EMPTY for col in range (0, width)] for row in range(0, height)]

    def parse_cell_char(self, players, row, col, char):
        result = -1
        if char == S_PLAYER1:
            players[0].row = row;
            players[0].col = col;
        elif char == S_PLAYER2:
            players[1].row = row;
            players[1].col = col;
        for (i, symbol) in CHARTABLE:
            if symbol == char:
                result = i
                break
        return result

    def parse_cell(self, players, row, col, data):
        cell = []
        for char in data:
            item = self.parse_cell_char(players, row, col, char)
            cell.append(item)
        return cell

    def parse(self, players, data):
        cells = data.split(',')
        col = 0
        row = 0
        for cell in cells:
            if (col >= self.width):
                col = 0
                row +=1
            self.cell[row][col] = self.parse_cell(players, row, col, cell)
            col += 1

    def in_bounds (self, row, col):
        return row >= 0 and col >= 0 and col < self.width and row < self.height

    def is_legal(self, row, col, my_id):
        enemy_id = my_id ^ 1
        return (self.in_bounds(row, col)) and (not BLOCKED == self.cell[row][col]) and (not enemy_id == self.cell[row][col])

    def is_legal_tuple(self, loc):
        row, col = loc
        return self.is_legal(row, col)

    def get_adjacent(self, row, col):
        result = []
        for (o_row, o_col), _ in DIRS:
            t_row, t_col = o_row + row, o_col + col
            if self.is_legal(t_row, t_col):
                result.append((t_row, t_col))
        return result

    def legal_moves(self, my_id, players):
        my_player = players[my_id]
        result = []
        for ((o_row, o_col), order) in DIRS:
            t_row = my_player.row + o_row
            t_col = my_player.col + o_col
            if self.is_legal(t_row, t_col, my_id):
                result.append(((o_row, o_col), order))
            else:
                pass
        return result
        
    def update_cell(self, row, col, data):
        self.cell[row][col] = data

    def output_cell(self, cell):
        done = False
        for (i, symbol) in CHARTABLE:
            if i == cell:
                if not done:
                    sys.stderr.write(symbol)
                done = True
                break
        if not done:
            sys.stderr.write("!")
            done = True
                
    def output(self):
        for row in self.cell:
            sys.stderr.write("\n")
            for cell in row:
                self.output_cell(cell)
        sys.stderr.write("\n")
        sys.stderr.flush()

    def tostring(self):
        res = ""
        for row in xrange(self.height):
            for col in xrange(self.width):
                res += str(self.cell[row][col])
            res += ","
        return res

[((107, 8, 107, 30), 'sys.stderr.write', 'sys.stderr.write', ({(107, 25, 107, 29): '"""\n"""'}, {}), "('\\n')", False, 'import sys\n'), ((108, 8, 108, 26), 'sys.stderr.flush', 'sys.stderr.flush', ({}, {}), '()', False, 'import sys\n'), ((99, 12, 99, 33), 'sys.stderr.write', 'sys.stderr.write', ({(99, 29, 99, 32): '"""!"""'}, {}), "('!')", False, 'import sys\n'), ((104, 12, 104, 34), 'sys.stderr.write', 'sys.stderr.write', ({(104, 29, 104, 33): '"""\n"""'}, {}), "('\\n')", False, 'import sys\n'), ((95, 20, 95, 44), 'sys.stderr.write', 'sys.stderr.write', ({(95, 37, 95, 43): 'symbol'}, {}), '(symbol)', False, 'import sys\n')]