Here I am again…
I ended up not quite using the algorithm you provided. I may do so in the future and it may well be better than my algorithm, but…
- I´m a beginner in js
- I´m not (yet) comfortable with the idea and use of callback functions
- I don´t fully understand the benefit of your algorithm compared to mine.
What did I do?
addLines(cell) {
for (let newLine of cell.neighbors) {
// Suchen, ob es diese Zeile schon gibt
let pushIt = true;
for (let i = 0; i < this.lines.length; i++) {
let lineExists = 0;
for (let j = 0; j < newLine.length; j++) {
// if (this.lines[i].includes(newLine[j])) {
// //passenden Merker setzen
// lineExists += 1;
// }
for (let k = 0; k < this.lines[i].length; k++) {
if (this.lines[i][k].equals(newLine[j])) {
//count number of identical vectors per line
lineExists += 1;
}
}
}
// if all vectors are identical
if (lineExists == newLine.length) {
pushIt = false;
break;
}
}
if (pushIt) {
this.lines.push(newLine);
}
}
}
I added a 4th layer of nested loops to go throug the elements of the existing line(s) and the new line to compare them.
I used p5.Vector.equals() instead of == to compare the vectors. This is the core point I took from your suggestion and I´m sure it was my main mistake.
As far as I understand it the explicit check for same length in your function rowsContainSameVecs() is an improvement over my algorithm.
I think that one is minor in my special case, as I ensure lines of length 4 at creation of my inputs.
And your use of array.some() with your callback is presumably faster than my additional loop.
One point I don´t really understand is:
what does the “this” point to in the line
const same = vec.equals(this);
```?