class Knot {
constructor(idx) {
this.idx = idx;
this.x = 0;
this.y = 0;
}
followKnot(knot) {
if (this.idx === 0) return;
let distX = knot.x - this.x;
let distY = knot.y - this.y;
if (Math.abs(distX) === 2) {
if (this.x < knot.x) this.x++;
else this.x--;
if (this.y < knot.y) this.y++;
else if (this.y > knot.y) this.y--;
} else if (Math.abs(distY) === 2) {
if (this.y < knot.y) this.y++;
else this.y--;
if (this.x < knot.x) this.x++;
else if (this.x > knot.x) this.x--;
}
}
move(direction) {
switch (direction) {
case "R":
this.x++;
break;
case "L":
this.x--;
break;
case "U":
this.y++;
break;
case "D":
this.y--;
break;
}
}
dump() {
return { x: this.x, y: this.y, idx: this.idx };
}
}