-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2] Submit the first version of the code
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
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |