Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dad57ea8ac | ||
|
|
e6fc0fde39 |
@@ -1,7 +1,10 @@
|
|||||||
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.*;
|
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 java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@@ -47,30 +50,21 @@ 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) {
|
case 1:
|
||||||
case 1:
|
track.getCar(i).setMoveStrategy(new DoNotMoveStrategy());
|
||||||
track.getCar(i).setMoveStrategy(new DoNotMoveStrategy());
|
break;
|
||||||
break;
|
case 2:
|
||||||
case 2:
|
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";
|
break;
|
||||||
try {
|
case 4:
|
||||||
MoveStrategy moveStrategy = new MoveListStrategy(path);
|
track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments
|
||||||
track.getCar(i).setMoveStrategy(moveStrategy);
|
break;
|
||||||
} 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;
|
||||||
@@ -115,6 +109,7 @@ 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
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ public class Track implements TrackSpecification {
|
|||||||
private List<String> track;
|
private List<String> track;
|
||||||
private List<Car> cars;
|
private List<Car> cars;
|
||||||
private final List<PositionVector> finishLine;
|
private final List<PositionVector> finishLine;
|
||||||
|
private ConfigSpecification.SpaceType finishTyp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize a Track from the given track file.
|
* Initialize a Track from the given track file.
|
||||||
@@ -132,7 +133,7 @@ public class Track implements TrackSpecification {
|
|||||||
if (finishLine.size() == 0) {
|
if (finishLine.size() == 0) {
|
||||||
throw new InvalidTrackFormatException();
|
throw new InvalidTrackFormatException();
|
||||||
}
|
}
|
||||||
ConfigSpecification.SpaceType finishTyp = getSpaceType(finishLine.get(0));
|
finishTyp = getSpaceType(finishLine.get(0));
|
||||||
for (PositionVector positionVector : finishLine) {
|
for (PositionVector positionVector : finishLine) {
|
||||||
if (getSpaceType(positionVector) != finishTyp) {
|
if (getSpaceType(positionVector) != finishTyp) {
|
||||||
throw new InvalidTrackFormatException();
|
throw new InvalidTrackFormatException();
|
||||||
@@ -207,12 +208,20 @@ public class Track implements TrackSpecification {
|
|||||||
* @param carIndex of the current car
|
* @param carIndex of the current car
|
||||||
*/
|
*/
|
||||||
private void makeCarMoveInTrack(int carIndex) {
|
private void makeCarMoveInTrack(int carIndex) {
|
||||||
PositionVector positionVector = findChar(getCarId(carIndex));
|
PositionVector carPositionVector = findChar(getCarId(carIndex));
|
||||||
//Removes the Car at Current Pos
|
//Removes the Car at Current Pos
|
||||||
drawCharOnTrackIndicator(positionVector, ConfigSpecification.SpaceType.TRACK.getValue());
|
drawCharOnTrackIndicator(carPositionVector, ConfigSpecification.SpaceType.TRACK.getValue());
|
||||||
|
|
||||||
|
//Redraw finishline if Car was on finish-line Position
|
||||||
|
for(PositionVector finishLinePositionVector : finishLine){
|
||||||
|
if(finishLinePositionVector.equals(carPositionVector)){
|
||||||
|
drawCharOnTrackIndicator(carPositionVector, finishTyp.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Adds Car at new Position
|
//Adds Car at new Position
|
||||||
positionVector = cars.get(carIndex).nextPosition();
|
carPositionVector = cars.get(carIndex).nextPosition();
|
||||||
drawCharOnTrackIndicator(positionVector, cars.get(carIndex).getID());
|
drawCharOnTrackIndicator(carPositionVector, cars.get(carIndex).getID());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -222,7 +231,7 @@ public class Track implements TrackSpecification {
|
|||||||
* @return true if car would crash. Else false.
|
* @return true if car would crash. Else false.
|
||||||
*/
|
*/
|
||||||
public boolean willCrashAtPosition(int carIndex, PositionVector positionVector) throws PositionVectorNotValid {
|
public boolean willCrashAtPosition(int carIndex, PositionVector positionVector) throws PositionVectorNotValid {
|
||||||
isPositionVectorOnTrack(positionVector);
|
isPositionVectorOnTrack(positionVector); //TODO: remove this line? Or Method?
|
||||||
char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX());
|
char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX());
|
||||||
if (getCarId(carIndex) == charAtPosition) return false;
|
if (getCarId(carIndex) == charAtPosition) return false;
|
||||||
return !(charAtPosition == ConfigSpecification.SpaceType.TRACK.value ||
|
return !(charAtPosition == ConfigSpecification.SpaceType.TRACK.value ||
|
||||||
@@ -236,14 +245,16 @@ public class Track implements TrackSpecification {
|
|||||||
* This Method will make the Car Crash. In Track and in the Car Object
|
* This Method will make the Car Crash. In Track and in the Car Object
|
||||||
*
|
*
|
||||||
* @param carIndex representing current Car
|
* @param carIndex representing current Car
|
||||||
* @param positionVector where the Crash did happen
|
* @param crashPositionVector where the Crash did happen
|
||||||
*/
|
*/
|
||||||
public void carDoesCrash(int carIndex, PositionVector positionVector) throws PositionVectorNotValid{
|
public void carDoesCrash(int carIndex, PositionVector crashPositionVector) throws PositionVectorNotValid{
|
||||||
isPositionVectorOnTrack(positionVector);
|
isPositionVectorOnTrack(crashPositionVector); //TODO: remove this line? and Method?
|
||||||
|
PositionVector currentCarPosition = getCarPos(carIndex);
|
||||||
|
drawCharOnTrackIndicator(new PositionVector(currentCarPosition.getX(), currentCarPosition.getY()), ConfigSpecification.SpaceType.TRACK.getValue());
|
||||||
Car car = cars.get(carIndex);
|
Car car = cars.get(carIndex);
|
||||||
car.crash();
|
car.crash();
|
||||||
car.setPosition(positionVector);
|
car.setPosition(crashPositionVector);
|
||||||
drawCharOnTrackIndicator(new PositionVector(positionVector.getX(), positionVector.getY()), CRASH_INDICATOR);
|
drawCharOnTrackIndicator(new PositionVector(crashPositionVector.getX(), crashPositionVector.getY()), CRASH_INDICATOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -2,44 +2,11 @@ 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() {
|
||||||
pointer += 1;
|
// TODO: implementation
|
||||||
if (pointer < moveList.size()) {
|
throw new UnsupportedOperationException();
|
||||||
return moveList.get(pointer);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,40 +0,0 @@
|
|||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user