From c4ea4d1920e59a2c06f2e56b8386dabc42ba930b Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Thu, 10 Mar 2022 14:58:54 +0100 Subject: [PATCH] =?UTF-8?q?-=20Crash=20Methoden=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ch/zhaw/pm2/racetrack/Track.java | 45 ++++++++++++++----- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index 2596103..e89b960 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -157,6 +157,12 @@ public class Track implements TrackSpecification { return vector; } + private void drawCharOnTrackIndicator(PositionVector positionVector, char symbol) { + String line = track.get(positionVector.getY()); + line = line.substring(0,positionVector.getX()) + symbol + line.substring(positionVector.getX()+1); + track.add(positionVector.getY(),line); + } + /** * @return all Cars */ @@ -184,22 +190,24 @@ public class Track implements TrackSpecification { * @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); - + makeCarMoveInTrack(carIndex); //Change position of car getCar(carIndex).move(); } + /** + * This class does change the Position of the car only in the track. + * @param carIndex + */ + private void makeCarMoveInTrack(int carIndex) { + PositionVector positionVector = findChar(getCarId(carIndex)); + //Removes the Car at Current Pos + drawCharOnTrackIndicator(positionVector,ConfigSpecification.SpaceType.TRACK.getValue()); + //Adds Car at new Position + positionVector = cars.get(carIndex).nextPosition(); + drawCharOnTrackIndicator(positionVector,cars.get(carIndex).getID()); + } + /** * This Method will check if the Car could crash at the specific position * @param positionVector the position to check if the car could crash @@ -210,6 +218,19 @@ public class Track implements TrackSpecification { return charAtPosition != ConfigSpecification.SpaceType.TRACK.value; } + /** + * This Method will make the Car Crash. In Track and in the Car Object + * @param carIndex representing current Car + * @param positionVector where the Crash did happen + */ + public void carDoesCrash(int carIndex,PositionVector positionVector) { + Car car = cars.get(carIndex); + car.crash(); + makeCarMoveInTrack(carIndex); + drawCharOnTrackIndicator(new PositionVector(positionVector.getX()+1,positionVector.getY()),CRASH_INDICATOR); + + } + /** * Return the type of space at the given position. * If the location is outside the track bounds, it is considered a wall.