-1

I'm trying to make the input maze of Astar algorithm(alogorithm to find the shortest path between start and destination and there can be some blockages within the maze, which takes input a maze representing blockages only, as shown below).

From the GUI using the Click1 command in each button, I intend to get an output like this(where I inserted a blockage at [3][2]).
1 represents blockage which is to avoided to find the path from start to end.

[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

but I get a output as following,I can't understand why it's blocking the same column of each and every row

[[0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0], [0, 1, 0, 0, 0]]

I created the maze in the init(): of class App() using this:

def __init__(self, master,dimension,indexes):
   self.maze=[[0]*self.dimension]*self.dimension

this entire thing is within a class App():
for creating the grid of buttons, and storing their reference

        self.gid = []
        for i in range(self.dimension):
            row = []
            Grid.rowconfigure(self.frame1, i + 1, weight=3)
            for j in range(self.dimension):
                Grid.columnconfigure(self.frame1, j + 1, weight=3)
                btn=Button(self.frame1,command=lambda i=i, j=j: self.Click1(i, j))
                btn.grid(sticky=N+S+E+W,padx=2,pady=2,ipadx=1,ipady=1)
                row.append(btn)
                row[-1].grid(row=i + 1, column=j+1)
            self.gid.append(row)


the Click1 method/Command that also within this class:

    def Click1(self, i, j):
        self.indxes.append((i,j))
        if len(self.indxes)==1:
            self.gid[i][j]["bg"]="blue" #indicates start
        elif len(self.indxes)==2:
            self.gid[i][j]["bg"]="green" #indicates destinations
        else:
            self.gid[i][j]["bg"] = "black"
            self.maze[i][j] = 1  #how I insert blockage within the maze

1 Answers1

0

Try this in your init:

def __init__(self, master,dimension,indexes):
   self.maze = [[0] * self.dimension] for _ in range(self.dimension)]

The latter * self.dimension call was assigning the same reference to all your inner lists (dimension number of times) - meaning when one is changed all will change.

This creates a unique list for each sublist

Tresdon
  • 1,211
  • 8
  • 18