Home > Software design >  Why are my dots going into the wall even though I have a condition?
Why are my dots going into the wall even though I have a condition?

Time:05-17

I have the following grid

grid = np.array([
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
    [1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
    [1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
    [1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
    [1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
    [1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
    [1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
    [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
    [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
    [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
    [1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
    [1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
    [1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
    [1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
    [1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
    [1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
    [1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])

And my code:

class Worker():
    def __init__(self, name):
        while True:
            x = random.randint(0,23)
            y = random.randint(0,22)
            if grid[x,y] != 1:
                self.row = x
                self.col = y
                break

        self.name = name


    def move(self, workers):
        while True:
            self.row  = random.randint(-1,1)
            self.col  = random.randint(-1,1)
            if self.row >= 26:
                self.row = 25
            elif self.row<0:
                self.row=0
            if self.col >= 24:
                self.col = 23
            elif self.col<0:
                self.col=0
            if grid[self.col,self.row] != 1:
                self.row = self.row
                self.col = self.col
                break 


def plot_worker_scatter(workers):
    xlist = []
    ylist = []
    for k in range(len(workers)):
        xlist.append(workers[k].col)
        ylist.append(workers[k].row)
        plt.scatter(xlist,ylist,c="red")

def main():
    workerNames = ["1", "2","3","4","5","6","7","8","9","10"]
    numWorkers = 10
    workerList = []
    
    for i in range(numWorkers):
        workerList.append(Worker(workerNames[i]))
    
    for t in range(4):
        for i in range(numWorkers):
            workerList[i].move(workerList)
        print("Just a normal day")
        plt.pcolormesh(grid)
        plot_worker_scatter(workerList)
        plt.scatter(0,0,c="blue")
        plt.title("Normal Activity")
        plt.xlabel("Columns")
        plt.ylabel("Rows")
        plt.xlim(0,MAXCOLS)
        plt.ylim(0,MAXROWS)
        plt.show()
    print(grid[0,10])

if __name__ == "__main__":
    main()

enter image description here

Why are my dots going into the walls even though I have the condition where [self.row, self.col] != 1, Because in the grid the ones are the walls, so how could the dots still go into the grid spaces that are one. Please help. I don't know what to do. This is a simulation where the beings move around their environment randomly. How are the beings moving inside walls even though I have a condition so they don't do that. Please recommend a better code for it if mine isn't right Thank you

CodePudding user response:

I tried your code and I think you are switching rows and cols. When you create your scatter list you do:

        xlist.append(workers[k].col)
        ylist.append(workers[k].row)

meaning that col is the x coordinate, but when you check it you do:

        if grid[self.col,self.row] != 1:

You should do instead:

        if grid[self.row,self.col] != 1:

I tested it with this change and I don't see dots in the wrong area any more after 20 refreshes.

enter image description here

CodePudding user response:

The other solution is correct. In case you also want to avoid the workers overlapping the walls you need to do one more thing:

The workers are currently drawn in the corner of their grid cells. Since every cell is 1 wide and high you can simply shift the workers by 0.5 when drawing them to make sure they are in the middle of the cell:

def plot_worker_scatter(workers):
    xlist = []
    ylist = []
    for k in range(len(workers)):
        xlist.append(workers[k].col   0.5)
        ylist.append(workers[k].row   0.5)
        plt.scatter(xlist,ylist,c="red")

result

  • Related