Merge remote-tracking branch 'origin/master'
# Conflicts: # README.md # pom.xml # src/main/java/de/dj_steam/bot/cli/InvalidUserInputException.java # src/main/java/de/dj_steam/bot/cli/LoopingConsole.java # src/main/java/de/dj_steam/bot/domain/Command.java # src/main/java/de/dj_steam/bot/domain/Direction.java # src/main/java/de/dj_steam/bot/domain/Position.java # src/main/java/de/dj_steam/bot/domain/ToyBot.java # src/main/java/de/dj_steam/bot/domain/ToyBotField.java # src/main/java/de/dj_steam/bot/moving/DirectionCalculator.java # src/main/java/de/dj_steam/bot/moving/LoopedDirectionsArrayList.java # src/main/java/de/dj_steam/bot/moving/strategy/ChangingStrategy.java # src/main/java/de/dj_steam/bot/moving/strategy/MoveStrategy.java # src/main/java/de/dj_steam/bot/moving/strategy/PlaceStrategy.java # src/main/java/de/dj_steam/bot/moving/strategy/ReportStrategy.java # src/main/java/de/dj_steam/bot/moving/strategy/TurnStrategy.java # src/test/java/de/dj_steam/bot/cli/LoopingConsoleTest.java # src/test/java/de/dj_steam/bot/moving/LoopedDirectionsArrayListTest.java
This commit is contained in:
commit
1db2daad9c
15
scm/.drone.yml
Normal file
15
scm/.drone.yml
Normal file
@ -0,0 +1,15 @@
|
||||
---
|
||||
kind: pipeline
|
||||
name: "toy-bot coding challenge pipeline"
|
||||
steps:
|
||||
-
|
||||
commands:
|
||||
- "mvn clean test"
|
||||
image: "maven:3.8.1-jdk-11"
|
||||
name: test
|
||||
-
|
||||
image: appleboy/drone-telegram
|
||||
name: "send telegram notification"
|
||||
settings:
|
||||
to: 733452136
|
||||
token: "<INVALID_TOKEN>"
|
53
src/main/java/de/dj_steam/bot/engine/ToyBotEngine.java
Normal file
53
src/main/java/de/dj_steam/bot/engine/ToyBotEngine.java
Normal file
@ -0,0 +1,53 @@
|
||||
package de.dj_steam.bot.engine;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author steam
|
||||
*
|
||||
* toyBotEngine implements setup of moving strategies and initial setup of ToyBot and ToyBotField
|
||||
*/
|
||||
public class ToyBotEngine {
|
||||
|
||||
private final ToyBotField toyBotField;
|
||||
private final ToyBot toyBot;
|
||||
private List<ChangingStrategy> changingStrategies;
|
||||
|
||||
public ToyBotEngine() {
|
||||
this.toyBot = new ToyBot(Direction.NORTH, new Position());
|
||||
this.toyBotField = new ToyBotField();
|
||||
initChangingStrategies();
|
||||
}
|
||||
|
||||
ToyBotEngine(final ToyBotField toyBotField, final ToyBot toyBot) {
|
||||
this.toyBotField = toyBotField;
|
||||
this.toyBot = toyBot;
|
||||
initChangingStrategies();
|
||||
}
|
||||
|
||||
public void commandBot(Command command) {
|
||||
for (ChangingStrategy strategy : changingStrategies) {
|
||||
strategy.change(toyBot, toyBotField, command);
|
||||
}
|
||||
}
|
||||
|
||||
private void initChangingStrategies() {
|
||||
changingStrategies = new ArrayList<>();
|
||||
changingStrategies.add(new TurnStrategy());
|
||||
changingStrategies.add(new ReportStrategy());
|
||||
changingStrategies.add(new PlaceStrategy());
|
||||
changingStrategies.add(new MoveStrategy());
|
||||
}
|
||||
}
|
139
src/test/java/de/dj_steam/bot/engine/ToyBotEngineTest.java
Normal file
139
src/test/java/de/dj_steam/bot/engine/ToyBotEngineTest.java
Normal file
@ -0,0 +1,139 @@
|
||||
package de.dj_steam.bot.engine;
|
||||
|
||||
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 ToyBotEngineTest {
|
||||
|
||||
private ToyBotEngine toyBotEngine;
|
||||
private ToyBot toyBot;
|
||||
|
||||
private Command turnLeftCommand;
|
||||
private Command turnRightCommand;
|
||||
private Command moveCommand;
|
||||
private Command placeCommand;
|
||||
|
||||
private final ToyBotField toyBotField = new ToyBotField();
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
toyBot = new ToyBot(Direction.NORTH, new Position());
|
||||
toyBotEngine = new ToyBotEngine(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() {
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
|
||||
toyBotEngine.commandBot(turnLeftCommand);
|
||||
assertEquals(Direction.WEST, toyBot.getDirection());
|
||||
|
||||
toyBotEngine.commandBot(turnLeftCommand);
|
||||
assertEquals(Direction.SOUTH, toyBot.getDirection());
|
||||
|
||||
toyBotEngine.commandBot(turnLeftCommand);
|
||||
assertEquals(Direction.EAST, toyBot.getDirection());
|
||||
|
||||
toyBotEngine.commandBot(turnLeftCommand);
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTurnRightCommand() {
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
|
||||
toyBotEngine.commandBot(turnRightCommand);
|
||||
assertEquals(Direction.EAST, toyBot.getDirection());
|
||||
|
||||
toyBotEngine.commandBot(turnRightCommand);
|
||||
assertEquals(Direction.SOUTH, toyBot.getDirection());
|
||||
|
||||
toyBotEngine.commandBot(turnRightCommand);
|
||||
assertEquals(Direction.WEST, toyBot.getDirection());
|
||||
|
||||
toyBotEngine.commandBot(turnRightCommand);
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlaceCommand() {
|
||||
assertEquals(new Position(0, 0), toyBot.getPosition());
|
||||
|
||||
toyBotEngine.commandBot(placeCommand);
|
||||
assertEquals(new Position(0, 0), toyBot.getPosition());
|
||||
|
||||
placeCommand = new Command(Command.PLACE, Optional.of("0,2,NORTH"));
|
||||
toyBotEngine.commandBot(placeCommand);
|
||||
assertEquals(new Position(0, 2), toyBot.getPosition());
|
||||
|
||||
placeCommand = new Command(Command.PLACE, Optional.of("5,1,NORTH"));
|
||||
toyBotEngine.commandBot(placeCommand);
|
||||
assertEquals(new Position(0, 2), toyBot.getPosition());
|
||||
|
||||
placeCommand = new Command(Command.PLACE, Optional.of("0,3,WEST"));
|
||||
toyBotEngine.commandBot(placeCommand);
|
||||
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++) {
|
||||
toyBotEngine.commandBot(moveCommand);
|
||||
assertEquals(new Position(0, i), toyBot.getPosition());
|
||||
}
|
||||
|
||||
toyBotEngine.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());
|
||||
|
||||
toyBotEngine.commandBot(moveCommand);
|
||||
assertEquals(new Position(0, 1), toyBot.getPosition());
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
|
||||
toyBotEngine.commandBot(turnRightCommand);
|
||||
assertEquals(Direction.EAST, toyBot.getDirection());
|
||||
|
||||
for (int i = 0; i <= 3; i++) {
|
||||
toyBotEngine.commandBot(moveCommand);
|
||||
}
|
||||
|
||||
assertEquals(new Position(4, 1), toyBot.getPosition());
|
||||
|
||||
toyBotEngine.commandBot(moveCommand); // border
|
||||
assertEquals(new Position(4, 1), toyBot.getPosition());
|
||||
|
||||
toyBotEngine.commandBot(turnLeftCommand);
|
||||
for (int i = 0; i <= 3; i++) {
|
||||
toyBotEngine.commandBot(moveCommand);
|
||||
}
|
||||
|
||||
assertEquals(new Position(4, 4), toyBot.getPosition());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user