From 15123b05bb5ae19a02c186a0a1bc470af3ce9054 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Tue, 8 Mar 2022 11:27:05 +0100 Subject: [PATCH] =?UTF-8?q?-=20Methoden=20=C3=BCberarbeitet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + .../java/ch/zhaw/pm2/racetrack/Track.java | 96 ++++++++++++------- .../java/ch/zhaw/pm2/racetrack/TrackTest.java | 29 +++++- 3 files changed, 91 insertions(+), 35 deletions(-) diff --git a/build.gradle b/build.gradle index 37dcb87..669126f 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index 688ee61..1c7dffc 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -63,6 +63,7 @@ public class Track implements TrackSpecification { // TODO: Add necessary variables private List track; private List cars; + private List 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 allSpaceTypesAsChar = new ArrayList<>(); + List allSpaceTypesAsChar = new ArrayList<>(); + List 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 getCars() { + return cars; } + /** + * + * @return finishLine + */ + public List 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(); } } diff --git a/src/test/java/ch/zhaw/pm2/racetrack/TrackTest.java b/src/test/java/ch/zhaw/pm2/racetrack/TrackTest.java index 6821ea9..a4decfb 100644 --- a/src/test/java/ch/zhaw/pm2/racetrack/TrackTest.java +++ b/src/test/java/ch/zhaw/pm2/racetrack/TrackTest.java @@ -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() { - trackObj.addCar(); + try { + trackObj.addCar(); + } catch (InvalidTrackFormatException e) { + e.printStackTrace(); + } } + + @Test + void findFinish() { + List 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(); + } + } }