extract Direction from MarsMap
This commit is contained in:
parent
b1ecef985b
commit
b0e6fc197f
|
@ -1,7 +1,6 @@
|
||||||
package cat.hack3.codingtests.marsrover;
|
package cat.hack3.codingtests.marsrover;
|
||||||
|
|
||||||
|
|
||||||
import cat.hack3.codingtests.marsrover.MarsMap.Direction;
|
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package cat.hack3.codingtests.marsrover;
|
||||||
|
|
||||||
|
public enum Direction {
|
||||||
|
NORTH, SOUTH, EAST, WEST;
|
||||||
|
|
||||||
|
public Direction reversed() {
|
||||||
|
return switch (this) {
|
||||||
|
case NORTH -> SOUTH;
|
||||||
|
case SOUTH -> NORTH;
|
||||||
|
case EAST -> WEST;
|
||||||
|
case WEST -> EAST;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public Direction changeDirectionToLeft() {
|
||||||
|
return switch (this) {
|
||||||
|
case NORTH -> WEST;
|
||||||
|
case SOUTH -> EAST;
|
||||||
|
case EAST -> NORTH;
|
||||||
|
case WEST -> SOUTH;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,18 +1,6 @@
|
||||||
package cat.hack3.codingtests.marsrover;
|
package cat.hack3.codingtests.marsrover;
|
||||||
|
|
||||||
import static cat.hack3.codingtests.marsrover.MarsMap.Direction.*;
|
|
||||||
|
|
||||||
public class MarsMap {
|
public class MarsMap {
|
||||||
public enum Direction {NORTH, SOUTH, EAST, WEST;
|
|
||||||
public Direction reversed() {
|
|
||||||
return switch (this) {
|
|
||||||
case NORTH -> SOUTH;
|
|
||||||
case SOUTH -> NORTH;
|
|
||||||
case EAST -> WEST;
|
|
||||||
case WEST -> EAST;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
private final int width;
|
private final int width;
|
||||||
private final int height;
|
private final int height;
|
||||||
|
|
||||||
|
@ -21,12 +9,4 @@ public class MarsMap {
|
||||||
this.height = height;
|
this.height = height;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Direction changeDirectionToLeft(Direction currentDirection) {
|
|
||||||
return switch (currentDirection) {
|
|
||||||
case NORTH -> WEST;
|
|
||||||
case SOUTH -> EAST;
|
|
||||||
case EAST -> NORTH;
|
|
||||||
case WEST -> SOUTH;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,9 @@ package cat.hack3.codingtests.marsrover;
|
||||||
public class MarsRover {
|
public class MarsRover {
|
||||||
private final MarsMap marsMap;
|
private final MarsMap marsMap;
|
||||||
private final Coordinates currentCoordinates;
|
private final Coordinates currentCoordinates;
|
||||||
private MarsMap.Direction currentDirection;
|
private Direction currentDirection;
|
||||||
|
|
||||||
public MarsRover(MarsMap marsMap, Coordinates startingCoordinates, MarsMap.Direction startingDirection) {
|
public MarsRover(MarsMap marsMap, Coordinates startingCoordinates, Direction startingDirection) {
|
||||||
this.marsMap = marsMap;
|
this.marsMap = marsMap;
|
||||||
currentCoordinates = startingCoordinates;
|
currentCoordinates = startingCoordinates;
|
||||||
currentDirection = startingDirection;
|
currentDirection = startingDirection;
|
||||||
|
@ -19,8 +19,8 @@ public class MarsRover {
|
||||||
return currentCoordinates.updatePositionTowards(currentDirection.reversed());
|
return currentCoordinates.updatePositionTowards(currentDirection.reversed());
|
||||||
}
|
}
|
||||||
|
|
||||||
public MarsMap.Direction turnLeft() {
|
public Direction turnLeft() {
|
||||||
currentDirection = marsMap.changeDirectionToLeft(currentDirection);
|
currentDirection = currentDirection.changeDirectionToLeft();
|
||||||
return currentDirection;
|
return currentDirection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ public class MarsRover {
|
||||||
return currentCoordinates.copy();
|
return currentCoordinates.copy();
|
||||||
}
|
}
|
||||||
|
|
||||||
public MarsMap.Direction getCurrentDirection() {
|
public Direction getCurrentDirection() {
|
||||||
return currentDirection;
|
return currentDirection;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,7 @@ import org.testng.annotations.Test;
|
||||||
import java.util.function.IntConsumer;
|
import java.util.function.IntConsumer;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
import static cat.hack3.codingtests.marsrover.MarsMap.Direction;
|
import static cat.hack3.codingtests.marsrover.Direction.*;
|
||||||
import static cat.hack3.codingtests.marsrover.MarsMap.Direction.*;
|
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
public class MarsRoverTest {
|
public class MarsRoverTest {
|
||||||
|
|
Loading…
Reference in New Issue