Home > Software engineering >  Object referencing breaking iteration loops
Object referencing breaking iteration loops

Time:04-07

So I have an app, which I have made a stackblitz for. It is basically going to be Pacman with a random maze being generated for the 'maze'.

The maze algorithm is designed to work in one of two settings

  • As a generation which happens and simply returns a maze object or
  • As an iterative generation which updates the main thread via a Subject after each iteration.

This means you can generate it fast, or generate it over time and watch the maze grow. The immediate solution works fine, the 'over time' solution does too, except in one circumstance.

I have three additional functions in the maze-algorithms.model.ts file:

  • mirrorMazeYDirection - make a mirror of the maze in the y direction
  • mirrorMazeXDirection - make a mirror of the maze in the x direction
  • makeWallsThick - makes the walls of the maze 1 thick, meaning no sharp turns or anything

If I call them like this it all works just fine:

    let newMaze: Maze2D = { ...iterationUpdate.maze };

    newMaze = mazeObject.makeWallsThick({ ...newMaze });
    newMaze = mazeObject.mirrorMazeXDirection({ ...newMaze }, 2);
    newMaze = mazeObject.mirrorMazeYDirection({ ...newMaze }, 2);

If however I mirror first and then make the walls thick it doesnt...

    newMaze = mazeObject.mirrorMazeXDirection({ ...newMaze }, 2);
    newMaze = mazeObject.mirrorMazeYDirection({ ...newMaze }, 2);
    newMaze = mazeObject.makeWallsThick({ ...newMaze });

You can test this in the stackblitz in the generateMaze() function in the app.component.ts.

What happens in this case is the maze doubles in size every time - the subject when it gets new data seems to be working on the prcoessed data from the mirrorx and y functions, not the new data. The array very quickly gets out of hand. It also doesn't appear to be correctly mirroring, so it feels like the mirrorX and Y functions are feeding back somehow into the generation algorithm.

Im stumped as to why this would be the case, have tried copying objects and tried to be very careful im not feeding anything back into the iteration routing but it just seems to keep not working unless I thicken the walls first.

Edit: Just to reiterate something, this is only a problem if the program iterates over time, if it just makes the maze, returns that via the subject, and then I mirror and thicken, it all works fine. Its only if I iterate over time, and if I mirror before I thicken the walls.

CodePudding user response:

The issue is that when you call your functions with { ...newMaze } you are only passing in a shallow copy. The properties of the objects still share the same references (they point to the same things). Looking at your code I see that makeWallsThick creates a new maze object but the mirror.. functions do another shallow copy of the input and then update the tiles in the shallow copy - which is also updating the tiles in the input.

  • Related