diff options
Diffstat (limited to 'imago/sgfParser/astNode.py')
-rw-r--r-- | imago/sgfParser/astNode.py | 85 |
1 files changed, 57 insertions, 28 deletions
diff --git a/imago/sgfParser/astNode.py b/imago/sgfParser/astNode.py index ec28c2a..41629ce 100644 --- a/imago/sgfParser/astNode.py +++ b/imago/sgfParser/astNode.py @@ -1,11 +1,11 @@ -from imago.gameLogic.gameTree import GameTree from imago.gameLogic.gameData import GameData from imago.gameLogic.gameMove import GameMove -from imago.gameLogic.gameState import Player +from imago.gameLogic.gameBoard import GameBoard +from imago.data.enums import Player class ASTNode: """Abstract Syntax Tree Node of SGF parser""" - def __init__(self, children=None, leaf=None, props=None): + def __init__(self, children=None, props=None): if children: self.children = children else: @@ -13,8 +13,7 @@ class ASTNode: if props: self.props = props else: - self.props = {} - self.leaf = leaf + self.props = [] def addToSequence(self, move): """Appends a move to the last of the sequence started by this move""" @@ -68,7 +67,7 @@ class ASTNode: gameData.roundInfo = prop.value if prop.name == "RU": # Rules used for the game gameData.rules = prop.value - if prop.name == "SO": # Source of the gamw + if prop.name == "SO": # Source of the game gameData.source = prop.source if prop.name == "TM": # Time limit in seconds gameData.timeInfo = prop.source @@ -77,42 +76,69 @@ class ASTNode: firstMoves = [] for child in self.children: - firstMoves.append(child.toGameMoveTree) + firstMoves.append(child.toGameMoveTree(size)) return GameTree(firstMoves, gameData) - def toGameMoveTree(self): - player = 0 - coords = [] - for prop in self.props: - if prop.name == "B": # White move - player = Player.BLACK - coords = textToCoords(prop.value) - if prop.name == "W": # White move - player = Player.WHITE - coords = textToCoords(prop.value) - gameMove = GameMove(player, coords[0], coords[1]) + def toGameMoveTree(self, previousMove=None): + if previousMove is None: + # Game root node + size = int(self.getPropertyValue("SZ")) + board = GameBoard(size, size) + gameMove = GameMove(board) + else: + coords = [] + player = None + for prop in self.props: + if prop.name == "B" or prop.name == "W": + if prop.value != "tt" and prop.value != "": + coords = textToCoords(prop.value) + if prop.name == "B": + player = Player.BLACK + else: + player = Player.WHITE + if len(coords) == 2: + gameMove = previousMove.addMoveForPlayer(coords[0], coords[1], player) + else: + gameMove = previousMove.addPassForPlayer(player) for child in self.children: - newMove = child.toGameMoveTree() - gameMove.nextMoves.append(newMove) - newMove.previousMove = gameMove + child.toGameMoveTree(previousMove=gameMove) + # Add a couple passes at the end of the match since they are not usually recorded + if len(self.children) == 0 and not gameMove.isPass: + nextMove = gameMove.addPass() + nextMove.addPass() + return gameMove -def textToCoords(text): # Poner en PropertyMove, subclase de Property - col = ord(text[1]) - ord('a') - row = ord(text[0]) - ord('a') - return [row, col] + def hasProperty(self, propertyName): + """Returns True if the node contains a property with the given name.""" + for prop in self.props: + if prop.name == propertyName: + return True + return False + def getPropertyValue(self, propertyName): + """Returns the value of the given property for this node.""" + for prop in self.props: + if prop.name == propertyName: + return prop.value + raise RuntimeError("ASTNode has no property %s" % propertyName) - def toString(self): + def toString(self, debug=False): """Returns a depth-first representation of the tree.""" out = '(' + str(self.props) + ')' - out += "in" + if debug: out += "in" for node in self.children: out = out + node.toString() - out += "out" + if debug: out += "out" return out +def textToCoords(text): # Poner en PropertyMove, subclase de Property + col = ord(text[0]) - ord('a') + row = ord(text[1]) - ord('a') + return [row, col] + + class Property: """Property of a Node""" def __init__(self, name, value): @@ -125,3 +151,6 @@ class Property: self.value.append(value) else: self.value = [self.value, value] + + def toString(self): + return("Property(%s, %s)" % (self.name, self.value)) |