From 77652705dcf852eb5ea5527aba365a6a001811b9 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 25 Mar 2022 21:55:19 +0100 Subject: [PATCH 1/2] code cleanup --- .../racetrack/InvalidFileFormatException.java | 5 +- .../InvalidTrackFormatException.java | 1 + .../ch/zhaw/pm2/racetrack/PositionVector.java | 2 +- .../strategy/PathFinderMoveStrategy.java | 73 ++++++++++--------- 4 files changed, 45 insertions(+), 36 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/InvalidFileFormatException.java b/src/main/java/ch/zhaw/pm2/racetrack/InvalidFileFormatException.java index 22b7126..62b672b 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/InvalidFileFormatException.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/InvalidFileFormatException.java @@ -7,5 +7,8 @@ public class InvalidFileFormatException extends Exception { public InvalidFileFormatException(String errorMessage) { super(errorMessage); } - public InvalidFileFormatException(){super();} + + public InvalidFileFormatException() { + super(); + } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/InvalidTrackFormatException.java b/src/main/java/ch/zhaw/pm2/racetrack/InvalidTrackFormatException.java index fa24c25..4d102f9 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/InvalidTrackFormatException.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/InvalidTrackFormatException.java @@ -7,6 +7,7 @@ public class InvalidTrackFormatException extends Exception { public InvalidTrackFormatException(String errorMessage) { super(errorMessage); } + public InvalidTrackFormatException() { super(); } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/PositionVector.java b/src/main/java/ch/zhaw/pm2/racetrack/PositionVector.java index 4f3eafe..c059038 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/PositionVector.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/PositionVector.java @@ -109,6 +109,6 @@ public final class PositionVector { @Override public String toString() { - return "(X:" + this.x + ", Y:" + this.y + ")"; + return "(X:" + this.x + ", Y:" + this.y + ")"; } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java index 1442e76..cc3b6c7 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java @@ -9,7 +9,7 @@ import java.util.List; /** * Strategy which calculates the path automatically */ -public class PathFinderMoveStrategy implements MoveStrategy{ +public class PathFinderMoveStrategy implements MoveStrategy { private final Track track; private final int carIndex; private List moveList; @@ -36,17 +36,17 @@ public class PathFinderMoveStrategy implements MoveStrategy{ /** * Method to create a working moveList */ - private void createMoveList(){ + private void createMoveList() { // if Movelist is recreated next move will be the first move in moveList pointer = 0; calculatedStates = new ArrayList<>(); PossibleMove finishedMove = null; - List possibleMoves= new ArrayList<>(); + List possibleMoves = new ArrayList<>(); // create a PossibleMove object for each direction which doesn't end with a crash. - for(PositionVector.Direction direction : allDirections){ + for (PositionVector.Direction direction : allDirections) { PossibleMove newMove = new PossibleMove(null, direction); - if(! newMove.crashed()){ + if (!newMove.crashed()) { possibleMoves.add(newMove); } @@ -54,22 +54,22 @@ public class PathFinderMoveStrategy implements MoveStrategy{ // while no PossibleMove crosses the finishline // every PossibleMove will be accelerated in each direction to find a Move which finishes. - while(finishedMove == null){ + while (finishedMove == null) { List newMoves = new ArrayList<>(); - for(PossibleMove previousMove : possibleMoves){ - for(PositionVector.Direction direction : allDirections){ + for (PossibleMove previousMove : possibleMoves) { + for (PositionVector.Direction direction : allDirections) { PossibleMove newMove = new PossibleMove(previousMove, direction); State newState = new State(newMove.endPosition, newMove.endVelocity); - //only use the new created Possible Move if it doesn't crash, end State isn't in List of calculatedStates - // and if there is no move found yet which is finished. - if(! (newMove.crashed() || alreadyCalculated(newState) || finishedMove != null)){ - if(newMove.finished()){ - finishedMove = newMove; - } else { - calculatedStates.add(newState); - newMoves.add(newMove); - } + //only use the new created Possible Move if it doesn't crash, end State isn't in List of calculatedStates + // and if there is no move found yet which is finished. + if (!(newMove.crashed() || alreadyCalculated(newState) || finishedMove != null)) { + if (newMove.finished()) { + finishedMove = newMove; + } else { + calculatedStates.add(newState); + newMoves.add(newMove); } + } } } possibleMoves = newMoves; @@ -85,9 +85,9 @@ public class PathFinderMoveStrategy implements MoveStrategy{ * @param state the State which should be checked * @return true if it is in List, false if it isn't in List */ - private boolean alreadyCalculated(State state){ - for(State calculatedState: calculatedStates){ - if(state.equals(calculatedState)){ + private boolean alreadyCalculated(State state) { + for (State calculatedState : calculatedStates) { + if (state.equals(calculatedState)) { return true; } } @@ -97,26 +97,28 @@ public class PathFinderMoveStrategy implements MoveStrategy{ /** * Combination of position and velocity */ - public static class State{ + public static class State { final PositionVector position; final PositionVector velocity; /** * Constructor of State + * * @param position the PositionVector object with coordinates of the Position * @param velocity the PositionVector object with coordinates of the Velocity */ - public State(PositionVector position, PositionVector velocity){ + public State(PositionVector position, PositionVector velocity) { this.position = position; this.velocity = velocity; } /** * Checks if a state has the same Position and the same Velocity + * * @param compareState the State object to compare * @return true if it is equal, false if it is not equal */ - public boolean equals(State compareState){ + public boolean equals(State compareState) { return compareState.position.equals(position) && compareState.velocity.equals(velocity); } } @@ -136,21 +138,21 @@ public class PathFinderMoveStrategy implements MoveStrategy{ /** * Constructor of PossibleMove - * @param previousMove The move which must be executed bevor this move can be executed + * + * @param previousMove The move which must be executed bevor this move can be executed * @param nextDirection The direction of the move */ - public PossibleMove(PossibleMove previousMove, PositionVector.Direction nextDirection){ + public PossibleMove(PossibleMove previousMove, PositionVector.Direction nextDirection) { // Velocity of the car bevor the move is executed PositionVector startVelocity; directions = new ArrayList<>(); // check if there was a previousMove. - if(previousMove != null){ + if (previousMove != null) { directions.addAll(previousMove.directions); //copy the LIst of Directions from the previousMove startPosition = previousMove.endPosition; //use the endPosition from previousMove as startPosition startVelocity = previousMove.endVelocity; //use the endVelocity from previousMove as startVelocity - } - else { //if there was no previousMove + } else { //if there was no previousMove startPosition = track.getCarPos(carIndex); //use the current Position of the car from track as startPosition startVelocity = track.getCar(carIndex).getVelocity(); //use the current Velocity of the car from track as startVelocity } @@ -161,20 +163,22 @@ public class PathFinderMoveStrategy implements MoveStrategy{ /** * check if the finishline is crossed (in correct direction) if this move is executed + * * @return true if finishline will be crossed */ - public boolean finished(){ + public boolean finished() { return track.calculateNewWinPoints(startPosition, endPosition) == 1; } /** * checks if the car will crash or finishline will be crossed in wrong direction if this move is executed + * * @return true if car will crash */ public boolean crashed() { List points = track.calculatePointsOnPath(startPosition, endPosition); - for(PositionVector point : points) { - if (track.willCrashAtPosition(carIndex, point)){ + for (PositionVector point : points) { + if (track.willCrashAtPosition(carIndex, point)) { return true; } } @@ -187,6 +191,7 @@ public class PathFinderMoveStrategy implements MoveStrategy{ /** * Checks if the next Move in moveList will crash. If no crash next move in moveList will be executed. * If crash the moveList will be recreated. + * * @return the direction of acceleration which should be executed. */ @Override @@ -197,11 +202,11 @@ public class PathFinderMoveStrategy implements MoveStrategy{ PositionVector newVelocity = new PositionVector(currentVelocity.getX() + direction.vector.getX(), currentVelocity.getY() + direction.vector.getY()); PositionVector currentPosition = track.getCarPos(carIndex); PositionVector newPosition = new PositionVector(currentPosition.getX() + newVelocity.getX(), currentPosition.getY() + newVelocity.getY()); - for(PositionVector point : track.calculatePointsOnPath(currentPosition, newPosition)){ - if(track.willCrashAtPosition(carIndex, point)){ + for (PositionVector point : track.calculatePointsOnPath(currentPosition, newPosition)) { + if (track.willCrashAtPosition(carIndex, point)) { createMoveList(); pointer = 0; - direction = moveList.get(pointer); + direction = moveList.get(pointer); break; } } From d08ebc018770ab4f1429ebd872125a1027805876 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 25 Mar 2022 21:58:23 +0100 Subject: [PATCH 2/2] code cleanup --- src/main/java/ch/zhaw/pm2/racetrack/PositionVector.java | 4 ++++ .../ch/zhaw/pm2/racetrack/strategy/DoNotMoveStrategy.java | 1 + .../zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java | 2 ++ 3 files changed, 7 insertions(+) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/PositionVector.java b/src/main/java/ch/zhaw/pm2/racetrack/PositionVector.java index c059038..a33302e 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/PositionVector.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/PositionVector.java @@ -33,6 +33,7 @@ public final class PositionVector { /** * Adds two PositionVectors (e.g. car position and velocity vector or two velocity vectors). + * * @param vectorA A position or velocity vector * @param vectorB A position or velocity vector * @return A new PositionVector holding the result of the addition. If both @@ -45,6 +46,7 @@ public final class PositionVector { /** * Subtracts two PositionVectors (e.g. car position and velocity vector or two velocity vectors). + * * @param vectorA A position or velocity vector * @param vectorB A position or velocity vector * @return A new PositionVector holding the result of the addition. If both @@ -58,6 +60,7 @@ public final class PositionVector { /** * Calculates the scalar product (Skalarprodukt) of two 2D vectors. The scalar product * multiplies the lengths of the parallel components of the vectors. + * * @param vectorA A position or velocity vector * @param vectorB A position or velocity vector * @return The scalar product (vectorA * vectorB). Since vectorA and @@ -75,6 +78,7 @@ public final class PositionVector { /** * Copy constructor + * * @param other */ public PositionVector(final PositionVector other) { diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/DoNotMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/DoNotMoveStrategy.java index ecfe157..ae9a050 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/DoNotMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/DoNotMoveStrategy.java @@ -11,6 +11,7 @@ public class DoNotMoveStrategy implements MoveStrategy { /** * This method will be used to return the next Direction for the car. + * * @return a NONE Direction */ @Override diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java index cc3b6c7..c870b1f 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java @@ -23,6 +23,7 @@ public class PathFinderMoveStrategy implements MoveStrategy { /** * Constructor which initialises the needed variables and creates the MoveList + * * @param track track instance of the game * @param carIndex index of the car which owns this moveStrategy */ @@ -82,6 +83,7 @@ public class PathFinderMoveStrategy implements MoveStrategy { /** * Method to check if a State is already in List calculatedStates + * * @param state the State which should be checked * @return true if it is in List, false if it isn't in List */