Home > Software engineering >  why items are being pushed into array, despite failing conditional?
why items are being pushed into array, despite failing conditional?

Time:06-26

i am writing a terminal game in node.js, it generates a random field composed of

const hat = '^';
const hole = 'O';
const fieldCharacter = '░';
const pathCharacter = '*';

player needs to go through the maze and find their hat. Field is represented by

randomArr

Because array is random, it can happen that generated field is impossible to pass, because hat is surrounded by holes in a way that player simply just can't get there, so I'm making a feature where algorithm first checks if it is possible to win (basically maze generator). I have a code that should work, but it won't execute properly, because it fells into infinite loop. it comes across conditional , where conditions need to be met in order to add a bi-element array into a nested array (conditions are: neighbouring square must be a field element and the square hasn't been visited before). despite not meeting the second condition, algorithm keeps adding the same element to array, over and over again.

This is the beginning of the loop:

while (true) {           
    //checking if next move is a winning move
    if (randomArr?.[p[0]]?.[p[1]   1] === hat || randomArr?.[p[0]]?.[p[1] - 1] === hat || randomArr?.[p[0]   1]?.[p[1]] === hat || randomArr?.[p[0] - 1]?.[p[1]] === hat) {
        console.table(visited, visitedTwice, visited3x, visited4x, visited5x);
        console.log(`solution found, reached postion ${p}`)
        return randomArr;
    }          
    //checking if neighbouring square hasn't been visited, if no, go there
    else if (randomArr?.[p[0]]?.[p[1]   1] === fieldCharacter && !visited.includes([ p[0], p[1]   1 ])) {
        p = [p[0], p[1]   1];
        visited.push([p[0], p[1]]);
        //console.log(visited.includes([ p[0], p[1]   1 ]), 0);
        //console.log(visited);
    } else if (randomArr?.[p[0]]?.[p[1] - 1] === fieldCharacter && !visited.includes([ p[0], p[1] - 1 ])) {
        p = [p[0], p[1] - 1];
        visited.push([p[0], p[1]]);
        //console.log(visited.includes([ p[0], p[1] - 1 ]), 1);
        //console.log(visited);
    } else if (randomArr?.[p[0]   1]?.[p[1]] === fieldCharacter && !visited.includes([ p[0]   1, p[1] ])) {
        p = [p[0]   1, p[1]];
        visited.push([p[0], p[1]]);
        //console.log(!visited.includes([ p[0]   1, p[1] ]), 2);
        //console.log(visited);
    } else if (randomArr?.[p[0] - 1]?.[p[1]] === fieldCharacter && !visited.includes([ p[0] - 1, p[1] ])) {
        p = [p[0] - 1, p[1]];
        visited.push([p[0], p[1]]);
        //console.log(!visited.includes([ p[0] - 1, p[1] ]), 3);
        //console.log(visited);

This code, keeps going over first 2 else if statements and keeps adding same element to visited array. (commented out console.log statements are for me to check what's going on).

CodePudding user response:

You can't use !visited.includes([ p[0], p[1] 1 ]). Arrays are matched by identity, not by comparing the contents, so this will never be true.

Use

!visited.some(([el1, el2]) => el1 == p[0] && el2 == p[1] 1)
  • Related