Skip to content

Commit

Permalink
Unroll recursion in GameNode.board() (niklasf#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Apr 18, 2020
1 parent ddf915c commit 324561c
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions chess/pgn.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,28 +144,44 @@ def __init__(self) -> None:
def dangling_node(cls) -> "GameNode":
return GameNode()

# TODO: Unroll
def board(self, *, _cache: bool = True) -> chess.Board:
def _board(self) -> chess.Board:
assert False, "cannot get board of dangling GameNode"

def board(self) -> chess.Board:
"""
Gets a board with the position of the node.
For the root node, this is the default starting position (for the
``Variant``) unless the ``FEN`` header tag is set.
It's a copy, so modifying the board will not alter the game.
"""
assert self.parent is not None and self.move is not None, "cannot get board of dangling GameNode"

if self.board_cached is not None:
board = self.board_cached()
if board is not None:
return board.copy()
board_cached = self.board_cached()
if board_cached is not None:
return board_cached.copy()

stack = []
node = self
while True:
if node.parent is None or node.move is None:
board = node._board()
break
elif node.board_cached is not None:
board_cached = node.board_cached()
if board_cached is not None:
board = board_cached.copy()
break
stack.append(node.move)
node = node.parent

board = self.parent.board(_cache=False)
board.push(self.move)
while stack:
board.push(stack.pop())

if _cache:
if self.parent is not None:
self.board_cached = weakref.ref(board)
return board.copy()
else:
return board

return board

def _move(self) -> chess.Move:
assert self.move is not None, "cannot get move of dangling GameNode"
Expand Down Expand Up @@ -476,13 +492,7 @@ def __init__(self, headers: Optional[Union[Mapping[str, str], Iterable[Tuple[str
self.headers = Headers(headers)
self.errors: List[Exception] = []

def board(self, *, _cache: bool = False) -> chess.Board:
"""
Gets the starting position of the game.
Unless the ``FEN`` header tag is set, this is the default starting
position (for the ``Variant``).
"""
def _board(self) -> chess.Board:
return self.headers.board()

def setup(self, board: Union[chess.Board, str]) -> None:
Expand Down

0 comments on commit 324561c

Please sign in to comment.