From 3f4aafd43e38103726c486e1d22f39bf8033bd8b Mon Sep 17 00:00:00 2001 From: Xavier Fontanet Date: Thu, 20 Jun 2024 17:15:45 +0200 Subject: [PATCH] firs step forward sucessfully --- .../codingtests/marsrover/Coordinates.java | 18 ++++++++++++++++-- .../hack3/codingtests/marsrover/MarsRover.java | 17 +++++++++++------ .../codingtests/marsrover/MarsRoverTest.java | 3 ++- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/main/java/cat/hack3/codingtests/marsrover/Coordinates.java b/src/main/java/cat/hack3/codingtests/marsrover/Coordinates.java index 39b5348..5499738 100644 --- a/src/main/java/cat/hack3/codingtests/marsrover/Coordinates.java +++ b/src/main/java/cat/hack3/codingtests/marsrover/Coordinates.java @@ -1,8 +1,17 @@ package cat.hack3.codingtests.marsrover; +/** + * Notice that the representation is the following: + * Latitude is Y, starts from 0 on the top and goes down incrementally + * Longitude is X, starts from 0 on the left and goes to right incrementally + * 0 1 2 3 + * 1 11 12 13 + * 2 12 22 23 + * 3 13 23 33 + */ public class Coordinates { - private final int latitude; - private final int longitude; + private int latitude; + private int longitude; public static Coordinates of(int latitude, int longitude) { return new Coordinates(latitude, longitude); @@ -13,6 +22,11 @@ public class Coordinates { this.longitude = longitude; } + public Coordinates incrementSouth() { + latitude++; + return this; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java b/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java index 896924f..dffbf20 100644 --- a/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java +++ b/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java @@ -2,16 +2,21 @@ package cat.hack3.codingtests.marsrover; public class MarsRover { private final MarsMap marsMap; - private final int latitudeStartingPoint; - private final int longitudeStartingPoint; + private Coordinates currentCoordinates; + private MarsMap.Direction currentDirection; - public MarsRover(MarsMap marsMap, int latitudeStartingPoint, int longitudeStartingPoint, MarsMap.Direction startingDirection) { + public MarsRover(MarsMap marsMap, Coordinates startingCoordinates, MarsMap.Direction startingDirection) { this.marsMap = marsMap; - this.latitudeStartingPoint = latitudeStartingPoint; - this.longitudeStartingPoint = longitudeStartingPoint; + currentCoordinates = startingCoordinates; + currentDirection = startingDirection; } public Coordinates moveForward() { - return Coordinates.of(0, 0); + return switch (currentDirection) { + case NORTH -> null; + case SOUTH -> currentCoordinates.incrementSouth(); + case EAST -> null; + case WEST -> null; + }; } } diff --git a/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java b/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java index 58aed53..4963376 100644 --- a/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java +++ b/src/test/java/cat/hack3/codingtests/marsrover/MarsRoverTest.java @@ -19,7 +19,8 @@ public class MarsRoverTest { int latitudeStartingPoint = 2; int longitudeStartingPoint = 3; - rover = new MarsRover(marsMap, latitudeStartingPoint, longitudeStartingPoint, SOUTH); + var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint); + rover = new MarsRover(marsMap, startingCoordinates, SOUTH); } @Test