Track feature #21

Merged
fassband merged 9 commits from track-feature into main 2022-03-11 18:41:11 +01:00
3 changed files with 53 additions and 13 deletions
Showing only changes of commit befc67f4f0 - Show all commits

View File

@ -48,6 +48,14 @@ public class Car implements CarSpecification {
setPosition(position);
}
public char getId() {
return id;
}
public PositionVector getVelocity() {
return velocity;
}
/**
* Set this Car position directly, regardless of current position and velocity.
* This should only be used by the game controller in rare cases to set the crash or winning position.
@ -58,7 +66,7 @@ public class Car implements CarSpecification {
@Override
public void setPosition(final PositionVector position) {
// TODO: implementation
throw new UnsupportedOperationException();
this.position = position;
}
/**

View File

@ -127,6 +127,8 @@ public class Game implements GameSpecification {
throw new UnsupportedOperationException();
}
/**
* Does indicate if a car would have a crash with a WALL space or another car at the given position.
* @param carIndex The zero-based carIndex number
@ -135,7 +137,8 @@ public class Game implements GameSpecification {
*/
@Override
public boolean willCarCrash(int carIndex, PositionVector position) {
// TODO: implementation
throw new UnsupportedOperationException();
return true;
}
}

View File

@ -73,13 +73,12 @@ public class Track implements TrackSpecification {
* @throws InvalidTrackFormatException if the track file contains invalid data (no tracklines, ...)
*/
public Track(File trackFile) throws FileNotFoundException, InvalidTrackFormatException {
// TODO:
track = new ArrayList<>();
cars = new ArrayList<>();
finishLine = new ArrayList<>();
readFile(trackFile);
findFinish();
addCar();
addCars();
}
/**
@ -95,8 +94,8 @@ public class Track implements TrackSpecification {
}
}
//TODO: MAKE PRIVATE
public void addCar() throws InvalidTrackFormatException {
private void addCars() throws InvalidTrackFormatException {
ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values();
List<Character> allSpaceTypesAsChar = new ArrayList<>();
List<Character> usedSymbolForCar = new ArrayList<>();
@ -159,7 +158,6 @@ public class Track implements TrackSpecification {
}
/**
*
* @return all Cars
*/
public List<Car> getCars() {
@ -167,7 +165,6 @@ public class Track implements TrackSpecification {
}
/**
*
* @return finishLine
*/
public List<PositionVector> getFinishLine() {
@ -181,6 +178,32 @@ public class Track implements TrackSpecification {
return track;
}
/**
* 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) {
PositionVector positionVector = findChar(getCarId(carIndex));
//Removes the Car at Current Pos
String line = track.get(positionVector.getY());
line = line.substring(0,positionVector.getX()) + ConfigSpecification.SpaceType.TRACK.getValue() + line.substring(positionVector.getX()+1);
track.add(positionVector.getY(),line);
//Adds Car at new Position
positionVector = cars.get(carIndex).nextPosition();
line = track.get(positionVector.getY());
line = line.substring(0,positionVector.getX()) + cars.get(carIndex).getId() + line.substring(positionVector.getX()+1);
track.add(positionVector.getY(),line);
//Change position of car
getCar(carIndex).move();
}
public char getRealCharAtPosition(PositionVector positionVector) {
return track.get(positionVector.getY()).charAt(positionVector.getX());
}
/**
* Return the type of space at the given position.
* If the location is outside the track bounds, it is considered a wall.
@ -191,7 +214,6 @@ public class Track implements TrackSpecification {
@Override
public Config.SpaceType getSpaceType(PositionVector position) {
//TODO: RETURN NULL OR CUSTOM ERROR?
char charAtPosition = track.get(position.getY()).charAt(position.getX());
ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values();
@ -246,7 +268,6 @@ public class Track implements TrackSpecification {
@Override
public PositionVector getCarPos(int carIndex) {
return findChar(cars.get(carIndex).getId());
//TODO: SHOULD WE GET CAR POSITION FROM TRACK STRING OR CAR OBJ
}
/**
@ -271,8 +292,16 @@ public class Track implements TrackSpecification {
*/
@Override
public char getCharAtPosition(int y, int x, Config.SpaceType currentSpace) {
// TODO: implementation
throw new UnsupportedOperationException();
char charAtPos = track.get(y).charAt(x);
for (Car car : cars) {
if(charAtPos == car.getId()) {
if(car.isCrashed()) {
return CRASH_INDICATOR;
}
return charAtPos;
}
}
return currentSpace.getValue();
}
/**