#!/usr/bin/env python

from GoBoard import GoBoard as gb

class SGFParser(object):

    spotDic = {
    'a' : 0, 'b' : 1, 'c' : 2, 'd' : 3, 'e' : 4,
    'f' : 5, 'g' : 6, 'h' : 7, 'i' : 8, 'j' : 9,
    'k' : 10, 'l' : 11, 'm' : 12, 'n' : 13, 'o' : 14,
    'p' : 15, 'q' : 16, 'r' : 17, 's' : 18
    }

    def __init__(self, filename = None):
        self.__opened = False
        if filename != None:
            self.open(filename)

    def open(self, filename):
        if not isinstance(filename, str):
            self.__opened = False
            raise Exception("SGFParser: open: error: invalid filename")
        with open(filename, "r") as f:
            self.__moves = [s for s in f.read().split(';') if s[0] == 'B' or s[0] == 'W']
        self.__opened = True
        self.__index = 0

    def getNextMove(self):
        while True:
            if not self.hasNextMove():
                return False
            move = self.__moves[self.__index]
            self.__index += 1
            color = gb.black
            if move[0] == 'W':
                color = gb.white
            if move[2] not in SGFParser.spotDic or move[3] not in SGFParser.spotDic:
                continue
            return (SGFParser.spotDic[move[3]], SGFParser.spotDic[move[2]], color)

    def hasNextMove(self):
        if not self.__opened:
            return False
        return self.__index < len(self.__moves)

def test():
    sgf = SGFParser(input("Filename: "))
    board = gb()
    while sgf.hasNextMove():
        move = sgf.getNextMove()
        board.move(move[0], move[1], move[2])
    board.printBoard()

if __name__ == '__main__':
    test()

@@ -347,38 +347,59 @@
      self.__
-opened = False
+moves = %5B%5D%0A        self.__index = 0
 %0A        if 
@@ -475,24 +475,75 @@
  filename):%0A
+        self.__moves = %5B%5D%0A        self.__index = 0%0A
         if n
@@ -576,42 +576,8 @@
 r):%0A
-            self.__opened = False%0A
     
@@ -792,21 +792,311 @@
 f.__
-opened = True
+index = 0%0A%0A    def moveCount(self):%0A        return len(self.__moves)%0A%0A    def getIndex(self):%0A        return self.__index%0A%0A    def setIndex(self, index):%0A        if not isinstance(index, int) or not 0 %3C= index %3C len(self.__moves):%0A            raise Exception('SGFParser: setIndex: error: invalid index')
 %0A   
@@ -1107,33 +1107,37 @@
  self.__index = 
-0
+index
 %0A%0A    def getNex
@@ -1221,35 +1221,85 @@
            r
-eturn False
+aise Exception('SGFParser: getNextMove: error: out of moves')
 %0A           

refactor sgfparser