Skip to content

Commit

Permalink
[#2] Submit the first version of the code
Browse files Browse the repository at this point in the history
1. Add MarsRover.java that includes the solution for the MarsRover problem
2. Add MarsRoverTest.java, this file only contains 2 acceptance tests
3. Add pom file
  • Loading branch information
aatwi committed Jan 14, 2019
1 parent 7d48413 commit 5aa927a
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
52 changes: 52 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>aatwi.dev</groupId>
<artifactId>mars-rover-refactoring-kata</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

</project>
42 changes: 42 additions & 0 deletions src/main/java/mars/rover/MarsRover.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
23 changes: 23 additions & 0 deletions src/test/java/mars/rover/MarsRoverTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 5aa927a

Please sign in to comment.