Game #23

Merged
schrom01 merged 43 commits from Game into main 2022-03-20 16:56:34 +01:00
3 changed files with 100 additions and 22 deletions
Showing only changes of commit fec3aceaa6 - Show all commits

View File

@ -1,10 +1,7 @@
package ch.zhaw.pm2.racetrack;
import ch.zhaw.pm2.racetrack.given.GameSpecification;
import ch.zhaw.pm2.racetrack.strategy.DoNotMoveStrategy;
import ch.zhaw.pm2.racetrack.strategy.MoveListStrategy;
import ch.zhaw.pm2.racetrack.strategy.PathFollowerMoveStrategy;
import ch.zhaw.pm2.racetrack.strategy.UserMoveStrategy;
import ch.zhaw.pm2.racetrack.strategy.*;
import java.io.File;
import java.io.FileNotFoundException;
@ -50,6 +47,7 @@ public class Game implements GameSpecification {
moveStrategies.add("Move List Strategy");
moveStrategies.add("Path Follow Move Strategy");
for (int i = 0; i < track.getCarCount(); i++) {
while(track.getCar(i).getMoveStrategy() == null) {
int moveStrategie = userInterface.selectOption(
"Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies);
switch (moveStrategie + 1) {
@ -60,13 +58,21 @@ public class Game implements GameSpecification {
track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i)));
break;
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;
case 4:
track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments
break;
}
}
}
return true;
} else {
userInterface.printInformation("No Trackfile found!");
@ -109,7 +115,6 @@ public class Game implements GameSpecification {
/**
* Get the velocity of the specified car.
*
* @param carIndex The zero-based carIndex number
* @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 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 {
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
public Direction nextMove() {
// TODO: implementation
throw new UnsupportedOperationException();
pointer += 1;
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());
}
}
}