JSFiddle - React, Tailwind, and code Playground
by Farzad YZ
JavaScript
class Rover {
constructor(position, initialDirection) {
//what passing to the rover x,y and what we want it to hold in a rover
//whatever con. has goes in a initialization. position and facing direction
this.position = position;
this.direction = initialDirection;
}
move() {
switch (this.direction) {
case "N":
this.position.y++;
break;
case "S":
this.position.y--;
break;
case "W":
this.position.x--;
break;
case "E":
this.position.x++;
break;
default:
}
}
// L, R
rotate(rotation) {
if (rotation === "L") {
if (this.direction === "N") {
this.direction = "W";
} else if (this.direction === "W") {
this.direction = "S";
} else if (this.direction === "S") {
this.direction = "E";
} else if (this.direction === "E") {
this.direction = "N";
}
}
if (rotation === "R") {
if (this.direction === "N") {
this.direction = "E";
} else if (this.direction === "W") {
this.direction = "N";
} else if (this.direction === "S") {
this.direction = "W";
} else if (this.direction === "E") {
this.direction = "S";
}
}
}
}
// instance of the rover class objects are instances of the class. rover can move behaviour, shape of the rover object.
//we can cretae inifinite amount of object of the class inheritcence polimorphysm.
// breed of birds/crawlers in a future there is a new type of animal which holds to features of two
//instead of class of factory pattern design patterns
/* const rover1 = new Rover({
x: 1,
y: 2
}, 'N'); */
/* rover1.move(); // (1,3) */
/* rover1.rotate("L"); // W */
/* console.log(rover1.direction); */
/* rover1.move(); // (0,3) */
/* console.log(rover1.position) */
function main(input) {
const info = input.split("\n\n");
const gridSize = info[0];
const rover1Position = info[1].split("...