Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
Object referencing breaking iteration loops

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda