Skip to content

Commit

Permalink
feat: Implement turn left and right
Browse files Browse the repository at this point in the history
  • Loading branch information
maiisthebest committed Mar 3, 2024
1 parent 225c042 commit 49a2c7b
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 28 deletions.
20 changes: 20 additions & 0 deletions src/Robot.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const directions = ["NORTH", "EAST", "SOUTH", "WEST"];

class Robot {
constructor() {}

Expand Down Expand Up @@ -39,6 +41,24 @@ class Robot {
this.x = newX;
this.y = newY;
}

turnLeft() {
const newDirectionIndex = directions.indexOf(this.direction) - 1;

this.direction =
newDirectionIndex < 0
? directions[newDirectionIndex + directions.length]
: directions[newDirectionIndex];
}

turnRight() {
const newDirectionIndex = directions.indexOf(this.direction) + 1;

this.direction =
newDirectionIndex >= directions.length
? directions[newDirectionIndex - directions.length]
: directions[newDirectionIndex];
}
}

module.exports = Robot;
93 changes: 65 additions & 28 deletions src/Robot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,71 @@ describe("Robot", () => {

expect(robot.isPlaced()).toBe(false);
});
});

describe("move()", () => {
it.each`
currentX | currentY | currentDirection | newX | newY | newDirection
${3} | ${4} | ${"NORTH"} | ${3} | ${5} | ${"NORTH"}
${0} | ${0} | ${"EAST"} | ${1} | ${0} | ${"EAST"}
${3} | ${3} | ${"SOUTH"} | ${3} | ${2} | ${"SOUTH"}
${5} | ${5} | ${"WEST"} | ${4} | ${5} | ${"WEST"}
`(
"should move the robot one unit forward in the direction it is facing given current position ($currentX, $currentY, $currentDirection)",
({
currentX,
currentY,
currentDirection,
newX,
newY,
newDirection,
}) => {
const robot = new Robot();

robot.place(currentX, currentY, currentDirection);
robot.move();

expect(robot.x).toBe(newX);
expect(robot.y).toBe(newY);
expect(robot.direction).toBe(newDirection);
}
);
});
describe("move()", () => {
it.each`
currentX | currentY | currentDirection | newX | newY | newDirection
${3} | ${4} | ${"NORTH"} | ${3} | ${5} | ${"NORTH"}
${0} | ${0} | ${"EAST"} | ${1} | ${0} | ${"EAST"}
${3} | ${3} | ${"SOUTH"} | ${3} | ${2} | ${"SOUTH"}
${5} | ${5} | ${"WEST"} | ${4} | ${5} | ${"WEST"}
`(
"should move the robot one unit forward in the direction it is facing given current position ($currentX, $currentY, $currentDirection)",
({ currentX, currentY, currentDirection, newX, newY, newDirection }) => {
const robot = new Robot();

robot.place(currentX, currentY, currentDirection);
robot.move();

expect(robot.x).toBe(newX);
expect(robot.y).toBe(newY);
expect(robot.direction).toBe(newDirection);
}
);
});

describe("turnLeft()", () => {
it.each`
currentX | currentY | currentDirection | newDirection
${3} | ${4} | ${"NORTH"} | ${"WEST"}
${0} | ${0} | ${"EAST"} | ${"NORTH"}
${3} | ${3} | ${"SOUTH"} | ${"EAST"}
${5} | ${5} | ${"WEST"} | ${"SOUTH"}
`(
"should rotate the robot 90 degrees to LEFT given current position ($currentX, $currentY, $currentDirection)",
({ currentX, currentY, currentDirection, newDirection }) => {
const robot = new Robot();

robot.place(currentX, currentY, currentDirection);
robot.turnLeft();

expect(robot.x).toBe(currentX);
expect(robot.y).toBe(currentY);
expect(robot.direction).toBe(newDirection);
}
);
});

describe("turnRight()", () => {
it.each`
currentX | currentY | currentDirection | newDirection
${3} | ${4} | ${"NORTH"} | ${"EAST"}
${0} | ${0} | ${"EAST"} | ${"SOUTH"}
${3} | ${3} | ${"SOUTH"} | ${"WEST"}
${5} | ${5} | ${"WEST"} | ${"NORTH"}
`(
"should rotate the robot 90 degrees to RIGHT given current position ($currentX, $currentY, $currentDirection)",
({ currentX, currentY, currentDirection, newDirection }) => {
const robot = new Robot();

robot.place(currentX, currentY, currentDirection);
robot.turnRight();

expect(robot.x).toBe(currentX);
expect(robot.y).toBe(currentY);
expect(robot.direction).toBe(newDirection);
}
);
});
});
8 changes: 8 additions & 0 deletions src/RobotSimulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ class RobotSimulator {
if (this.table.isOnTheTable(newX, newY)) this.robot.move();
}
}

turnLeft() {
if (this.robot.isPlaced()) this.robot.turnLeft();
}

turnRight() {
if (this.robot.isPlaced()) this.robot.turnRight();
}
}

module.exports = RobotSimulator;
62 changes: 62 additions & 0 deletions src/RobotSimulator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,66 @@ describe("RobotSimulator", () => {
expect(robot.direction).toBe(undefined);
});
});

describe("turnLeft()", () => {
it("should rotate the robot 90 degrees to LEFT", () => {
const table = new Table(5, 5);
const robot = new Robot();
const simulator = new RobotSimulator(table, robot);

const currentX = 0;
const currentY = 0;
const currentDirection = "NORTH";
simulator.place(currentX, currentY, currentDirection);

simulator.turnLeft();

expect(robot.x).toBe(currentX);
expect(robot.y).toBe(currentY);
expect(robot.direction).toBe("WEST");
});

it("should NOT turn the robot given the robot is not placed on the table yet", () => {
const table = new Table(5, 5);
const robot = new Robot();
const simulator = new RobotSimulator(table, robot);

simulator.turnLeft();

expect(robot.x).toBe(undefined);
expect(robot.y).toBe(undefined);
expect(robot.direction).toBe(undefined);
});
});

describe("turnRight()", () => {
it("should rotate the robot 90 degrees to RIGHT", () => {
const table = new Table(5, 5);
const robot = new Robot();
const simulator = new RobotSimulator(table, robot);

const currentX = 1;
const currentY = 1;
const currentDirection = "WEST";
simulator.place(currentX, currentY, currentDirection);

simulator.turnRight();

expect(robot.x).toBe(currentX);
expect(robot.y).toBe(currentY);
expect(robot.direction).toBe("NORTH");
});

it("should NOT turn the robot given the robot is not placed on the table yet", () => {
const table = new Table(5, 5);
const robot = new Robot();
const simulator = new RobotSimulator(table, robot);

simulator.turnRight();

expect(robot.x).toBe(undefined);
expect(robot.y).toBe(undefined);
expect(robot.direction).toBe(undefined);
});
});
});

0 comments on commit 49a2c7b

Please sign in to comment.