1

I want to make a Tree structure that shows my data from a number of chessGames. I dont want to use any libraries

The Node class is who has the turn and which move number it is. So there will be one node that is 1 White, and one that is 1 Black.

The Branch class is the children of the nodeclass that contains the exact move that was playes and then whos turn it is.

The moves from each game is stored in a string like this:

  1. f4 d5 2. b3 Bg4 3. Bb2 Nc6 4. h3 Bh5 5. g4 Bg6 6. Nf3 h5 7. g5 e6 8. e3 Nge7 9. Bb5 a6 10. Bxc6+ Nxc6 11. d3 Qe7 12.Qe2 O-O-O 13. Qf2 f6 14. Nh4 Bf7 15. gxf6 gxf6 16. Nc3 Qe8 17. O-O-O e5 18. Rdf1 Be6 19. f5 Bf7 20. Kb1 Rg8 21. Rhg1 Rxg1 22. Rxg1 Bc5 23. Ng6 b6 24. Na4 Bd6 25. Nc3 Bc5 26.Bc1 Qd7 27. Qf3 Kb8 28. Qxh5 Nb4 29. Na4 Bd6 30. Qg4 Qc6 31. Qg2 b5 32. Nb2 Qb6 33. Bd2 Bc5 34. c3 Nc6 35. d4 Bd6 36. h4 b4 37. dxe5 Nxe5 38. cxb4 d4 39. e4 d3 40. Rd1 Nc6 41. h5 Bxb4 42. Bc1 d2 43. Bxd2 Ba3 44. Qe2 Kb7 45. Bc1 Bxb2 46. Kxb2 Rxd1 47. Qxd1 Qf2+ 48. Qd2 Qg1 49. h6 Bxg6 50. fxg6 1-0

This is my code so far:

class Node: 
def __init__(self, moveNr, color):
    self.moveNr = moveNr
    self.color = color
    self.branches = []

def addNewBranch(self,branch):
    for b in self.branches:
        if b.move == branch.move: 
            return
    self.branches.append(branch)
    return 
        


class Branch:
   def __init__(self, move):
       self.move = move 
       self.nextMove = None

   def setNextMove(self, nextMove):
       self.nextMove = nextMove

   def getNextMove(self):
       return self.nextMove

def getMoveDict(string1):
       moves_dict = {}
       moves = string1.split()
       for i in range(0, len(moves), 3):
          if i+2 < len(moves):
             key = moves[i].split('.')[0]
             value = (moves[i+1], moves[i+2])
             moves_dict[key] = value
       return moves_dict

 def makeTreeMoves(dict, tree):
   nodeList = []
   branchList = []
   for key in dict:
       node1 = Node(key, "white")
       node2 = Node(key, "black")
       nodeList.append(node1)
       nodeList.append(node2)
       tree.addTreeNode(node1)
       tree.addTreeNode(node2)

       branch1 = Branch(dict[key][0]) 
       branch2 = Branch(dict[key][1])
       branchList.append(branch1)
       branchList.append(branch2)
       tree.addTreeBranch(branch1)
       tree.addTreeBranch(branch2)

       node1.addNewBranch(branch1) 
       node2.addNewBranch(branch2)

   for x in range(len(nodeList)):
       if x == len(nodeList)-1:
          return
       branchList[x].setNextMove(nodeList[x+1])

class Tree: 
   def __init__(self):
       self.nodes = []
       self.branches = []

   def addTreeNode(self, node):
       self.nodes.append(node)

   def addTreeBranch(self, branch): 
       self.branches.append(branch)
        

But i think i may be on the wrong path? How should i make my tree structure and how do i add the nodes and branches(also nodes in tree) into the tree and making sure that i can follow the tree and see where a branch splits(where to similar games becomes different?

marfin
  • 21
  • 1
  • What do you expect from the tree? What's the purpose of storing all nodes and branches in the Tree instance. Do you have use cases where you will access the `branches` list and search for something? Can you provide examples of input and output scenarios? – trincot Mar 02 '23 at 13:00
  • @trincot: I want to somehow store the nodes and branches so that i can 1. vizualize the tree using graphviz. I also want to be able to Design functions to print an opening tree at a given depth, i.e. number of 1/2 moves. The idea here is to get the number of games won by white pieces, by black pieces and the number of games that ended up in a draw in each opening. Design functions to insert your drawing in the report. And design functions to parse a data base file such as "Stockfish 15 64-bit.commented.[2600].pgn.gz " and load it into an opening tree – marfin Mar 02 '23 at 13:07
  • *"Design functions to insert your drawing in the report."*: what does that mean? – trincot Mar 02 '23 at 13:09
  • *"file such as "Stockfish 15 64-bit.commented.[2600].pgn.gz"*: I have no idea what that file is. – trincot Mar 02 '23 at 13:09
  • @trincot the drawing is the pdf that graphviz produces when you add nodes and edges. The Stockfish" file is a pgn file that cpontains 2600 games played by the chessAI StockFish. This is where we get our moves-strings from. I know how to do this, but i am unsure of how to store these moves from all the games in a tree -structure using nodes and branches – marfin Mar 02 '23 at 13:20
  • *"I know how to do this"*: did you notice how your example string is not correctly converted to a dict? The move 12.Qe2 is lost. – trincot Mar 02 '23 at 13:24
  • Anyway, this question is too open-ended. Try to focus on a *specific* problem you have. – trincot Mar 02 '23 at 13:38

0 Answers0