Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 14, 2023
1 parent f0be3b3 commit 2c51a0e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ public void turnRight() {
}

// l : left, r : right, f : forward, b : back
public void receiveCommands(String commands) {
public String receiveCommands(String commands) {

StringBuilder output = new StringBuilder();

for (char cmd : commands.toCharArray()){
boolean status = true;
System.out.println("_cmd = " + cmd);
switch (cmd){
case 'l':
Expand All @@ -69,14 +72,20 @@ public void receiveCommands(String commands) {
turnRight();
break;
case 'f':
moveForward();
status = moveForward();
break;
case 'b':
moveBackward();
status = moveBackward();
break;
}
}

if (status){
output.append('O');
}else{
output.append('X');
}
}
return output.toString();
}

public Planet getPlanet() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void whenMoveBackwardThenBackward() {
obstacles.add(new Point(45, 46));
Planet planet = new Planet(max, obstacles);
Ship ship = new Ship(location, planet);

// Point max = new Point(50, 50);
// Location location = new Location(new Point(21, 13), Direction.NORTH);
// Planet planet = new Planet(max);
Expand Down Expand Up @@ -260,4 +260,22 @@ public void whenReceiveCommandsThenStopOnObstacle() {
assertEquals(ship.getLocation(), expected);
}

@Test
public void whenReceiveCommandsThenOForOkAndXForObstacle() {

// https://bitbucket.org/vfarcic/tdd-java-ch04-ship/src/5b832a142fab70505af2d9e3c7896cc5079bce32/src/test/java/com/packtpublishing/tddjava/ch04ship/ShipSpec.java#lines-19
Point max = new Point(50, 50);
Location location = new Location(new Point(21, 13), Direction.NORTH);
//Planet planet = new Planet(max, obstacles);
//Ship ship = new Ship(location, planet);
Planet planet = new Planet(max);
Ship ship = new Ship(location, planet);

List<Point> obstacles = new ArrayList<>();
obstacles.add(new Point(location.getX() + 1, location.getY()));
ship.getPlanet().setObstacles(obstacles);
String status = ship.receiveCommands("rflb");
assertEquals(status, "OXOO");
}

}

0 comments on commit 2c51a0e

Please sign in to comment.