Move list feature #26

Merged
fassband merged 4 commits from MoveListFeature into Game 2022-03-18 20:54:27 +01:00
3 changed files with 100 additions and 22 deletions

View File

@ -1,10 +1,7 @@
package ch.zhaw.pm2.racetrack; package ch.zhaw.pm2.racetrack;
import ch.zhaw.pm2.racetrack.given.GameSpecification; import ch.zhaw.pm2.racetrack.given.GameSpecification;
import ch.zhaw.pm2.racetrack.strategy.DoNotMoveStrategy; import ch.zhaw.pm2.racetrack.strategy.*;
import ch.zhaw.pm2.racetrack.strategy.MoveListStrategy;
import ch.zhaw.pm2.racetrack.strategy.PathFollowerMoveStrategy;
import ch.zhaw.pm2.racetrack.strategy.UserMoveStrategy;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -50,21 +47,30 @@ public class Game implements GameSpecification {
moveStrategies.add("Move List Strategy"); moveStrategies.add("Move List Strategy");
moveStrategies.add("Path Follow Move Strategy"); moveStrategies.add("Path Follow Move Strategy");
for (int i = 0; i < track.getCarCount(); i++) { for (int i = 0; i < track.getCarCount(); i++) {
int moveStrategie = userInterface.selectOption( while(track.getCar(i).getMoveStrategy() == null) {
"Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies); int moveStrategie = userInterface.selectOption(
switch (moveStrategie + 1) { "Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies);
case 1: switch (moveStrategie + 1) {
track.getCar(i).setMoveStrategy(new DoNotMoveStrategy()); case 1:
break; track.getCar(i).setMoveStrategy(new DoNotMoveStrategy());
case 2: break;
track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i))); case 2:
break; track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i)));
case 3: break;
track.getCar(i).setMoveStrategy(new MoveListStrategy()); //TODO: add Arguments case 3:
break; String path = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt";
case 4: try {
track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments MoveStrategy moveStrategy = new MoveListStrategy(path);
break; track.getCar(i).setMoveStrategy(moveStrategy);
} catch (FileNotFoundException e) {
userInterface.printInformation("There is no MoveList implemented. Choose another Strategy!");
}
//TODO: Backslash kompatibel für Linux
break;
case 4:
track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments
break;
}
} }
} }
return true; return true;
@ -109,7 +115,6 @@ public class Game implements GameSpecification {
/** /**
* Get the velocity of the specified car. * Get the velocity of the specified car.
*
* @param carIndex The zero-based carIndex number * @param carIndex The zero-based carIndex number
* @return A PositionVector containing the car's current velocity * @return A PositionVector containing the car's current velocity
*/ */

View File

@ -2,11 +2,44 @@ package ch.zhaw.pm2.racetrack.strategy;
import ch.zhaw.pm2.racetrack.PositionVector.Direction; import ch.zhaw.pm2.racetrack.PositionVector.Direction;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MoveListStrategy implements MoveStrategy { public class MoveListStrategy implements MoveStrategy {
private List<Direction> moveList;
private int pointer;
public MoveListStrategy(String path) throws FileNotFoundException{
moveList = new ArrayList<>();
pointer = -1;
readFile(new File(path));
}
private void readFile(File trackFile) throws FileNotFoundException {
Scanner scanner = new Scanner(new FileInputStream(trackFile), "UTF-8");
Direction[] directions = Direction.values();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
for (Direction dir : directions) {
if (dir.toString().equals(line)) {
moveList.add(dir);
break;
}
}
}
}
@Override @Override
public Direction nextMove() { public Direction nextMove() {
// TODO: implementation pointer += 1;
throw new UnsupportedOperationException(); if (pointer < moveList.size()) {
return moveList.get(pointer);
}
return null;
} }
} }

View File

@ -0,0 +1,40 @@
package ch.zhaw.pm2.racetrack;
import ch.zhaw.pm2.racetrack.strategy.MoveListStrategy;
import ch.zhaw.pm2.racetrack.strategy.MoveStrategy;
import org.junit.jupiter.api.*;
import java.io.FileNotFoundException;
public class MoveStrategyTest {
private MoveStrategy moveList;
@Nested
@DisplayName("MoveListStrategy")
class MoveList {
@BeforeEach
void setup() {
try {
moveList = new MoveListStrategy(".\\moves\\challenge-car-a.txt");
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@Test
void checkMove() {
Assertions.assertEquals(PositionVector.Direction.RIGHT,moveList.nextMove());
for (int i = 0; i < 3; i++) {
moveList.nextMove();
}
Assertions.assertEquals(PositionVector.Direction.NONE,moveList.nextMove());
for (int i = 0; i < 40; i++) {
moveList.nextMove();
}
Assertions.assertNull(moveList.nextMove());
}
}
}