diff options
Diffstat (limited to 'imago/gameLogic/gameMove.py')
-rw-r--r-- | imago/gameLogic/gameMove.py | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/imago/gameLogic/gameMove.py b/imago/gameLogic/gameMove.py index 224e120..6d2d464 100644 --- a/imago/gameLogic/gameMove.py +++ b/imago/gameLogic/gameMove.py @@ -7,12 +7,13 @@ class GameMove: Tree: the board can be empty, or the move can consist of more than one added or removed stones.""" - def __init__(self, board, coords=None, isPass=False): + def __init__(self, board, coords=None, isPass=False, playerWhoPassed=None): self.board = board self.nextMoves = [] self.previousMove = None self.isPass = isPass self.coords = coords + self.playerWhoPassed = playerWhoPassed def getRow(self): """Returns the row of the vertex the move was played on.""" @@ -31,7 +32,7 @@ class GameMove: if self.isPass: if self.previousMove is None: return Player.BLACK - return Player.otherPlayer(self.previousMove.getPlayer()) + return self.playerWhoPassed if self.coords is None: # Not pass and no coordinates: root move of the tree return Player.EMPTY @@ -65,15 +66,30 @@ class GameMove: def getPlayableVertices(self): """Returns a set with the playable vertices.""" - playables = set() + return self._getVerticesByFilter(self.board.isPlayable) + + def getSensibleVertices(self): + """Returns a set with the sensible vertices.""" + return self._getVerticesByFilter(self.board.isSensible) + + def _getVerticesByFilter(self, filterFunction): + """Returns a set with the vertices which fill a requirement.""" + vertices = set() player = self.getNextPlayer() prevBoards = self.getThisAndPrevBoards() for row in range(self.board.getBoardHeight()): for col in range(self.board.getBoardWidth()): - isPlayable, _ = self.board.isPlayable(row, col, player, prevBoards) - if isPlayable: - playables.add((row, col)) - return playables + valid, _ = filterFunction(row, col, player, prevBoards) + if valid: + vertices.add((row, col)) + return vertices + + def addMoveByCoords(self, coords): + """Adds a move to the next moves list creating its board from this move's board + plus a new stone at the specified coordinates. + """ + return self.addMove(coords[0], coords[1]) + def addMove(self, row, col): """Adds a move to the next moves list creating its board from this move's board @@ -98,12 +114,24 @@ class GameMove: def addPass(self): """Adds a pass move to the next moves list.""" + return self.addPassForPlayer(self.getNextPlayer()) + + def addPassForPlayer(self, player): + """Adds a pass move for the given player to the next moves list.""" newBoard = self.board.getDeepCopy() - newMove = GameMove(newBoard, True) + newMove = GameMove(newBoard, isPass=True, playerWhoPassed=player) newMove.previousMove = self self.nextMoves.append(newMove) return newMove + def toString(self): + """Returns the coordinates of the move as a string.""" + if self.isPass: + return "Pass" + if self.coords is None: + return "Root move" + return "(%d, %d)" % (self.getRow(), self.getCol()) + def printBoard(self): """Prints the board as of this move.""" self.board.printBoard() |