예제 #1
0
  def load(self, board, f):

    orig = ['']

    f_lines = utils.read_stripped_lines(f)
    def next_line():
      if f_lines == []:
        return False
      else:
        orig[0] = f_lines.pop(0)
        return orig[0].split(' ')

    def fail(msg):
      raise basic.BoardFormatException(i18n.i18n('Malformed gbb board') + '\n' +
                                 '  ' + i18n.i18n('Near line:') + ' "' + orig[0].strip('\r\n') + '"\n' +
                                 '  ' + msg)

    # GBB header
    l = next_line()
    if l != ['GBB/1.0']:
      fail(i18n.i18n('Expected header line "GBB/1.0"'))

    l = next_line()
    if len(l) == 3 and l[0] in ['size', 's'] and is_numeric(l[1]) and is_numeric(l[2]):
      width, height = board.size = int(l[1]), int(l[2])
    else:
      fail(i18n.i18n('Expected board size line "size <width> <height>"'))

    board.randomize_header()
    board._clear_board()

    color_to_index = gbs_builtins.COLOR_NAME_TO_INDEX_DICT

    visited_cells = {}

    while True:
      l = next_line()
      if not l: break
      if l[0] in ['head', 'x']:
        if len(l) == 3 and is_numeric(l[1]) and is_numeric(l[2]):
          x, y = int(l[1]), int(l[2])
          if x >= width or y >= height:
            fail(i18n.i18n('"head %u %u" out of bounds') % (x, y))
          board.head = y, x
        else:
          fail(i18n.i18n('Expected head line "head <x> <y>"'))
      elif l[0] in ['cell', 'o']:
        if len(l) >= 3 and is_numeric(l[1]) and is_numeric(l[2]):
          x, y = int(l[1]), int(l[2])
          if (x, y) in visited_cells:
            fail(i18n.i18n('Cell (%u,%u) is repeated.') % (x, y))
          visited_cells[(x, y)] = True
          count_col = {}
          rest = l[3:]
          while rest != []:
            if len(rest) >= 2 and rest[0] in color_to_index and is_numeric(rest[1]):
              coli = color_to_index[rest.pop(0)]
              count_col[coli] = count_col.get(coli, 0) + int(rest.pop(0))
            else:
              fail(i18n.i18n('Expected cell line "cell <x> <y> [<color> <num>]*"'))
          for k, v in count_col.items():
            board.cells[y][x].set_num_stones(k, v)
        else:
          fail(i18n.i18n('Expected cell line "cell <x> <y> [<color> <num>]*"'))
      else:
        fail(i18n.i18n('Expected head line "head <x> <y>" or cell line "cell <x> <y> [<color> <num>]*"'))
 def __init__(self, f, filename='...'):
   self._f = f
   self._filename = filename
   self._f_lines = utils.read_stripped_lines(f)