Skip to content

Commit

Permalink
feat: Move the robot
Browse files Browse the repository at this point in the history
  • Loading branch information
maiisthebest committed Mar 3, 2024
1 parent d3bd94b commit 225c042
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/Robot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,39 @@ class Robot {
this.y = y;
this.direction = direction;
}

isPlaced() {
return this.x !== undefined && this.y !== undefined;
}

calculateNewPosition() {
let newX = this.x;
let newY = this.y;

switch (this.direction) {
case "NORTH":
newY++;
break;
case "EAST":
newX++;
break;
case "SOUTH":
newY--;
break;
case "WEST":
newX--;
break;
}

return { newX, newY };
}

move() {
const { newX, newY } = this.calculateNewPosition();

this.x = newX;
this.y = newY;
}
}

module.exports = Robot;
47 changes: 46 additions & 1 deletion src/Robot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,56 @@ describe("Robot", () => {
const y = 3;
const direction = "WEST";

robot.place(x, y, direction);
robot.place(2, 3, "WEST");

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

describe("isPlaced()", () => {
it("should return true given the robot is already placed on a table", () => {
const robot = new Robot();

robot.place(2, 3, "WEST");

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

it("should return false given the robot is not placed on a table yet", () => {
const robot = new 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);
}
);
});
});
});
8 changes: 8 additions & 0 deletions src/RobotSimulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ class RobotSimulator {
place(x, y, direction) {
if (this.table.isOnTheTable(x, y)) this.robot.place(x, y, direction);
}

move() {
if (this.robot.isPlaced()) {
const { newX, newY } = this.robot.calculateNewPosition();

if (this.table.isOnTheTable(newX, newY)) this.robot.move();
}
}
}

module.exports = RobotSimulator;
60 changes: 60 additions & 0 deletions src/RobotSimulator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,64 @@ describe("RobotSimulator", () => {
}
);
});

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) and the new position is still inside the table",
({ currentX, currentY, currentDirection, newX, newY, newDirection }) => {
const table = new Table(5, 5);
const robot = new Robot();
const simulator = new RobotSimulator(table, robot);

simulator.place(currentX, currentY, currentDirection);

simulator.move();

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

it.each`
currentX | currentY | currentDirection
${4} | ${5} | ${"NORTH"}
${5} | ${0} | ${"EAST"}
${3} | ${0} | ${"SOUTH"}
${0} | ${5} | ${"WEST"}
`(
"should NOT move the robot given current position ($currentX, $currentY, $currentDirection) and the new position is outside of the table",
({ currentX, currentY, currentDirection }) => {
const table = new Table(5, 5);
const robot = new Robot();
const simulator = new RobotSimulator(table, robot);

simulator.place(currentX, currentY, currentDirection);

simulator.move();

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

it("should NOT move 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.move();

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

0 comments on commit 225c042

Please sign in to comment.