Exemplo n.º 1
0
def move_to_top(current_node, visited_map, queue):
    """function moves the empty state up one position"""
    #The function checks to see if the move is valid (if it is on the board),
    #if the move left is valide, it will create a temporary new_state and it will
    #call the function to swap the empty position with the left position. The new_state is
    #returned and if that state has not been previously visited, the counter is incremented
    #for nodes created and the node is put into the queue. This process is the same for each
    #possible position: right, down, and left. The only change is math that is done to look
    #at each position.
    if (current_node.empty_spot - 3) < 0:
        return

    new_empty_spot = current_node.empty_spot - 3
    new_state = current_node.start_state[:]
    new_state = swap_empty_position(new_state, current_node.empty_spot,
                                    new_empty_spot)

    if not check_visited(visited_map, new_state):
        set_glob_counter()
        add_to_visited(visited_map, new_state, COUNTER)
        new_node = Node(new_state)
        new_node.empty_spot = new_node.find_empty_position()
        new_node.depth = new_node.set_depth(current_node)
        new_node.set_path = new_node.set_path(current_node, new_empty_spot)
        queue.append(new_node)
Exemplo n.º 2
0
def move_to_left(current_node, visited_map, queue):
    """function moves the empty state left one position if possible."""
    if (current_node.empty_spot % 3) == 0:
        return

    new_empty_spot = current_node.empty_spot - 1
    new_state = current_node.start_state[:]
    new_state = swap_empty_position(new_state, current_node.empty_spot,
                                    new_empty_spot)

    if not check_visited(visited_map, new_state):
        set_glob_counter()
        add_to_visited(visited_map, new_state, COUNTER)
        new_node = Node(new_state)
        new_node.empty_spot = new_node.find_empty_position()
        new_node.depth = new_node.set_depth(current_node)
        new_node.set_path = new_node.set_path(current_node, new_empty_spot)
        queue.append(new_node)
Exemplo n.º 3
0
def move_to_bottom(current_node, visited_map, queue):
    """function moves the empty state down one position"""
    if (current_node.empty_spot + 3) > 8:
        return

    new_empty_spot = current_node.empty_spot + 3
    new_state = current_node.start_state[:]
    new_state = swap_empty_position(new_state, current_node.empty_spot,
                                    new_empty_spot)

    if not check_visited(visited_map, new_state):
        set_glob_counter()
        add_to_visited(visited_map, new_state, COUNTER)
        new_node = Node(new_state)
        new_node.empty_spot = new_node.find_empty_position()
        new_node.depth = new_node.set_depth(current_node)
        new_node.set_path = new_node.set_path(current_node, new_empty_spot)
        queue.append(new_node)
Exemplo n.º 4
0
def move_to_left(current_node, visited_map, queue):
    """function moves the empty state left one position if possible."""
    if (current_node.empty_spot % 3) == 0:
        return
    # from this point, the same steps are repeated as the move_to_up function, but now swapping left
    new_empty_spot = current_node.empty_spot - 1
    new_state = current_node.start_state[:]
    new_state = swap_empty_position(new_state, current_node.empty_spot, new_empty_spot)

    if not check_visited(visited_map, new_state):
        set_glob_counter()
        add_to_visited(visited_map, new_state, COUNTER)
        new_node = Node(new_state)
        new_node.empty_spot = new_node.find_empty_position()
        new_node.set_path = new_node.set_path(current_node, new_empty_spot)
        new_node.depth = new_node.set_depth(current_node)
        new_node.heuristic = new_node.calculate_misplaced_tiles()
        Q.heappush(queue, (new_node.heuristic, COUNTER, new_node))
Exemplo n.º 5
0
def move_to_bottom(current_node, visited_map, queue):
    """function moves the empty state down one position"""
    if (current_node.empty_spot + 3) > 8:
        return

    new_empty_spot = current_node.empty_spot + 3
    new_state = current_node.start_state[:]
    new_state = swap_empty_position(new_state, current_node.empty_spot,
                                    new_empty_spot)
    # from this point, the same steps are repeated as the move_to_up function, but now swapping down
    if not check_visited(visited_map, new_state):
        set_glob_counter()
        add_to_visited(visited_map, new_state, COUNTER)
        new_node = Node(new_state)
        new_node.empty_spot = new_node.find_empty_position()
        new_node.set_path = new_node.set_path(current_node, new_empty_spot)
        new_node.depth = new_node.set_depth(current_node)
        new_node.heuristic = new_node.calculate_manhattan_distance()
        Q.heappush(queue, (new_node.heuristic, COUNTER, new_node))
Exemplo n.º 6
0
def move_to_right(current_node, visited_map, stack):
    """function moves the empty state right one position"""
    if ((current_node.empty_spot + 1) % 3) == 0:
        return

    new_empty_spot = current_node.empty_spot + 1
    new_state = current_node.start_state[:]
    new_state = swap_empty_position(new_state, current_node.empty_spot,
                                    new_empty_spot)

    if not check_visited(visited_map, new_state):
        set_glob_counter()
        add_to_visited(visited_map, new_state, COUNTER)
        new_node = Node(new_state)
        new_node.empty_spot = new_node.find_empty_position()
        new_node.depth = new_node.set_depth(current_node)
        if new_node.depth > MAX_DEPTH:
            increment_max_depth(new_node.depth)
        stack.append(new_node)
Exemplo n.º 7
0
def move_to_top(current_node, visited_map, queue):
    """function moves the empty state up one position"""
    if (current_node.empty_spot - 3) < 0:
        return

    new_empty_spot = current_node.empty_spot - 3
    #copies the current state to a temporary variable called new_state
    new_state = current_node.start_state[:]
    #makes the swap for the empty position and tile above the empty spot
    new_state = swap_empty_position(new_state, current_node.empty_spot, new_empty_spot)

    #if the new_state is not in the visited map then proceed with making a new node and adding it to the queue
    if not check_visited(visited_map, new_state):
        set_glob_counter()
        add_to_visited(visited_map, new_state, COUNTER)
        new_node = Node(new_state)
        new_node.empty_spot = new_node.find_empty_position()
        new_node.set_path = new_node.set_path(current_node, new_empty_spot)
        new_node.depth = new_node.set_depth(current_node)
        new_node.heuristic = new_node.calculate_misplaced_tiles()
        Q.heappush(queue, (new_node.heuristic, COUNTER, new_node))