def make_tree(previous): """Сonstructing a game tree, by extending (by adding two child vertices) from the root of a tree """ # previous it is Board() if previous.if_won() == -1 or previous.if_won() == 1: return None new_move_left = random.choice(previous.empty_list()) tree = LinkedBinaryTree(previous) if len(previous.empty_list()) == 0: return None new_previous = copy.deepcopy(previous) new_previous.set_null(new_move_left) tree.left_child = LinkedBinaryTree(new_previous) make_tree(new_previous) if len(new_previous.empty_list()) == 0: return None new_move_right = random.choice(new_previous.empty_list()) n_new_previous = copy.deepcopy(new_previous) n_new_previous.set_null(new_move_right) tree.right_child = LinkedBinaryTree(n_new_previous) make_tree(n_new_previous)
def build_tree(b): if b.someone_wins == "TIE" or b.someone_wins == "O" or b.someone_wins == "X": return tree = LinkedBinaryTree(b) free_cells = b.empty_coords random_cell1 = choice(list(free_cells)) choice1 = deepcopy(b) choice1.turn("O", random_cell1) tree.left_child = LinkedBinaryTree(choice1) build_tree(choice1) free_cells.discard(random_cell1) if free_cells != set(): # we have to check if set isn't empty random_cell2 = choice(list(free_cells)) choice2 = deepcopy(b) choice2.turn("O", random_cell2) tree.right_child = LinkedBinaryTree(choice2) build_tree(choice2)