From 49a2c7b383a3c2d2b4c1f3a0289185369358ff3d Mon Sep 17 00:00:00 2001 From: Phuong Mai Nguyen Date: Sun, 3 Mar 2024 22:40:04 +1100 Subject: [PATCH] feat: Implement turn left and right --- src/Robot.js | 20 ++++++++ src/Robot.test.js | 93 ++++++++++++++++++++++++++------------ src/RobotSimulator.js | 8 ++++ src/RobotSimulator.test.js | 62 +++++++++++++++++++++++++ 4 files changed, 155 insertions(+), 28 deletions(-) diff --git a/src/Robot.js b/src/Robot.js index e0ee865..45e2a08 100644 --- a/src/Robot.js +++ b/src/Robot.js @@ -1,3 +1,5 @@ +const directions = ["NORTH", "EAST", "SOUTH", "WEST"]; + class Robot { constructor() {} @@ -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; diff --git a/src/Robot.test.js b/src/Robot.test.js index 8cf3638..a527747 100644 --- a/src/Robot.test.js +++ b/src/Robot.test.js @@ -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); + } + ); }); }); diff --git a/src/RobotSimulator.js b/src/RobotSimulator.js index 8b36ba3..122e663 100644 --- a/src/RobotSimulator.js +++ b/src/RobotSimulator.js @@ -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; diff --git a/src/RobotSimulator.test.js b/src/RobotSimulator.test.js index 451e3e8..fb8cd1d 100644 --- a/src/RobotSimulator.test.js +++ b/src/RobotSimulator.test.js @@ -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); + }); + }); });