move strategy implemented
This commit is contained in:
@@ -9,4 +9,12 @@ import lombok.Getter;
|
||||
public class ToyBotField {
|
||||
private int width = 4;
|
||||
private int height = 4;
|
||||
|
||||
public boolean isInsideTheField(final Position position) {
|
||||
return (position.getX() >= 0 &&
|
||||
position.getY() >= 0 &&
|
||||
position.getX() <= width &&
|
||||
position.getY() <= height);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.MoveStrategy;
|
||||
import de.dj_steam.bot.moving.strategy.PlaceStrategy;
|
||||
import de.dj_steam.bot.moving.strategy.ReportStrategy;
|
||||
import de.dj_steam.bot.moving.strategy.TurnStrategy;
|
||||
@@ -45,5 +46,6 @@ public class RobotEngine {
|
||||
changingStrategies.add(new TurnStrategy());
|
||||
changingStrategies.add(new ReportStrategy());
|
||||
changingStrategies.add(new PlaceStrategy());
|
||||
changingStrategies.add(new MoveStrategy());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package de.dj_steam.bot.moving.strategy;
|
||||
|
||||
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 MoveStrategy implements ChangingStrategy {
|
||||
@Override
|
||||
public void change(final ToyBot toyBot, final ToyBotField toyBotField, final Command command) {
|
||||
if (command.getCommand().equals(Command.MOVE)) {
|
||||
Position newPosition = toyBot.getPosition();
|
||||
|
||||
// TODO - refactor this ugly code
|
||||
if (toyBot.getDirection().equals(Direction.NORTH)) {
|
||||
newPosition = new Position(toyBot.getPosition().getX(), toyBot.getPosition().getY() + 1);
|
||||
} else if (toyBot.getDirection().equals(Direction.EAST)) {
|
||||
newPosition = new Position(toyBot.getPosition().getX() + 1, toyBot.getPosition().getY());
|
||||
} else if (toyBot.getDirection().equals(Direction.SOUTH)) {
|
||||
newPosition = new Position(toyBot.getPosition().getX(), toyBot.getPosition().getY() - 1);
|
||||
} else if (toyBot.getDirection().equals(Direction.WEST)) {
|
||||
newPosition = new Position(toyBot.getPosition().getX() - 1, toyBot.getPosition().getY());
|
||||
}
|
||||
setNewPosition(toyBot, toyBotField, newPosition);
|
||||
}
|
||||
}
|
||||
|
||||
private void setNewPosition(ToyBot toyBot, final ToyBotField toyBotField, Position position) {
|
||||
if (toyBotField.isInsideTheField(position)) {
|
||||
toyBot.setPosition(position);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,6 @@ public class PlaceStrategy implements ChangingStrategy {
|
||||
|
||||
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()));
|
||||
return toyBotField.isInsideTheField(new Position(Integer.parseInt(arguments[0].trim()),Integer.parseInt(arguments[1].trim())));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user