implement place strategy, some improvements

This commit is contained in:
Stanislav Nowogrudski 2017-04-22 17:26:00 +02:00
parent 8572b88b14
commit 5ceb31ee33
11 changed files with 174 additions and 6 deletions

View File

@ -17,7 +17,7 @@ import de.dj_steam.bot.engine.RobotEngine;
public class LoopingConsole {
private static final String EXIT_COMMAND = "exit";
public static final String COMMAND_DELIMITER = " ";
private static final String COMMAND_DELIMITER = " ";
public static void main(final String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

View File

@ -1,5 +1,6 @@
package de.dj_steam.bot.domain;
import java.util.List;
import java.util.Optional;
import lombok.EqualsAndHashCode;
@ -7,6 +8,13 @@ import lombok.Getter;
/**
* @author steam
*
* this is a general command implementation.
*
* The arguments as {@link String} is intentionally, to be flexible
* define some different argument formats.
*
* Use some kind of {@link List} for arguments in futher implementations.
*/
@Getter
@ -20,6 +28,7 @@ public class Command {
public static final String REPORT = "REPORT";
private String command;
private Optional<String> arguments;
public Command(final String command, Optional<String> arguments) {

View File

@ -1,5 +1,6 @@
package de.dj_steam.bot.domain;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
@ -9,6 +10,7 @@ import lombok.ToString;
@Getter
@ToString
@EqualsAndHashCode
public class Position {
private int x;
private int y;

View File

@ -7,6 +7,6 @@ import lombok.Getter;
*/
@Getter
public class ToyBotField {
private int width = 5;
private int height = 5;
private int width = 4;
private int height = 4;
}

View File

@ -9,6 +9,7 @@ import de.dj_steam.bot.domain.Position;
import de.dj_steam.bot.domain.ToyBot;
import de.dj_steam.bot.domain.ToyBotField;
import de.dj_steam.bot.moving.strategy.ChangingStrategy;
import de.dj_steam.bot.moving.strategy.PlaceStrategy;
import de.dj_steam.bot.moving.strategy.ReportStrategy;
import de.dj_steam.bot.moving.strategy.TurnStrategy;
@ -27,7 +28,7 @@ public class RobotEngine {
initChangingStrategies();
}
public RobotEngine(final ToyBotField toyBotField, final ToyBot toyBot) {
RobotEngine(final ToyBotField toyBotField, final ToyBot toyBot) {
this.toyBotField = toyBotField;
this.toyBot = toyBot;
initChangingStrategies();
@ -43,5 +44,6 @@ public class RobotEngine {
changingStrategies = new ArrayList<>();
changingStrategies.add(new TurnStrategy());
changingStrategies.add(new ReportStrategy());
changingStrategies.add(new PlaceStrategy());
}
}

View File

@ -6,7 +6,13 @@ import de.dj_steam.bot.domain.ToyBotField;
/**
* @author steam
*
* this strategy defines how the robot will be manipulated
* implement this, if new change variant is wanted.
*
* use {@link Command} to define new commans to implement
*/
public interface ChangingStrategy {
void change(ToyBot toyBot, ToyBotField toyBotField, Command command);
}

View File

@ -0,0 +1,45 @@
package de.dj_steam.bot.moving.strategy;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import de.dj_steam.bot.domain.Command;
import de.dj_steam.bot.domain.Direction;
import de.dj_steam.bot.domain.Position;
import de.dj_steam.bot.domain.ToyBot;
import de.dj_steam.bot.domain.ToyBotField;
/**
* @author steam
*/
public class PlaceStrategy implements ChangingStrategy {
@Override
public void change(ToyBot toyBot, ToyBotField toyBotField, Command command) {
if (command.getCommand().equals(Command.PLACE) &&
isPlaceCommandValid(command) &&
canBePlaced(toyBotField, command)) {
String[] arguments = command.getArguments().get().split(",");
toyBot.setPosition(new Position(Integer.parseInt(arguments[0].trim()), Integer.parseInt(arguments[1].trim())));
toyBot.setDirection(Direction.valueOf(arguments[2].trim()));
}
}
boolean isPlaceCommandValid(Command command) {
if (command.getArguments().isPresent()) {
String[] arguments = command.getArguments().get().split(",");
return arguments.length == 3 &&
StringUtils.isNumeric(arguments[0].trim()) &&
StringUtils.isNumeric(arguments[1].trim()) &&
EnumUtils.isValidEnum(Direction.class, arguments[2].trim());
}
return false;
}
boolean canBePlaced(final ToyBotField toyBotField, final Command command) {
String[] arguments = command.getArguments().get().split(",");
return (toyBotField.getWidth() >= Integer.parseInt(arguments[0].trim()) &&
toyBotField.getHeight() >= Integer.parseInt(arguments[1].trim()));
}
}

View File

@ -9,7 +9,7 @@ import de.dj_steam.bot.domain.ToyBotField;
*/
public class ReportStrategy implements ChangingStrategy {
@Override
public void change(ToyBot toyBot, ToyBotField toyBotField, Command command) {
public void change(final ToyBot toyBot, final ToyBotField toyBotField, final Command command) {
if (command.getCommand().equals(Command.REPORT)) {
printReport(toyBot);
}

View File

@ -17,7 +17,7 @@ public class TurnStrategy implements ChangingStrategy {
}
@Override
public void change(ToyBot toyBot, ToyBotField toyBotField, Command command) {
public void change(final ToyBot toyBot, final ToyBotField toyBotField, final Command command) {
if (command.getCommand().equals(Command.TURN_LEFT) || command.getCommand().equals(Command.TURN_RIGHT)) {
toyBot.setDirection(directionCalculator.calculateNewDirection(toyBot, command));
}

View File

@ -66,4 +66,26 @@ public class RobotEngineTest {
robotEngine.commandBot(turnRightCommand);
assertEquals(Direction.NORTH, toyBot.getDirection());
}
@Test
public void testPlaceCommand() {
Command placeCommand = new Command(Command.PLACE, Optional.empty());
assertEquals(new Position(0, 0), toyBot.getPosition());
robotEngine.commandBot(placeCommand);
assertEquals(new Position(0, 0), toyBot.getPosition());
placeCommand = new Command(Command.PLACE, Optional.of("0,2,NORTH"));
robotEngine.commandBot(placeCommand);
assertEquals(new Position(0, 2), toyBot.getPosition());
placeCommand = new Command(Command.PLACE, Optional.of("5,1,NORTH"));
robotEngine.commandBot(placeCommand);
assertEquals(new Position(0, 2), toyBot.getPosition());
placeCommand = new Command(Command.PLACE, Optional.of("0,3,WEST"));
robotEngine.commandBot(placeCommand);
assertEquals(new Position(0,3), toyBot.getPosition());
assertEquals(Direction.WEST, toyBot.getDirection());
}
}

View File

@ -0,0 +1,82 @@
package de.dj_steam.bot.moving.strategy;
import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import de.dj_steam.bot.domain.Command;
import de.dj_steam.bot.domain.Direction;
import de.dj_steam.bot.domain.Position;
import de.dj_steam.bot.domain.ToyBot;
import de.dj_steam.bot.domain.ToyBotField;
/**
* @author steam
*/
public class PlaceStrategyTest {
private PlaceStrategy placeStrategy;
private ToyBot toyBot;
@Before
public void setup() {
placeStrategy = new PlaceStrategy();
toyBot = new ToyBot(Direction.NORTH, new Position(0, 0));
}
@Test
public void testPlaceCommandValid() {
assertTrue(placeStrategy.isPlaceCommandValid(new Command(Command.PLACE, Optional.of("1,2,EAST"))));
assertTrue(placeStrategy.isPlaceCommandValid(new Command(Command.PLACE, Optional.of("1 ,2 ,EAST"))));
assertTrue(placeStrategy.isPlaceCommandValid(new Command(Command.PLACE, Optional.of("1 ,2,EAST"))));
assertTrue(placeStrategy.isPlaceCommandValid(new Command(Command.PLACE, Optional.of("1 ,2, EAST "))));
}
@Test
public void testPlaceCommandInvalid() {
assertFalse(placeStrategy.isPlaceCommandValid(new Command(Command.PLACE, Optional.empty())));
assertFalse(placeStrategy.isPlaceCommandValid(new Command(Command.PLACE, Optional.of("a,2,EAST"))));
assertFalse(placeStrategy.isPlaceCommandValid(new Command(Command.PLACE, Optional.of("1,2,NOTHING"))));
assertFalse(placeStrategy.isPlaceCommandValid(new Command(Command.PLACE, Optional.of("1,2"))));
assertFalse(placeStrategy.isPlaceCommandValid(new Command(Command.PLACE, Optional.of("a"))));
assertFalse(placeStrategy.isPlaceCommandValid(new Command(Command.PLACE, Optional.of("a,"))));
}
@Test
public void testNotCanBePlaced() {
assertFalse(placeStrategy.canBePlaced(new ToyBotField(), new Command(Command.PLACE, Optional.of("5,5,EAST"))));
assertFalse(placeStrategy.canBePlaced(new ToyBotField(), new Command(Command.PLACE, Optional.of("4,5,EAST"))));
assertFalse(placeStrategy.canBePlaced(new ToyBotField(), new Command(Command.PLACE, Optional.of("5,4,EAST"))));
}
@Test
public void testCanBePlaced() {
assertTrue(placeStrategy.canBePlaced(new ToyBotField(), new Command(Command.PLACE, Optional.of("0,0,EAST"))));
assertTrue(placeStrategy.canBePlaced(new ToyBotField(), new Command(Command.PLACE, Optional.of("4,4,EAST"))));
assertTrue(placeStrategy.canBePlaced(new ToyBotField(), new Command(Command.PLACE, Optional.of("4 , 4 ,EAST"))));
}
@Test
public void testPlaceStrategyPositiveTest() {
placeStrategy.change(toyBot, new ToyBotField(), new Command(Command.PLACE, Optional.of("0,2,NORTH")));
assertEquals(new Position(0, 2), toyBot.getPosition());
}
@Test
public void testPlaceStrategyNegativeTest() {
placeStrategy.change(toyBot, new ToyBotField(), new Command(Command.PLACE, Optional.of("5,2,NORTH")));
assertEquals(new Position(0, 0), toyBot.getPosition());
}
@Test
public void testPlaceStrategyDirection() {
placeStrategy.change(toyBot, new ToyBotField(), new Command(Command.PLACE, Optional.of("0,2,WEST")));
assertEquals(Direction.WEST, toyBot.getDirection());
}
}