From 571728b6fda3fa13e4ceb4b26c95ea27198e1be6 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Sun, 6 Mar 2022 16:10:18 +0100 Subject: [PATCH] - Method toString done --- .../java/ch/zhaw/pm2/racetrack/Track.java | 44 ++++++++++++++++--- .../java/ch/zhaw/pm2/racetrack/TrackTest.java | 22 ++++------ 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index 68e5625..8e74c93 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -6,6 +6,7 @@ import ch.zhaw.pm2.racetrack.given.TrackSpecification; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Scanner; @@ -61,6 +62,8 @@ public class Track implements TrackSpecification { // TODO: Add necessary variables private List track; + private List cars; + /** * Initialize a Track from the given track file. * @@ -69,7 +72,9 @@ public class Track implements TrackSpecification { * @throws InvalidTrackFormatException if the track file contains invalid data (no tracklines, ...) */ public Track(File trackFile) throws FileNotFoundException, InvalidTrackFormatException { - // TODO: implementation + // TODO: + track = new ArrayList<>(); + cars = new ArrayList<>(); readFile(trackFile); //TODO: throw error again... @@ -78,20 +83,43 @@ public class Track implements TrackSpecification { /** * This method reads the File and saves it to the track ArrayList Line by Line + * * @param trackFile the File where the track has been documented * @throws FileNotFoundException if the FilePath is invalid. */ private void readFile(File trackFile) throws FileNotFoundException { - track = new ArrayList<>(); Scanner scanner = new Scanner(trackFile); while (scanner.hasNextLine()) { track.add(scanner.nextLine()); } } + public void findCar() { + ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values(); + ArrayList allSpaceTypesAsChar = new ArrayList<>(); + + for (ConfigSpecification.SpaceType spaceType : spaceTypes) { + allSpaceTypesAsChar.add(spaceType.getValue()); + } + + + for (String line : track) { + 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? + } + } + } + + } + + private void findFinish() { + //TODO: + + } + /** - * * @return the track */ public List getTrack() { @@ -111,9 +139,8 @@ public class Track implements TrackSpecification { char charAtPosition = track.get(position.getY()).charAt(position.getX()); ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values(); - for (ConfigSpecification.SpaceType spaceType : spaceTypes) { - if(spaceType.getValue() == charAtPosition) { + if (spaceType.getValue() == charAtPosition) { return spaceType; } } @@ -203,6 +230,11 @@ public class Track implements TrackSpecification { @Override public String toString() { // TODO: implementation - throw new UnsupportedOperationException(); + String str = ""; + for (String line : track) { + 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 7034a97..c040580 100644 --- a/src/test/java/ch/zhaw/pm2/racetrack/TrackTest.java +++ b/src/test/java/ch/zhaw/pm2/racetrack/TrackTest.java @@ -7,9 +7,6 @@ import org.junit.jupiter.api.Test; import java.io.File; import java.io.FileNotFoundException; -import java.lang.reflect.Array; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; public class TrackTest { @@ -36,20 +33,19 @@ public class TrackTest { * Dirty test... */ @Test - void compareTrack() { - try { - List track = trackObj.getTrack(); - for (String s: track - ) { - System.out.println(s); - } - } catch (Exception e) { - System.err.println("Error in Test compareTrack" + e.getMessage()); - } + void printOutTrack() { + System.out.println(trackObj); } @Test void getSpaceTyp() { Assertions.assertEquals(ConfigSpecification.SpaceType.FINISH_RIGHT,trackObj.getSpaceType(new PositionVector(22,24))); } + + //TODO: + @Test + void findCarAtInit() { + trackObj.findCar(); + + } }