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:
- 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?