move strategy implemented
This commit is contained in:
parent
1b7e21c8ee
commit
6c9740bf4e
@ -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())));
|
||||
}
|
||||
}
|
||||
|
26
src/test/java/de/dj_steam/bot/domain/ToyBotFieldTest.java
Normal file
26
src/test/java/de/dj_steam/bot/domain/ToyBotFieldTest.java
Normal file
@ -0,0 +1,26 @@
|
||||
package de.dj_steam.bot.domain;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author steam
|
||||
*/
|
||||
public class ToyBotFieldTest {
|
||||
@Test
|
||||
public void isInsideTheField() throws Exception {
|
||||
ToyBotField toyBotField = new ToyBotField();
|
||||
assertTrue(toyBotField.isInsideTheField(new Position(0,0)));
|
||||
assertTrue(toyBotField.isInsideTheField(new Position(4,4)));
|
||||
|
||||
assertFalse(toyBotField.isInsideTheField(new Position( -1,-1)));
|
||||
assertFalse(toyBotField.isInsideTheField(new Position(5,5)));
|
||||
|
||||
assertFalse(toyBotField.isInsideTheField( new Position(-1,4)));
|
||||
assertFalse(toyBotField.isInsideTheField( new Position(1,5)));
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -21,18 +21,26 @@ public class RobotEngineTest {
|
||||
private RobotEngine robotEngine;
|
||||
private ToyBot toyBot;
|
||||
|
||||
private Command turnLeftCommand;
|
||||
private Command turnRightCommand;
|
||||
private Command moveCommand;
|
||||
private Command placeCommand;
|
||||
|
||||
private ToyBotField toyBotField = new ToyBotField();
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
toyBot = new ToyBot(Direction.NORTH, new Position());
|
||||
robotEngine = new RobotEngine(toyBotField, toyBot);
|
||||
turnLeftCommand = new Command(Command.TURN_LEFT, Optional.empty());
|
||||
turnRightCommand = new Command(Command.TURN_RIGHT, Optional.empty());
|
||||
moveCommand = new Command(Command.MOVE, Optional.empty());
|
||||
placeCommand = new Command(Command.PLACE, Optional.empty());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTurnLeftCommand() {
|
||||
Command turnLeftCommand = new Command(Command.TURN_LEFT, Optional.empty());
|
||||
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
|
||||
robotEngine.commandBot(turnLeftCommand);
|
||||
@ -50,8 +58,6 @@ public class RobotEngineTest {
|
||||
|
||||
@Test
|
||||
public void testTurnRightCommand() {
|
||||
|
||||
Command turnRightCommand = new Command(Command.TURN_RIGHT, Optional.empty());
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
|
||||
robotEngine.commandBot(turnRightCommand);
|
||||
@ -69,7 +75,6 @@ public class RobotEngineTest {
|
||||
|
||||
@Test
|
||||
public void testPlaceCommand() {
|
||||
Command placeCommand = new Command(Command.PLACE, Optional.empty());
|
||||
assertEquals(new Position(0, 0), toyBot.getPosition());
|
||||
|
||||
robotEngine.commandBot(placeCommand);
|
||||
@ -85,7 +90,50 @@ public class RobotEngineTest {
|
||||
|
||||
placeCommand = new Command(Command.PLACE, Optional.of("0,3,WEST"));
|
||||
robotEngine.commandBot(placeCommand);
|
||||
assertEquals(new Position(0,3), toyBot.getPosition());
|
||||
assertEquals(new Position(0, 3), toyBot.getPosition());
|
||||
assertEquals(Direction.WEST, toyBot.getDirection());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveCommand() {
|
||||
assertEquals(new Position(0, 0), toyBot.getPosition());
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
|
||||
for (int i = 1; i <= 4; i++) {
|
||||
robotEngine.commandBot(moveCommand);
|
||||
assertEquals(new Position(0, i), toyBot.getPosition());
|
||||
}
|
||||
|
||||
robotEngine.commandBot(moveCommand);
|
||||
assertEquals(new Position(0, 4), toyBot.getPosition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRunningRobotUiiiiii() {
|
||||
assertEquals(new Position(0, 0), toyBot.getPosition());
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
|
||||
robotEngine.commandBot(moveCommand);
|
||||
assertEquals(new Position(0, 1), toyBot.getPosition());
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
|
||||
robotEngine.commandBot(turnRightCommand);
|
||||
assertEquals(Direction.EAST, toyBot.getDirection());
|
||||
|
||||
for (int i = 0; i <= 3; i++) {
|
||||
robotEngine.commandBot(moveCommand);
|
||||
}
|
||||
|
||||
assertEquals(new Position(4, 1), toyBot.getPosition());
|
||||
|
||||
robotEngine.commandBot(moveCommand); // border
|
||||
assertEquals(new Position(4, 1), toyBot.getPosition());
|
||||
|
||||
robotEngine.commandBot(turnLeftCommand);
|
||||
for (int i = 0; i <= 3; i++) {
|
||||
robotEngine.commandBot(moveCommand);
|
||||
}
|
||||
|
||||
assertEquals(new Position(4, 4), toyBot.getPosition());
|
||||
}
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
package de.dj_steam.bot.moving.strategy;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
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 MoveStrategyTest {
|
||||
|
||||
private ToyBotField toyBotField;
|
||||
private MoveStrategy moveStrategy;
|
||||
private Command moveCommand;
|
||||
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
toyBotField = new ToyBotField();
|
||||
moveStrategy = new MoveStrategy();
|
||||
moveCommand = new Command(Command.MOVE, Optional.empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveToNorthAllowed() {
|
||||
Position startPosition = new Position(0,0);
|
||||
ToyBot toyBot = new ToyBot(Direction.NORTH, startPosition);
|
||||
assertEquals(startPosition, toyBot.getPosition());
|
||||
moveStrategy.change(toyBot, toyBotField, moveCommand);
|
||||
assertEquals(new Position(0,1), toyBot.getPosition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveToWestAllowed() {
|
||||
Position startPosition = new Position(1,0);
|
||||
ToyBot toyBot = new ToyBot(Direction.WEST, startPosition);
|
||||
assertEquals(startPosition, toyBot.getPosition());
|
||||
moveStrategy.change(toyBot, toyBotField, moveCommand);
|
||||
assertEquals(new Position(0,0), toyBot.getPosition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveToEastAllowed() {
|
||||
Position startPosition = new Position(0,0);
|
||||
ToyBot toyBot = new ToyBot(Direction.EAST, startPosition);
|
||||
assertEquals(startPosition, toyBot.getPosition());
|
||||
moveStrategy.change(toyBot, toyBotField, moveCommand);
|
||||
assertEquals(new Position(1,0), toyBot.getPosition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveToSouthAllowed() {
|
||||
Position startPosition = new Position(0,1);
|
||||
ToyBot toyBot = new ToyBot(Direction.EAST, startPosition);
|
||||
assertEquals(startPosition, toyBot.getPosition());
|
||||
moveStrategy.change(toyBot, toyBotField, moveCommand);
|
||||
assertEquals(new Position(1,1), toyBot.getPosition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveToEastDisallowed() {
|
||||
Position startPosition = new Position(4,1);
|
||||
ToyBot toyBot = new ToyBot(Direction.EAST, startPosition);
|
||||
assertEquals(startPosition, toyBot.getPosition());
|
||||
moveStrategy.change(toyBot, toyBotField, moveCommand);
|
||||
assertEquals(startPosition, toyBot.getPosition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveToSouthDisallowed() {
|
||||
Position startPosition = new Position(3,0);
|
||||
ToyBot toyBot = new ToyBot(Direction.SOUTH, startPosition);
|
||||
assertEquals(startPosition, toyBot.getPosition());
|
||||
moveStrategy.change(toyBot, toyBotField, moveCommand);
|
||||
assertEquals(startPosition, toyBot.getPosition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveToWestDisallowed() {
|
||||
Position startPosition = new Position(0,1);
|
||||
ToyBot toyBot = new ToyBot(Direction.WEST, startPosition);
|
||||
assertEquals(startPosition, toyBot.getPosition());
|
||||
moveStrategy.change(toyBot, toyBotField, moveCommand);
|
||||
assertEquals(startPosition, toyBot.getPosition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoveToNorthDisallowed() {
|
||||
Position startPosition = new Position(0,4);
|
||||
ToyBot toyBot = new ToyBot(Direction.WEST, startPosition);
|
||||
assertEquals(startPosition, toyBot.getPosition());
|
||||
moveStrategy.change(toyBot, toyBotField, moveCommand);
|
||||
assertEquals(startPosition, toyBot.getPosition());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user