Merge remote-tracking branch 'origin/track-feature' into Game
# Conflicts: # src/main/java/ch/zhaw/pm2/racetrack/Track.java
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package ch.zhaw.pm2.racetrack;
|
||||
|
||||
public class PositionVectorNotValid extends Throwable {
|
||||
public PositionVectorNotValid(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public PositionVectorNotValid() {}
|
||||
}
|
||||
@@ -59,11 +59,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,8 +70,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<>();
|
||||
@@ -160,8 +157,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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,6 +194,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) {
|
||||
@@ -198,38 +205,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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -241,9 +253,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) {
|
||||
@@ -321,14 +332,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();
|
||||
}
|
||||
|
||||
@@ -339,10 +349,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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user