- Methoden überarbeitet
This commit is contained in:
parent
007d8ee393
commit
15123b05bb
|
@ -29,6 +29,7 @@ dependencies {
|
|||
|
||||
// beryx uses SLF4J. To remove warning, we add the implementation "no operation"
|
||||
implementation 'org.slf4j:slf4j-nop:2.+'
|
||||
implementation 'junit:junit:4.13.1'
|
||||
|
||||
// Use JUnit Jupiter API for testing.
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
|
||||
|
|
|
@ -63,6 +63,7 @@ public class Track implements TrackSpecification {
|
|||
// TODO: Add necessary variables
|
||||
private List<String> track;
|
||||
private List<Car> cars;
|
||||
private List<PositionVector> finishLine;
|
||||
|
||||
/**
|
||||
* Initialize a Track from the given track file.
|
||||
|
@ -75,10 +76,10 @@ public class Track implements TrackSpecification {
|
|||
// TODO:
|
||||
track = new ArrayList<>();
|
||||
cars = new ArrayList<>();
|
||||
finishLine = new ArrayList<>();
|
||||
readFile(trackFile);
|
||||
|
||||
//TODO: throw error again...
|
||||
//throw new UnsupportedOperationException(); // was allready in !!
|
||||
findFinish();
|
||||
addCar();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -94,43 +95,84 @@ public class Track implements TrackSpecification {
|
|||
}
|
||||
}
|
||||
|
||||
public void addCar() {
|
||||
//TODO: MAKE PRIVATE
|
||||
public void addCar() throws InvalidTrackFormatException {
|
||||
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) {
|
||||
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++) {
|
||||
if (!allSpaceTypesAsChar.contains(line.charAt(i))) {
|
||||
//TODO: CREATE CAR - ADD IT TO CARS - BUT CHECK THAT Symbol is not allready used for another car?
|
||||
char possibleCarChar = line.charAt(i);
|
||||
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) {
|
||||
PositionVector vector = null;
|
||||
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) == symbol) {
|
||||
vector = new PositionVector(j,i);
|
||||
vector = new PositionVector(j, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
|
@ -148,7 +190,8 @@ public class Track implements TrackSpecification {
|
|||
*/
|
||||
@Override
|
||||
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());
|
||||
|
||||
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
|
||||
public int getCarCount() {
|
||||
// TODO: error???
|
||||
return cars.size();
|
||||
//throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -181,9 +222,7 @@ public class Track implements TrackSpecification {
|
|||
*/
|
||||
@Override
|
||||
public Car getCar(int carIndex) {
|
||||
// TODO: error???
|
||||
return cars.get(carIndex);
|
||||
//throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -194,27 +233,20 @@ public class Track implements TrackSpecification {
|
|||
*/
|
||||
@Override
|
||||
public char getCarId(int carIndex) {
|
||||
// TODO: error???
|
||||
return cars.get(carIndex).getID();
|
||||
//throw new UnsupportedOperationException();
|
||||
return cars.get(carIndex).getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the position of the specified car.
|
||||
* Returns Null if carIndex not valid
|
||||
*
|
||||
* @param carIndex The zero-based carIndex number
|
||||
* @return A PositionVector containing the car's current position
|
||||
*/
|
||||
@Override
|
||||
public PositionVector getCarPos(int carIndex) {
|
||||
// TODO: nextPosition or Position of Trackfile?
|
||||
//Alternative 1
|
||||
return cars.get(carIndex).nextPosition();
|
||||
|
||||
//Alternative 2 //NULL aussschliessen falls umgesetzt.
|
||||
return findChar(cars.get(carIndex).getID());
|
||||
|
||||
//throw new UnsupportedOperationException();
|
||||
return findChar(cars.get(carIndex).getId());
|
||||
//TODO: SHOULD WE GET CAR POSITION FROM TRACK STRING OR CAR OBJ
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -225,9 +257,7 @@ public class Track implements TrackSpecification {
|
|||
*/
|
||||
@Override
|
||||
public PositionVector getCarVelocity(int carIndex) {
|
||||
// TODO: error???
|
||||
return cars.get(carIndex).getVelocity();
|
||||
//throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -252,12 +282,10 @@ public class Track implements TrackSpecification {
|
|||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
// TODO: error???
|
||||
String str = "";
|
||||
for (String line : track) {
|
||||
str += line+"\n";
|
||||
str += line + "\n";
|
||||
}
|
||||
return str;
|
||||
//throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,17 @@
|
|||
package ch.zhaw.pm2.racetrack;
|
||||
|
||||
import ch.zhaw.pm2.racetrack.given.ConfigSpecification;
|
||||
import org.junit.Before;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class TrackTest {
|
||||
Track trackObj;
|
||||
|
||||
|
@ -45,7 +48,31 @@ public class TrackTest {
|
|||
//TODO:
|
||||
@Test
|
||||
void addCarAtInit() {
|
||||
try {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue