Esempio n. 1
0
def find_route(name, maze, crumbs):
    prepare_maze(maze)
    
    p = mazes.get_start(maze)
    d = choose_direction(p, maze)
    
    result = ""

    while not mazes.is_finish(p[0], p[1], maze):
        if check_right(p, d, maze):
            # Move right
            d = right[d]
            p = move(p, d, maze, crumbs)
            result += d
        elif check_forward(p, d, maze):
            # Move forward
            p = move(p, d, maze, crumbs)
            result += d
        else:
            # Turn left
            d = left[d]

    if crumbs:
        mazes.print_maze((name, maze))
            
    return result
Esempio n. 2
0
 def __init__(self, maze, debug):
     self.domain = []
     self.finish = {}
     self.start = {}
     self.visited = []
     self.complete_colors = []
     self.debug = ''
     if debug is 'True':
         self.debug = True
     elif debug is 'False':
         self.debug = False
     self.moves = 0
     self.find_s_and_f(maze)
     print('Solving:')
     mazes.print_maze(maze)
     self.c = constraints.Constraints(self.start, self.finish, self.debug)
Esempio n. 3
0
    def backtracking(self, assignment):  #backtracking with heuristics
        if self.complete(
                assignment
        ):  #if the assignment is complete, return and print maze
            mazes.print_maze(assignment)
            return assignment

        colors, node = self.get_min_rem_val(
            assignment
        )  #get a node that has the least amount of available legal moves

        if node is None:  #when all nodes have been visited but the assignment is not complete, instant fail
            return False

        for color in colors:
            self.moves += 1
            if self.debug:
                print("Evaluating: ")
                print("Domain is :{}".format(self.get_colors(node)))
                print("Color: {}".format(color))
                print("node is at x: {} y:{}".format(node.x, node.y))
                node.value = 'X'
                mazes.print_maze(assignment)
                node.value = '_'

            node.value = color
            self.visited.append(node)

            if self.color_complete(color):
                self.complete_colors.append(color)

            result = self.backtracking(assignment)  #move on to next node
            if result:
                return result

            self.visited.remove(node)  #that branch failed, backtrack
            if color in self.complete_colors:
                self.complete_colors.remove(color)

            node.value = '_'
        return False
Esempio n. 4
0
    def dumb_backtracking(self, assignment):  #backtracking with no heuristics
        if self.complete(
                assignment
        ):  #if the assignment is complete, return and print maze
            mazes.print_maze(assignment)
            return assignment

        node = self.get_node(assignment)  #get a node that has not been visited

        if node is None:  #when all nodes have been visited but the assignment is not complete, instant fail
            return False
        for color in self.get_colors(
                node):  #get all colors that are not complete
            self.moves += 1
            if self.debug:
                print("Evaluating: ")
                print("Domain is :{}".format(self.get_colors(node)))
                print("Color: {}".format(color))
                print("node is at x: {} y:{}".format(node.x, node.y))
                node.value = 'X'
                mazes.print_maze(assignment)
                node.value = '_'

            if self.consistant(
                    color, node,
                    assignment):  #if the color we have chosen is legal, use it
                if self.color_complete(color):
                    self.complete_colors.append(color)
                self.visited.append(node)

                result = self.dumb_backtracking(
                    assignment)  #move on to next node
                if result:
                    return result

                self.visited.remove(node)  #that branch failed, backtrack
                if color in self.complete_colors:
                    self.complete_colors.remove(color)

                node.value = '_'
        return False
    def print_results(self, search, curr_node, cost):
        print("{} \nCoord: {},{} \nCost: {}".format(search, curr_node.x,
                                                    curr_node.y, cost))
        print("Solution:")

        #print path
        node = curr_node
        while node.previous is not None:
            if node.value is not 'P' and node.value is not '*':
                node.value = '.'

            node = node.previous
        mazes.print_maze(self.maze)

        #clean maze
        node = curr_node
        while node.previous is not None:
            if node.value is not 'P' and node.value is not '*':
                node.value = ' '

            node = node.previous