Answers.Body: <p>If you want to access every node, then you're basically iterating over the game tree. A straightforward recursion will get you there, something like:</p>
  
  <pre><code>def traverse(node):
      # do whatever stuff you want for the node here
      print(node.move)
      # terminating condition
      if (node.is_end()):
          return
      # recursion
      for child_node in node.variations:
          traverse(child_node)
  </code></pre>
  
  <p>And to use it, call <code>traverse(game)</code> where <code>game</code> is the root node of your game.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-05T04:06:38.880Z
Answers.LastActivityDate: 2020-03-05T04:06:38.880Z
Answers.UserDisplayName: Remellion
Body: <p>I'm trying to write a simple code that is capable of reading a PGN file and searching for moves and comments throughout the variant tree. However, I have not been successful, I have not yet been able to understand how I access the moves for a particular variant outside the main line.</p>
  
  <p>Here is an example.</p>
  
  <p>Reading the game from the pgn file below:</p>
  
  <hr>
  
  <p>[Event "?"]<br>
  [Site "?"]<br>
  [Date "????.??.??"]<br>
  [Round "?"]<br>
  [White "Study"]<br>
  [Black "Variant Caro-Kann"]<br>
  [Result "1/2-1/2"]<br>
  [ECO "B12"]<br>
  [Annotator "Joao Paulo"]<br>
  [PlyCount "10"]<br>
  [EventDate "2020.03.04"]<br>
  [SourceDate "2020.03.04"]</p>
  
  <ol>
  <li>e4 c6 2. d4 d5 3. e5 {Variant 1} (3. exd5 {Variant 2} cxd5 4. Nf3 Nd7 (4... Nf6 {Variant 2.2} 5. c4 dxc4 6. Bxc4 e6) 5. Nc3 Ngf6) (3. Nc3 {Variant 3} dxe4 4. Nxe4 Nd7 5. Nf3 Ngf6) (3. Nd2 {Variant 4} dxe4 4. Nxe4 Nf6 5. Nxf6+ gxf6) 3... Bf5 4. Bd3 Bxd3 5. Qxd3 e6 1/2-1/2   </li>
  </ol>
  
  <hr>
  
  <p>Using the following code:</p>
  
  <pre>
  -------------------------------------------------------------------------- 
  import chess
  import chess.pgn
  pgn = open("pgns/Variants Caro-Kann.pgn")
  game = chess.pgn.read_game(pgn)
  bd = game.board()
  node = game
  ct = 0;
  while not node.is_end():
      print('Current Move')
      print(node.variations[0].move)
      print(node.starts_variation())
      bd.push(node.variations[0].move)
      node = node.variations[0]
      print(bd)
      print(node.starts_variation())
      print(node.comment)
      if node.is_end() == 0:
          print('Next Move')
          print(node.variations[0].move)
  --------------------------------------------------------------------------
  How could I access the bids for the entire variant tree (variants: 2, 2.2, 3 and 4)?
  I would appreciate your help.
  </pre>
  
Tags: <programming><pgn><python>
Title: Question on how to access a move on a line outside the main line using python-chess
ThreadText: # Question on how to access a move on a line outside the main line using python-chess
  
  (Asked by: username_0 on 2020-03-04)
  
  I'm trying to write a simple code that is capable of reading a PGN file and searching for moves and comments throughout the variant tree. However, I have not been successful, I have not yet been able to understand how I access the moves for a particular variant outside the main line.
  
  Here is an example.
  
  Reading the game from the pgn file below:
  
  ---
  
  [Event "?"]  
  [Site "?"]  
  [Date "????.??.??"]  
  [Round "?"]  
  [White "Study"]  
  [Black "Variant Caro-Kann"]  
  [Result "1/2-1/2"]  
  [ECO "B12"]  
  [Annotator "Joao Paulo"]  
  [PlyCount "10"]  
  [EventDate "2020.03.04"]  
  [SourceDate "2020.03.04"]
  
  1. e4 c6 2. d4 d5 3. e5 {Variant 1} (3. exd5 {Variant 2} cxd5 4. Nf3 Nd7 (4... Nf6 {Variant 2.2} 5. c4 dxc4 6. Bxc4 e6) 5. Nc3 Ngf6) (3. Nc3 {Variant 3} dxe4 4. Nxe4 Nd7 5. Nf3 Ngf6) (3. Nd2 {Variant 4} dxe4 4. Nxe4 Nf6 5. Nxf6+ gxf6) 3... Bf5 4. Bd3 Bxd3 5. Qxd3 e6 1/2-1/2
  
  ---
  
  Using the following code:
  
  ```
  
  -------------------------------------------------------------------------- 
  import chess
  import chess.pgn
  pgn = open("pgns/Variants Caro-Kann.pgn")
  game = chess.pgn.read_game(pgn)
  bd = game.board()
  node = game
  ct = 0;
  while not node.is_end():
      print('Current Move')
      print(node.variations[0].move)
      print(node.starts_variation())
      bd.push(node.variations[0].move)
      node = node.variations[0]
      print(bd)
      print(node.starts_variation())
      print(node.comment)
      if node.is_end() == 0:
          print('Next Move')
          print(node.variations[0].move)
  --------------------------------------------------------------------------
  How could I access the bids for the entire variant tree (variants: 2, 2.2, 3 and 4)?
  I would appreciate your help.
  
  ```
  
  **Tags:** programming, pgn, python
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-03-05. Score: 2
  
  If you want to access every node, then you're basically iterating over the game tree. A straightforward recursion will get you there, something like:
  
  ```
  def traverse(node):
      # do whatever stuff you want for the node here
      print(node.move)
      # terminating condition
      if (node.is_end()):
          return
      # recursion
      for child_node in node.variations:
          traverse(child_node)
  
  ```
  
  And to use it, call `traverse(game)` where `game` is the root node of your game.
  
  
  ---
  
  