game_state.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:robot-arena 作者: kenganong 项目源码 文件源码
def create_state(board_file, brains):
  lines = [line.rstrip('\n') for line in board_file]
  size = max(len(lines), max(len(line) for line in lines))
  state = RoboRallyGameState(size)
  for row in range(len(lines)):
    for col in range(len(lines[row])):
      char = lines[row][col]
      cell = state.board.get_item((row, col))
      if char == '#':
        cell.content = make_wall()
      elif char == '<':
        cell.content = make_mounted(WEST)
      elif char == '>':
        cell.content = make_mounted(EAST)
      elif char == 'v':
        cell.content = make_mounted(SOUTH)
      elif char == '^':
        cell.content = make_mounted(NORTH)
      elif char == '&':
        cell.floor = None
      elif char == '{':
        cell.floor = LEFT_SPINNER
      elif char == '}':
        cell.floor = RIGHT_SPINNER
      elif char.isdecimal():
        cell.floor = FLAG + char
        state.flags[int(char) - 1] = (row, col)
  state.brains = brains
  empty_cells = [cell for cell, pos in state.board.traverse() if not cell.content and cell.floor == EMPTY]
  for cell, brain in zip(random.sample(empty_cells, len(brains) * NUM_ROBOTS_PER_BRAIN), brains * NUM_ROBOTS_PER_BRAIN):
    cell.content = make_robot(brain, random.choice([NORTH, WEST, EAST, SOUTH]))
  state.calculate_statistics()
  return state
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号