- TrackTest done Track done

This commit is contained in:
Andrin Fassbind
2022-03-11 16:01:49 +01:00
parent c4ea4d1920
commit 884b349077
4 changed files with 223 additions and 81 deletions
@@ -0,0 +1,9 @@
package ch.zhaw.pm2.racetrack;
public class PositionVectorNotValid extends Throwable {
public PositionVectorNotValid(String message) {
super(message);
}
public PositionVectorNotValid() {}
}
+36 -28
View File
@@ -6,7 +6,6 @@ import ch.zhaw.pm2.racetrack.given.TrackSpecification;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
@@ -59,11 +58,9 @@ import java.util.Scanner;
public class Track implements TrackSpecification {
public static final char CRASH_INDICATOR = 'X';
// TODO: Add necessary variables
private List<String> track;
private List<Car> cars;
private List<PositionVector> finishLine;
private final List<PositionVector> finishLine;
/**
* Initialize a Track from the given track file.
@@ -72,7 +69,7 @@ public class Track implements TrackSpecification {
* @throws FileNotFoundException if the given track file could not be found
* @throws InvalidTrackFormatException if the track file contains invalid data (no tracklines, ...)
*/
public Track(File trackFile) throws FileNotFoundException, InvalidTrackFormatException {
public Track(File trackFile) throws FileNotFoundException, InvalidTrackFormatException, PositionVectorNotValid {
track = new ArrayList<>();
cars = new ArrayList<>();
finishLine = new ArrayList<>();
@@ -159,8 +156,17 @@ public class Track implements TrackSpecification {
private void drawCharOnTrackIndicator(PositionVector positionVector, char symbol) {
String line = track.get(positionVector.getY());
line = line.substring(0,positionVector.getX()) + symbol + line.substring(positionVector.getX()+1);
track.add(positionVector.getY(),line);
line = line.substring(0, positionVector.getX()) + symbol + line.substring(positionVector.getX() + 1);
track.remove(positionVector.getY());
track.add(positionVector.getY(), line);
}
private void isPositionVectorOnTrack(PositionVector positionVector) throws PositionVectorNotValid {
try{
track.get(positionVector.getY()).charAt(positionVector.getX());
}catch (IndexOutOfBoundsException e) {
throw new PositionVectorNotValid();
}
}
/**
@@ -187,6 +193,7 @@ public class Track implements TrackSpecification {
/**
* This Method will update the Car on the track
* and will make the Car move to the next position
*
* @param carIndex representing the current Car
*/
public void moveCar(int carIndex) {
@@ -197,38 +204,43 @@ public class Track implements TrackSpecification {
/**
* This class does change the Position of the car only in the track.
* @param carIndex
*
* @param carIndex of the current car
*/
private void makeCarMoveInTrack(int carIndex) {
PositionVector positionVector = findChar(getCarId(carIndex));
//Removes the Car at Current Pos
drawCharOnTrackIndicator(positionVector,ConfigSpecification.SpaceType.TRACK.getValue());
drawCharOnTrackIndicator(positionVector, ConfigSpecification.SpaceType.TRACK.getValue());
//Adds Car at new Position
positionVector = cars.get(carIndex).nextPosition();
drawCharOnTrackIndicator(positionVector,cars.get(carIndex).getID());
drawCharOnTrackIndicator(positionVector, cars.get(carIndex).getID());
}
/**
* This Method will check if the Car could crash at the specific position
*
* @param positionVector the position to check if the car could crash
* @return true if car would crash. Else false.
*/
public boolean willCrashAtPosition(PositionVector positionVector) {
public boolean willCrashAtPosition(int carIndex, PositionVector positionVector) throws PositionVectorNotValid {
isPositionVectorOnTrack(positionVector);
char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX());
return charAtPosition != ConfigSpecification.SpaceType.TRACK.value;
if (getCarId(carIndex) == charAtPosition) return false;
return (charAtPosition == ConfigSpecification.SpaceType.WALL.value);
}
/**
* 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
*/
public void carDoesCrash(int carIndex,PositionVector positionVector) {
public void carDoesCrash(int carIndex, PositionVector positionVector) throws PositionVectorNotValid{
isPositionVectorOnTrack(positionVector);
Car car = cars.get(carIndex);
car.crash();
makeCarMoveInTrack(carIndex);
drawCharOnTrackIndicator(new PositionVector(positionVector.getX()+1,positionVector.getY()),CRASH_INDICATOR);
car.setPosition(positionVector);
drawCharOnTrackIndicator(new PositionVector(positionVector.getX(), positionVector.getY()), CRASH_INDICATOR);
}
/**
@@ -240,9 +252,8 @@ public class Track implements TrackSpecification {
*/
@Override
public Config.SpaceType getSpaceType(PositionVector position) {
//isPositionVectorOnTrack(position); Should be used but we are not allowed to change method head. We don't use function anyway
char charAtPosition = track.get(position.getY()).charAt(position.getX());
ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values();
for (ConfigSpecification.SpaceType spaceType : spaceTypes) {
if (spaceType.getValue() == charAtPosition) {
@@ -320,14 +331,13 @@ public class Track implements TrackSpecification {
@Override
public char getCharAtPosition(int y, int x, Config.SpaceType currentSpace) {
char charAtPos = track.get(y).charAt(x);
PositionVector positionVector = new PositionVector(x, y);
for (Car car : cars) {
if(charAtPos == car.getID()) {
if(car.isCrashed()) {
return CRASH_INDICATOR;
}
if (charAtPos == car.getID()) {
return charAtPos;
}
}
if (positionVector.equals(findChar(CRASH_INDICATOR))) return CRASH_INDICATOR;
return currentSpace.getValue();
}
@@ -338,10 +348,8 @@ public class Track implements TrackSpecification {
*/
@Override
public String toString() {
String str = "";
for (String line : track) {
str += line + "\n";
}
return str;
StringBuilder str = new StringBuilder();
for (String line : track) str.append(line).append("\n");
return str.toString();
}
}