Move list feature #26
|
@ -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,6 +47,7 @@ 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++) {
|
||||||
|
while(track.getCar(i).getMoveStrategy() == null) {
|
||||||
int moveStrategie = userInterface.selectOption(
|
int moveStrategie = userInterface.selectOption(
|
||||||
"Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies);
|
"Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies);
|
||||||
switch (moveStrategie + 1) {
|
switch (moveStrategie + 1) {
|
||||||
|
@ -60,13 +58,21 @@ public class Game implements GameSpecification {
|
||||||
track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i)));
|
track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i)));
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
track.getCar(i).setMoveStrategy(new MoveListStrategy()); //TODO: add Arguments
|
String path = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt";
|
||||||
|
try {
|
||||||
|
MoveStrategy moveStrategy = new MoveListStrategy(path);
|
||||||
|
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;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments
|
track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
userInterface.printInformation("No Trackfile found!");
|
userInterface.printInformation("No Trackfile found!");
|
||||||
|
@ -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
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue