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 91 additions and 35 deletions
Showing only changes of commit 15123b05bb - Show all commits

View File

@ -29,6 +29,7 @@ dependencies {
// beryx uses SLF4J. To remove warning, we add the implementation "no operation" // beryx uses SLF4J. To remove warning, we add the implementation "no operation"
implementation 'org.slf4j:slf4j-nop:2.+' implementation 'org.slf4j:slf4j-nop:2.+'
implementation 'junit:junit:4.13.1'
// Use JUnit Jupiter API for testing. // Use JUnit Jupiter API for testing.
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'

View File

@ -63,6 +63,7 @@ public class Track implements TrackSpecification {
// TODO: Add necessary variables // TODO: Add necessary variables
private List<String> track; private List<String> track;
private List<Car> cars; private List<Car> cars;
private List<PositionVector> finishLine;
/** /**
* Initialize a Track from the given track file. * Initialize a Track from the given track file.
@ -75,10 +76,10 @@ public class Track implements TrackSpecification {
// TODO: // TODO:
track = new ArrayList<>(); track = new ArrayList<>();
cars = new ArrayList<>(); cars = new ArrayList<>();
finishLine = new ArrayList<>();
readFile(trackFile); readFile(trackFile);
findFinish();
//TODO: throw error again... addCar();
//throw new UnsupportedOperationException(); // was allready in !!
} }
/** /**
@ -94,25 +95,56 @@ public class Track implements TrackSpecification {
} }
} }
public void addCar() { //TODO: MAKE PRIVATE
public void addCar() throws InvalidTrackFormatException {
ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values(); ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values();
ArrayList<Character> allSpaceTypesAsChar = new ArrayList<>(); List<Character> allSpaceTypesAsChar = new ArrayList<>();
List<Character> usedSymbolForCar = new ArrayList<>();
for (ConfigSpecification.SpaceType spaceType : spaceTypes) { for (ConfigSpecification.SpaceType spaceType : spaceTypes) {
allSpaceTypesAsChar.add(spaceType.getValue()); allSpaceTypesAsChar.add(spaceType.getValue());
} }
for (String line : track) { for (int j = 0; j < track.size(); j++) {
String line = track.get(j);
for (int i = 0; i < line.length(); i++) { for (int i = 0; i < line.length(); i++) {
if (!allSpaceTypesAsChar.contains(line.charAt(i))) { char possibleCarChar = line.charAt(i);
//TODO: CREATE CAR - ADD IT TO CARS - BUT CHECK THAT Symbol is not allready used for another car? if (!allSpaceTypesAsChar.contains(possibleCarChar)) {
if (usedSymbolForCar.contains(possibleCarChar)) {
throw new InvalidTrackFormatException();
}
usedSymbolForCar.add(possibleCarChar);
cars.add(new Car(possibleCarChar, new PositionVector(i, j)));
} }
} }
} }
} }
private void findFinish() throws InvalidTrackFormatException {
for (int i = 0; i < track.size(); i++) {
String line = track.get(i);
for (int j = 0; j < line.length(); j++) {
if (line.charAt(j) == ConfigSpecification.SpaceType.FINISH_LEFT.getValue() ||
line.charAt(j) == ConfigSpecification.SpaceType.FINISH_RIGHT.getValue() ||
line.charAt(j) == ConfigSpecification.SpaceType.FINISH_DOWN.getValue() ||
line.charAt(j) == ConfigSpecification.SpaceType.FINISH_UP.getValue()) {
finishLine.add(new PositionVector(j, i));
}
}
}
if (finishLine.size() == 0) {
throw new InvalidTrackFormatException();
}
ConfigSpecification.SpaceType finishTyp = getSpaceType(finishLine.get(0));
for (PositionVector positionVector : finishLine) {
if (getSpaceType(positionVector) != finishTyp) {
throw new InvalidTrackFormatException();
}
}
}
private PositionVector findChar(char symbol) { private PositionVector findChar(char symbol) {
PositionVector vector = null; PositionVector vector = null;
for (int i = 0; i < track.size(); i++) { for (int i = 0; i < track.size(); i++) {
@ -126,11 +158,21 @@ public class Track implements TrackSpecification {
return vector; return vector;
} }
private void findFinish() { /**
//TODO: *
* @return all Cars
*/
public List<Car> getCars() {
return cars;
} }
/**
*
* @return finishLine
*/
public List<PositionVector> getFinishLine() {
return finishLine;
}
/** /**
* @return the track * @return the track
@ -148,7 +190,8 @@ public class Track implements TrackSpecification {
*/ */
@Override @Override
public Config.SpaceType getSpaceType(PositionVector position) { public Config.SpaceType getSpaceType(PositionVector position) {
// TODO: implementation // Done first tests passed but what to do with error
//TODO: RETURN NULL OR CUSTOM ERROR?
char charAtPosition = track.get(position.getY()).charAt(position.getX()); char charAtPosition = track.get(position.getY()).charAt(position.getX());
ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values(); ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values();
@ -158,7 +201,7 @@ public class Track implements TrackSpecification {
} }
} }
throw new UnsupportedOperationException(); return null;
} }
/** /**
@ -168,9 +211,7 @@ public class Track implements TrackSpecification {
*/ */
@Override @Override
public int getCarCount() { public int getCarCount() {
// TODO: error???
return cars.size(); return cars.size();
//throw new UnsupportedOperationException();
} }
/** /**
@ -181,9 +222,7 @@ public class Track implements TrackSpecification {
*/ */
@Override @Override
public Car getCar(int carIndex) { public Car getCar(int carIndex) {
// TODO: error???
return cars.get(carIndex); return cars.get(carIndex);
//throw new UnsupportedOperationException();
} }
/** /**
@ -194,27 +233,20 @@ public class Track implements TrackSpecification {
*/ */
@Override @Override
public char getCarId(int carIndex) { public char getCarId(int carIndex) {
// TODO: error??? return cars.get(carIndex).getId();
return cars.get(carIndex).getID();
//throw new UnsupportedOperationException();
} }
/** /**
* Get the position of the specified car. * Get the position of the specified car.
* Returns Null if carIndex not valid
* *
* @param carIndex The zero-based carIndex number * @param carIndex The zero-based carIndex number
* @return A PositionVector containing the car's current position * @return A PositionVector containing the car's current position
*/ */
@Override @Override
public PositionVector getCarPos(int carIndex) { public PositionVector getCarPos(int carIndex) {
// TODO: nextPosition or Position of Trackfile? return findChar(cars.get(carIndex).getId());
//Alternative 1 //TODO: SHOULD WE GET CAR POSITION FROM TRACK STRING OR CAR OBJ
return cars.get(carIndex).nextPosition();
//Alternative 2 //NULL aussschliessen falls umgesetzt.
return findChar(cars.get(carIndex).getID());
//throw new UnsupportedOperationException();
} }
/** /**
@ -225,9 +257,7 @@ public class Track implements TrackSpecification {
*/ */
@Override @Override
public PositionVector getCarVelocity(int carIndex) { public PositionVector getCarVelocity(int carIndex) {
// TODO: error???
return cars.get(carIndex).getVelocity(); return cars.get(carIndex).getVelocity();
//throw new UnsupportedOperationException();
} }
/** /**
@ -252,12 +282,10 @@ public class Track implements TrackSpecification {
*/ */
@Override @Override
public String toString() { public String toString() {
// TODO: error???
String str = ""; String str = "";
for (String line : track) { for (String line : track) {
str += line + "\n"; str += line + "\n";
} }
return str; return str;
//throw new UnsupportedOperationException();
} }
} }

View File

@ -1,14 +1,17 @@
package ch.zhaw.pm2.racetrack; package ch.zhaw.pm2.racetrack;
import ch.zhaw.pm2.racetrack.given.ConfigSpecification; import ch.zhaw.pm2.racetrack.given.ConfigSpecification;
import org.junit.Before;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class TrackTest { public class TrackTest {
Track trackObj; Track trackObj;
@ -45,7 +48,31 @@ public class TrackTest {
//TODO: //TODO:
@Test @Test
void addCarAtInit() { void addCarAtInit() {
try {
trackObj.addCar(); trackObj.addCar();
} catch (InvalidTrackFormatException e) {
e.printStackTrace();
}
} }
@Test
void findFinish() {
List<PositionVector> expected = new ArrayList<>();
expected.add(new PositionVector(22,22));
expected.add(new PositionVector(22,23));
expected.add(new PositionVector(22,24));
Assertions.assertEquals(expected,trackObj.getFinishLine());
}
@Test
void makeTrackObj() {
try {
Track t1 = new Track(new File("C:\\Studium\\Semester2\\PM2\\Projekt1\\racetrack\\tracks\\challenge.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InvalidTrackFormatException e) {
e.printStackTrace();
}
}
} }