https://editor.p5js.org/paulgoux/sketches/eiY8BkMUm
So I’m trying to calculate the steps in between each node, and this is working seemingly ok, however some will be out by about 1-5 steps. I don’t understand whats causing this. The main sketch is above and this is the function used to travel from node to node.
Please note, nodefinder and nodebackup are populated once you press generate nodes, however you need to wait for the maze to have completed first. Nodefinder identifies a node based on its number of neighbours, a node is a node if it does not have 2 neighbours. Then this.findnode populates the this.parents if a cell if not a node using some if statements.
The nodefinder function uses nodefinder[i] as a starting point, uses a function called next to check if its neighbouring cell has open walls and updates the neighbouring cells “array” value with its id and a boolean true, so it knows that that node has visited that cell.
So far so good. However when I updated the steps it generates approximately correct steps but with some errors.
var count = 0;
function findnode(){
var step = 0;
if(findnodes.toggle===1){
for(var i=0;i<nodefinder.length;i++){
var a = nodebackup[i].oid.toString();
var b = nodebackup[i].oid
nodefinder[i].array[b] = true;
var next = nodefinder[i].newNeighbour(b);
nodefinder[i].highlight();
var steps = nodebackup[i].steps;
//count ++;
if(next){
count++;
//nodefinde
nodebackup[i].steps[0] ++;
nodebackup[i].steps[1] ++;
if(next.node){
var a = nodefinder[i].parent[0]
var b = nodefinder[i].parent[1]
if(a ===nodebackup[i]){
nodebackup[i].c1[nodebackup[i].c2][1] = nodefinder[i].parent[1];
nodebackup[i].c1[nodebackup[i].c2][0] = steps[1];
}
else if(b === nodebackup[i]){
nodebackup[i].c1[nodebackup[i].c2][1] = nodefinder[i].parent[0];
nodebackup[i].c1[nodebackup[i].c2][0] = steps[0];
}
if(nodestack[i].length>0){
nodefinder[i] = nodestack[i].pop();
nodebackup[i].c2 ++;
nodebackup[i].steps = [0,0];
}
}
else{
nodestack[i].push(nodefinder[i]);
var a = next.parent[0]
var b = next.parent[1]
if(a ===nodebackup[i]){
nodebackup[i].c1[nodebackup[i].c2][1] = next.parent[1];
nodebackup[i].c1[nodebackup[i].c2][0] = steps[1];
}
else if(b === nodebackup[i]){
nodebackup[i].c1[nodebackup[i].c2][1] = next.parent[0];
nodebackup[i].c1[nodebackup[i].c2][0] = steps[0];
}
if(!next.c1[0][1] ){
next.c1[0][0] = steps[0];
next.c1[0][1] = nodebackup[i];
}
else{
next.c1[1][0] = steps[1];
next.c1[1][1] = nodebackup[i];
}
nodefinder[i] = next;
}
}
else if(nodestack[i].length>0){
nodefinder[i] = nodestack[i].pop();
if(nodefinder[i] === nodebackup[i]&&count>0){
nodebackup[i].c2 ++;
nodebackup[i].steps = [0,0];
}
}}
}
};