Beispiel #1
0
    def generate(self, width, height):

        graph = Graph(width, height)

        visited_nodes = set()

        nodes = list(flatten(graph.nodes))
        node = choice(nodes)
        nodes.remove(node)

        visited_nodes.add(node)

        while nodes:
            path = {}
            node = n = choice(nodes)
            while node not in visited_nodes:
                next = choice(list(graph.get_neighbours_for_node(node)))
                path[node] = next

                node = next

            node = n
            while node not in visited_nodes:
                next = path[node]
                node.connect(next)
                visited_nodes.add(node)
                nodes.remove(node)

                node = next

        return graph
Beispiel #2
0
 def hunt():
     for n in flatten(graph.nodes):
         if n in visited_nodes:
             unvisited = get_unvisited_neighbours(graph, visited_nodes, n)
             if unvisited:
                 next = choice(unvisited)
                 n.connect(next)
                 return next
Beispiel #3
0
    def draw(self, widget, ctx):

        if not self.maze:
            return

        ctx.set_line_width(LINE_WIDTH)
        ctx.set_operator(cairo.OPERATOR_OVER)
        ctx.set_font_size(10)

        width = self.get_allocation().width - 2*PADDING
        height = self.get_allocation().height - 2*PADDING

        node_width = width / float(len(self.maze.nodes[0]))
        node_height = height / float(len(self.maze.nodes))

        # paint outline
        ctx.set_source_rgb(0, 0, 0)
        ctx.rectangle(PADDING, PADDING, width, height)
        ctx.stroke()

        for node in flatten(self.maze.nodes):
            x = PADDING + node.x * node_width
            y = PADDING + node.y * node_height
            x1 = x + node_width
            y1 = y + node_height

            right = bottom = True
            for neighbour in node.connected_nodes:
                if node.x == neighbour.x and node.y < neighbour.y:
                    bottom = False
                if node.y == neighbour.y and node.x < neighbour.x:
                    right = False

            if bottom and node.y < len(self.maze.nodes) - 1:
                draw_line(ctx, x, y1, x1, y1)
            if right and node.x < len(self.maze.nodes[0]) - 1:
                draw_line(ctx, x1, y, x1, y1)

            if node.value is not None:
                value = str(node.value)
                ctx.move_to(x + node_width / 2, y + node_height / 2)
                ctx.show_text(value)

            if self.runner and node in self.runner.path:
                ctx.set_source_rgba(0.8, 0.0, 0.0, 0.2)
                ctx.rectangle(x, y, node_width, node_height)
                ctx.fill()


        # Draw runner
        if not self.runner: return

        x = PADDING + self.runner.x * node_width
        y = PADDING + self.runner.y * node_height

        ctx.set_source_rgba(0.9, 0.0, 0.0, 0.6)
        ctx.rectangle(x, y, node_width, node_height)
        ctx.fill()