From befc67f4f02363ef3e260d02a46188717f49c9f9 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Thu, 10 Mar 2022 14:35:33 +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 --- src/main/java/ch/zhaw/pm2/racetrack/Car.java | 10 +++- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 7 ++- .../java/ch/zhaw/pm2/racetrack/Track.java | 49 +++++++++++++++---- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Car.java b/src/main/java/ch/zhaw/pm2/racetrack/Car.java index 9b97c3d..dfcce64 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Car.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Car.java @@ -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; } /** diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 27609ca..b0204e9 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -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; } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index 1c7dffc..5cc9993 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -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 allSpaceTypesAsChar = new ArrayList<>(); List usedSymbolForCar = new ArrayList<>(); @@ -159,7 +158,6 @@ public class Track implements TrackSpecification { } /** - * * @return all Cars */ public List getCars() { @@ -167,7 +165,6 @@ public class Track implements TrackSpecification { } /** - * * @return finishLine */ public List 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(); } /**