<commit_before>import re

import logging
logger = logging.getLogger(__name__)

TAVERN = 0
AIR = -1
WALL = -2

PLAYER1 = 1
PLAYER2 = 2
PLAYER3 = 3
PLAYER4 = 4

AIM = {'North': (-1, 0),
       'East': (0, 1),
       'South': (1, 0),
       'West': (0, -1),
       'Stay': (0, 0)
       }

class HeroTile:
    def __init__(self, id):
        self.id = id

class MineTile:
    def __init__(self, heroId = None):
        self.heroId = heroId

class Game:
    def __init__(self, state):
        self.state = state
        self.board = Board(state['game']['board'])
        self.heroes = [Hero(state['game']['heroes'][i]) for i in range(len(state['game']['heroes']))]
        self.me = Hero(state['hero'])
        self.my_intpos = self.me.pos['x'], self.me.pos['y']
        self.mines_locs = {}
        self.heroes_locs = {}
        self.taverns_locs = set([])
        for row in range(len(self.board.tiles)):
            for col in range(len(self.board.tiles[row])):
                obj = self.board.tiles[row][col]
                if isinstance(obj, MineTile):
                    self.mines_locs[(row, col)] = obj.heroId
                elif isinstance(obj, HeroTile):
                    self.heroes_locs[(row, col)] = obj.id
                elif (obj == TAVERN):
                    self.taverns_locs.add((row, col))



class Board:
    def __parseTile(self, str):
        if (str == '  '):
            return AIR
        if (str == '##'):
            return WALL
        if (str == '[]'):
            return TAVERN
        match = re.match('\$([-0-9])', str)
        if (match):
            return MineTile(match.group(1))
        match = re.match('\@([0-9])', str)
        if (match):
            return HeroTile(match.group(1))

    def __parseTiles(self, tiles):
        vector = [tiles[i:i+2] for i in range(0, len(tiles), 2)]
        matrix = [vector[i:i+self.size] for i in range(0, len(vector), self.size)]

        return [[self.__parseTile(x) for x in xs] for xs in matrix]

    def __init__(self, board):
        self.size = board['size']
        self.tiles = self.__parseTiles(board['tiles'])

    def passable(self, loc):
        'true if can walk through'
        x, y = loc
        pos = self.tiles[x][y]
        return (pos != WALL) and (pos != TAVERN) and not isinstance(pos, MineTile)

    def is_wall(self, loc):
        'true if loc is wall'
        x, y = loc
        pos = self.tiles[x][y]
        return pos == WALL

    def is_tavern(self, loc):
        'true if loc is tavern'
        x, y = loc
        pos = self.tiles[x][y]
        return pos == TAVERN

    def is_mine(self, loc):
        'true if loc is mine'
        x, y = loc
        pos = self.tiles[x][y]
        return isinstance(pos, MineTile)

    def to(self, loc, direction):
        'calculate a new location given the direction'
        row, col = loc
        d_row, d_col = AIM[direction]
        n_row = row + d_row
        if (n_row < 0): n_row = 0
        if (n_row > self.size): n_row = self.size
        n_col = col + d_col
        if (n_col < 0): n_col = 0
        if (n_col > self.size): n_col = self.size

        return (n_row, n_col)



class Hero:
    def __init__(self, hero):
        if hero['crashed'] == 'true':
            logger.fatal("Hero crashed.")
        self.heroId = hero['id']
        self.name = hero['name']
        self.pos = hero['pos']
        self.life = hero['life']
        self.gold = hero['gold']<commit_msg>fix some tile check functions, and make bounds checking explicit<commit_after>import re

import logging
logger = logging.getLogger(__name__)

TAVERN = 0
AIR = -1
WALL = -2

PLAYER1 = 1
PLAYER2 = 2
PLAYER3 = 3
PLAYER4 = 4

AIM = {'North': (-1, 0),
       'East': (0, 1),
       'South': (1, 0),
       'West': (0, -1),
       'Stay': (0, 0)
       }

class HeroTile:
    def __init__(self, id):
        self.id = id

class MineTile:
    def __init__(self, heroId = None):
        self.heroId = heroId

class Game:
    def __init__(self, state):
        self.state = state
        self.board = Board(state['game']['board'])
        self.heroes = [Hero(state['game']['heroes'][i]) for i in range(len(state['game']['heroes']))]
        self.me = Hero(state['hero'])
        self.my_intpos = self.me.pos['x'], self.me.pos['y']
        self.mines_locs = {}
        self.heroes_locs = {}
        self.taverns_locs = set([])
        for row in range(len(self.board.tiles)):
            for col in range(len(self.board.tiles[row])):
                obj = self.board.tiles[row][col]
                if isinstance(obj, MineTile):
                    self.mines_locs[(row, col)] = obj.heroId
                elif isinstance(obj, HeroTile):
                    self.heroes_locs[(row, col)] = obj.id
                elif (obj == TAVERN):
                    self.taverns_locs.add((row, col))



class Board:
    def __parseTile(self, str):
        if (str == '  '):
            return AIR
        if (str == '##'):
            return WALL
        if (str == '[]'):
            return TAVERN
        match = re.match('\$([-0-9])', str)
        if (match):
            return MineTile(match.group(1))
        match = re.match('\@([0-9])', str)
        if (match):
            return HeroTile(match.group(1))

    def __parseTiles(self, tiles):
        vector = [tiles[i:i+2] for i in range(0, len(tiles), 2)]
        matrix = [vector[i:i+self.size] for i in range(0, len(vector), self.size)]

        return [[self.__parseTile(x) for x in xs] for xs in matrix]

    def __init__(self, board):
        self.size = board['size']
        self.tiles = self.__parseTiles(board['tiles'])

    def is_passable(self, loc):
        'true if can walk through'
        x, y = loc
        pos = self.tiles[x][y]
        return ((pos != WALL) and (pos != TAVERN) and not isinstance(pos, MineTile)
                and x < self.size and x > 0
                and y < self.size and y > 0)

    def can_step_to(self, loc):
        """" true if can move in this direction

        This differs from is_passable because it returns True for taverns and mines,
        because stepping to these is legal, even though the hero doesn't move.
        """
        x, y = loc
        pos = self.tiles[x][y]
        return self.is_passable(loc) or self.is_tavern(loc) or self.is_mine(loc)

    def is_wall(self, loc):
        'true if loc is wall'
        x, y = loc
        pos = self.tiles[x][y]
        return pos == WALL

    def is_tavern(self, loc):
        'true if loc is tavern'
        x, y = loc
        pos = self.tiles[x][y]
        return pos == TAVERN

    def is_mine(self, loc):
        'true if loc is mine'
        x, y = loc
        pos = self.tiles[x][y]
        return isinstance(pos, MineTile)

    def to(self, loc, direction):
        """calculate a new location given the direction

        New location may not be a legal location, if e.g. the location moves off the map
        """
        row, col = loc
        d_row, d_col = AIM[direction]
        n_row = row + d_row
        n_col = col + d_col
        return (n_row, n_col)



class Hero:
    def __init__(self, hero):
        if hero['crashed'] == 'true':
            logger.fatal("Hero crashed.")
        self.heroId = hero['id']
        self.name = hero['name']
        self.pos = hero['pos']
        self.life = hero['life']
        self.gold = hero['gold']
{'fraction_non_alphanumeric': 0.1434219495569189, 'fraction_numerical': 0.0092706203135651, 'mean_word_length': 1.8984591070723034, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1471674', 'n_tokens_mistral': 2525, 'n_tokens_neox': 2430, 'n_words': 673}