Exemplo n.º 1
0
        def process(node, C_Turn):
            '''
            Help func for building tree
            '''
            is_free = bool(max([cell is None for line in node.data for cell in line]))
            if not is_free:
                return
            # Adding left tree root as board
            while True:
                position = [random.choice([0, 1, 2]), random.choice([0, 1, 2])]
                if 0 > position[0] or position[0] > 2 or 0 > position[1] or position[0] > 2 or node.data[position[0]][position[1]] is not None:
                    continue
                else:
                    break

            if Board.check_status(node.data) is not None:
                return

            new_board = copy.deepcopy(node.data)
            if C_Turn:
                new_board[position[0]][position[1]] = Board.COMPUTER_MARK
                self.lastMark = Board.COMPUTER_MARK
                self.lastTurn = position
            else:
                Board.USER_MARK
                # self.lastMark = Board.USER_MARK
                # self.lastTurn = position

            node.left = BSTNode(new_board)

            # Adding right tree root as board
            while True:
                position = [random.choice([0, 1, 2]), random.choice([0, 1, 2])]
                if 0 > position[0] or position[0] > 2 or 0 > position[1] or position[0] > 2 or node.data[position[0]][position[1]] is not None:
                    continue
                else:
                    break

            if Board.check_status(node.data) is not None:
                return

            new_board = copy.deepcopy(node.data)
            if C_Turn:
                new_board[position[0]][position[1]] = Board.COMPUTER_MARK
                self.lastMark = Board.COMPUTER_MARK
                self.lastTurn = position
            else:
                Board.USER_MARK
                # self.lastMark = Board.USER_MARK
                # self.lastTurn = position

            node.right = BSTNode(new_board)

            C_Turn = not C_Turn

            process(node.left, C_Turn)
            process(node.right, C_Turn)
Exemplo n.º 2
0
        def recurse(node, computer_turn):
            try:
                free = bool(
                    max([
                        position is None for line in node.data
                        for position in line
                    ]))
                if not free:
                    return None
            except:
                pass

            while True:
                position = [random.choice([0, 1, 2]), random.choice([0, 1, 2])]
                if node.data[position[0]][position[1]] is None:
                    break
                else:
                    continue

            if Board.check_the_status(node.data) is not None:
                return None

            our_board = copy.deepcopy(node.data)
            if computer_turn:
                our_board[position[0]][position[1]] = Board.computer_symbol
            else:
                our_board[position[0]][position[1]] = Board.user_symbol

            node.left = BSTNode(our_board)

            while True:
                position = [random.choice([0, 1, 2]), random.choice([0, 1, 2])]
                if node.data[position[0]][position[1]] is None:
                    break
                else:
                    continue

            if Board.check_the_status(node.data) is not None:
                return None

            our_board = copy.deepcopy(node.data)
            if computer_turn:
                our_board[position[0]][position[1]] = Board.computer_symbol
            else:
                our_board[position[0]][position[1]] = Board.user_symbol

            node.right = BSTNode(our_board)

            if computer_turn is True:
                computer_turn = False
            elif computer_turn is False:
                computer_turn = True

            recurse(node.left, computer_turn)
            recurse(node.right, computer_turn)
Exemplo n.º 3
0
 def recurse(node, node_id):
     if node.left is None:
         node.left = BSTNode(state, node_id)
     else:
         node_id += 1
         recurse(node.left, node_id)
     # New item is greater or equal,
     # go right until spot is found
     if node.right is None:
         node.right = BSTNode(state, node_id)
     else:
         node_id += 1
         recurse(node.right, node_id)
Exemplo n.º 4
0
 def recurse(node):
     # New item is less, go left until spot is found
     if item < node.data:
         if node.left == None:
             node.left = BSTNode(item)
         else:
             recurse(node.left)
     # New item is greater or equal,
     # go right until spot is found
     elif node.right == None:
         node.right = BSTNode(item)
     else:
         recurse(node.right)
Exemplo n.º 5
0
    def make_a_tree(self):
        current_tree = LinkedBST()
        current_tree._root = BSTNode(self)

        def make(tr):
            has_winner = tr.data.has_winner()
            if has_winner:
                return False
            left_board = copy.deepcopy(tr)
            right_board = copy.deepcopy(tr)
            left_board.data.make_random_move()
            right_board.data.make_random_move()
            tr.left = left_board
            tr.right = right_board
            make(tr.left)
            make(tr.right)

        make(current_tree._root)

        self.tree = current_tree
        left_score = current_tree._root.left.data.compute_score()
        right_score = current_tree._root.right.data.compute_score()
        if left_score > right_score:
            position = current_tree._root.left.data.last_position
        if left_score < right_score:
            position = current_tree._root.right.data.last_position
        else:
            position = random.choice([current_tree._root.right.data.last_position,\
                current_tree._root.left.data.last_position])
        return position
Exemplo n.º 6
0
    def add(self, coords, game_step):
        """Adds item to the tree."""

        # Helper function to search for item's position
        def recurse(node, node_id):
            if node.left is None:
                node.left = BSTNode(state, node_id)
            else:
                node_id += 1
                recurse(node.left, node_id)
            # New item is greater or equal,
            # go right until spot is found
            if node.right is None:
                node.right = BSTNode(state, node_id)
            else:
                node_id += 1
                recurse(node.right, node_id)

        state = self.board
        state[coords[0]][coords[1]] = COMPUTER

        # Tree is empty, so new item goes at the root
        if self.isEmpty():
            self._root = BSTNode(state, game_step)
        # Otherwise, search for the item's spot
        else:
            recurse(self._root, game_step)
Exemplo n.º 7
0
    def add(self, item):
        """Adds item to the tree."""

        # Helper function to search for item's position
        def recurse(node):
            # New item is less, go left until spot is found
            if item < node.data:
                if node.left == None:
                    node.left = BSTNode(item)
                else:
                    recurse(node.left)
            # New item is greater or equal,
            # go right until spot is found
            elif node.right == None:
                node.right = BSTNode(item)
            else:
                recurse(node.right)
                # End of recurse

        # Tree is empty, so new item goes at the root
        if self.isEmpty():
            self._root = BSTNode(item)
        # Otherwise, search for the item's spot
        else:
            recurse(self._root)
        self._size += 1
Exemplo n.º 8
0
    def remove(self, item):
        """Precondition: item is in self.
        Raises: KeyError if item is not in self.
        postcondition: item is removed from self."""
        if not item in self:
            raise KeyError("Item not in tree." "")

        # Helper function to adjust placement of an item
        def liftMaxInLeftSubtreeToTop(top):
            # Replace top's datum with the maximum datum in the left subtree
            # Pre:  top has a left child
            # Post: the maximum node in top's left subtree
            #       has been removed
            # Post: top.data = maximum value in top's left subtree
            parent = top
            currentNode = top.left
            while not currentNode.right == None:
                parent = currentNode
                currentNode = currentNode.right
            top.data = currentNode.data
            if parent == top:
                top.left = currentNode.left
            else:
                parent.right = currentNode.left

        # Begin main part of the method
        if self.isEmpty(): return None

        # Attempt to locate the node containing the item
        itemRemoved = None
        preRoot = BSTNode(None)
        preRoot.left = self._root
        parent = preRoot
        direction = 'L'
        currentNode = self._root
        while not currentNode == None:
            if currentNode.data == item:
                itemRemoved = currentNode.data
                break
            parent = currentNode
            if currentNode.data > item:
                direction = 'L'
                currentNode = currentNode.left
            else:
                direction = 'R'
                currentNode = currentNode.right

        # Return None if the item is absent
        if itemRemoved == None: return None

        # The item is present, so remove its node

        # Case 1: The node has a left and a right child
        #         Replace the node's value with the maximum value in the
        #         left subtree
        #         Delete the maximium node in the left subtree
        if not currentNode.left == None \
                and not currentNode.right == None:
            liftMaxInLeftSubtreeToTop(currentNode)
        else:

            # Case 2: The node has no left child
            if currentNode.left == None:
                newChild = currentNode.right

                # Case 3: The node has no right child
            else:
                newChild = currentNode.left

                # Case 2 & 3: Tie the parent to the new child
            if direction == 'L':
                parent.left = newChild
            else:
                parent.right = newChild

        # All cases: Reset the root (if it hasn't changed no harm done)
        #            Decrement the collection's size counter
        #            Return the item
        self._size -= 1
        if self.isEmpty():
            self._root = None
        else:
            self._root = preRoot.left
        return itemRemoved