diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a89a654 --- /dev/null +++ b/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + aatwi.dev + mars-rover-refactoring-kata + 1.0-SNAPSHOT + + + 1.8 + 1.8 + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + org.junit.platform + junit-platform-surefire-provider + 1.0.0 + + + org.junit.jupiter + junit-jupiter-engine + 5.0.0 + + + + + + + + + org.junit.jupiter + junit-jupiter-engine + 5.0.0 + test + + + org.junit.platform + junit-platform-surefire-provider + 1.0.0 + + + + \ No newline at end of file diff --git a/src/main/java/mars/rover/MarsRover.java b/src/main/java/mars/rover/MarsRover.java new file mode 100644 index 0000000..7dbedc1 --- /dev/null +++ b/src/main/java/mars/rover/MarsRover.java @@ -0,0 +1,42 @@ +package mars.rover; + +public class MarsRover { + + public static String move(int x, int y, char direction, String instructions) { + if (!instructions.isEmpty()) { + char instruction = instructions.charAt(0); + if (instruction == 'L') { + if (direction == 'N') { + return move(x, y, 'W', instructions.substring(1, instructions.length())); + } else if (direction == 'W') { + return move(x, y, 'S', instructions.substring(1, instructions.length())); + } else if (direction == 'S') { + return move(x, y, 'E', instructions.substring(1, instructions.length())); + } else if (direction == 'E') { + return move(x, y, 'N', instructions.substring(1, instructions.length())); + } + } else if (instruction == 'R') { + if (direction == 'N') { + return move(x, y, 'E', instructions.substring(1, instructions.length())); + } else if (direction == 'W') { + return move(x, y, 'N', instructions.substring(1, instructions.length())); + } else if (direction == 'S') { + return move(x, y, 'W', instructions.substring(1, instructions.length())); + } else if (direction == 'E') { + return move(x, y, 'S', instructions.substring(1, instructions.length())); + } + } else if (instruction == 'M') { + if (direction == 'N') { + return move(x, y + 1, 'N', instructions.substring(1, instructions.length())); + } else if (direction == 'S') { + return move(x, y - 1, 'S', instructions.substring(1, instructions.length())); + } else if (direction == 'W') { + return move(x - 1, y, 'W', instructions.substring(1, instructions.length())); + } else if (direction == 'E') { + return move(x + 1, y, 'E', instructions.substring(1, instructions.length())); + } + } + } + return x + " " + y + " " + direction; + } +} diff --git a/src/test/java/mars/rover/MarsRoverTest.java b/src/test/java/mars/rover/MarsRoverTest.java new file mode 100644 index 0000000..7289c3c --- /dev/null +++ b/src/test/java/mars/rover/MarsRoverTest.java @@ -0,0 +1,23 @@ +package mars.rover; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class MarsRoverTest { + + @Test + public void + acceptance_test_1() { + String newPosition = MarsRover.move(1, 2, 'N', "LMLMLMLMM"); + assertEquals("1 3 N", newPosition); + } + + @Test + public void + acceptance_test_2() { + String newPosition = MarsRover.move(3, 3, 'E', "MMRMMRMRRM"); + assertEquals("5 1 E", newPosition); + } + +} \ No newline at end of file