added javadocs in InvalidFileFormatException, InvalidTrackFormatException, Track, UserInterface and Main. Enhanced Game methods

This commit is contained in:
Leonardo Brandenberger 2022-03-25 22:34:26 +01:00
parent 2f6b2d2e41
commit bb0e52b8bd
7 changed files with 34 additions and 36 deletions

View File

@ -6,6 +6,7 @@ import ch.zhaw.pm2.racetrack.strategy.*;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import static ch.zhaw.pm2.racetrack.PositionVector.Direction; import static ch.zhaw.pm2.racetrack.PositionVector.Direction;
@ -35,9 +36,7 @@ public class Game implements GameSpecification {
public boolean initPhase() { public boolean initPhase() {
if (config.getTrackDirectory().listFiles().length > 0) { if (config.getTrackDirectory().listFiles().length > 0) {
List<String> tracks = new ArrayList<>(); List<String> tracks = new ArrayList<>();
for (String file : config.getTrackDirectory().list()) { tracks.addAll(Arrays.asList(config.getTrackDirectory().list()));
tracks.add(file);
}
File selectedTrack = config.getTrackDirectory().listFiles()[userInterface.selectOption("Select Track file", tracks)]; File selectedTrack = config.getTrackDirectory().listFiles()[userInterface.selectOption("Select Track file", tracks)];
try { try {
@ -62,22 +61,11 @@ public class Game implements GameSpecification {
int moveStrategie = userInterface.selectOption( int moveStrategie = userInterface.selectOption(
"Select Strategy for Car " + carIndex + " (" + track.getCarId(carIndex) + ")", moveStrategies); "Select Strategy for Car " + carIndex + " (" + track.getCarId(carIndex) + ")", moveStrategies);
switch (moveStrategie) { switch (moveStrategie) {
case 0: case 0 -> moveStrategy = new DoNotMoveStrategy();
moveStrategy = new DoNotMoveStrategy(); case 1 -> moveStrategy = new UserMoveStrategy(userInterface, carIndex, track.getCarId(carIndex));
break; case 2 -> moveStrategy = getMoveListStrategy(selectedTrack, carIndex);
case 1: case 3 -> moveStrategy = getPathFollowerMoveStrategy(selectedTrack, carIndex);
moveStrategy = new UserMoveStrategy(userInterface, carIndex, track.getCarId(carIndex)); case 4 -> moveStrategy = new PathFinderMoveStrategy(track, carIndex);
break;
case 2:
moveStrategy = getMoveListStrategy(selectedTrack, carIndex);
break;
case 3:
moveStrategy = getPathFollowerMoveStrategy(selectedTrack, carIndex);
break;
case 4:
moveStrategy = new PathFinderMoveStrategy(track, carIndex);
break;
} }
} }
selectMoveStrategy(car, moveStrategy); selectMoveStrategy(car, moveStrategy);

View File

@ -1,7 +1,7 @@
package ch.zhaw.pm2.racetrack; package ch.zhaw.pm2.racetrack;
/** /**
* Class for Exception when invalid file format is used. * Class for Exception when an invalid file format is used.
*/ */
public class InvalidFileFormatException extends Exception { public class InvalidFileFormatException extends Exception {
public InvalidFileFormatException() { public InvalidFileFormatException() {

View File

@ -4,11 +4,17 @@ package ch.zhaw.pm2.racetrack;
* Class for Exception when invalid track format is used. * Class for Exception when invalid track format is used.
*/ */
public class InvalidTrackFormatException extends Exception { public class InvalidTrackFormatException extends Exception {
public InvalidTrackFormatException() {
super();
}
/**
* Constructor that is used when an error message is given with the exception.
*
* @param errorMessage is the message to be displayed
*/
public InvalidTrackFormatException(String errorMessage) { public InvalidTrackFormatException(String errorMessage) {
super(errorMessage); super(errorMessage);
} }
public InvalidTrackFormatException() {
super();
}
} }

View File

@ -3,8 +3,19 @@ package ch.zhaw.pm2.racetrack;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/**
* Class containing the main method
*
* @author Roman Schenk
*/
public class Main { public class Main {
/**
* Method creating the game and initializing the Configs and Userinterface.
* Starts and initializes a new game until the user decides to stop the game.
* In charge of closing the user interface when user decides to stop the game
*
* @param args no arguments needed
*/
public static void main(String[] args) { public static void main(String[] args) {
UserInterface userInterface = new UserInterface("Hello and Welcome to Racetrack by Team02-\"AngryNerds\""); UserInterface userInterface = new UserInterface("Hello and Welcome to Racetrack by Team02-\"AngryNerds\"");
Config config = new Config(); Config config = new Config();

View File

@ -7,8 +7,8 @@ package ch.zhaw.pm2.racetrack;
* Created by mach 21.01.2020 * Created by mach 21.01.2020
*/ */
public final class PositionVector { public final class PositionVector {
private final int x; // horizontal component (position / velocity) private int x; // horizontal component (position / velocity)
private final int y; // vertical component (position / velocity) private int y; // vertical component (position / velocity)
/** /**
* Enum representing a direction on the track grid. * Enum representing a direction on the track grid.
@ -33,7 +33,6 @@ public final class PositionVector {
/** /**
* Adds two PositionVectors (e.g. car position and velocity vector or two velocity vectors). * Adds two PositionVectors (e.g. car position and velocity vector or two velocity vectors).
*
* @param vectorA A position or velocity vector * @param vectorA A position or velocity vector
* @param vectorB A position or velocity vector * @param vectorB A position or velocity vector
* @return A new PositionVector holding the result of the addition. If both * @return A new PositionVector holding the result of the addition. If both
@ -46,7 +45,6 @@ public final class PositionVector {
/** /**
* Subtracts two PositionVectors (e.g. car position and velocity vector or two velocity vectors). * Subtracts two PositionVectors (e.g. car position and velocity vector or two velocity vectors).
*
* @param vectorA A position or velocity vector * @param vectorA A position or velocity vector
* @param vectorB A position or velocity vector * @param vectorB A position or velocity vector
* @return A new PositionVector holding the result of the addition. If both * @return A new PositionVector holding the result of the addition. If both
@ -60,7 +58,6 @@ public final class PositionVector {
/** /**
* Calculates the scalar product (Skalarprodukt) of two 2D vectors. The scalar product * Calculates the scalar product (Skalarprodukt) of two 2D vectors. The scalar product
* multiplies the lengths of the parallel components of the vectors. * multiplies the lengths of the parallel components of the vectors.
*
* @param vectorA A position or velocity vector * @param vectorA A position or velocity vector
* @param vectorB A position or velocity vector * @param vectorB A position or velocity vector
* @return The scalar product (vectorA * vectorB). Since vectorA and * @return The scalar product (vectorA * vectorB). Since vectorA and
@ -78,7 +75,6 @@ public final class PositionVector {
/** /**
* Copy constructor * Copy constructor
*
* @param other * @param other
*/ */
public PositionVector(final PositionVector other) { public PositionVector(final PositionVector other) {

View File

@ -45,7 +45,7 @@ import java.util.Scanner;
* There are 1 to {@link Config#MAX_CARS} allowed. </li> * There are 1 to {@link Config#MAX_CARS} allowed. </li>
* </ul> * </ul>
* *
* <p>All lines must have the same length, used to initialize the grid width). * <p>All lines must have the same length, used to initialize the grid width.
* Beginning empty lines are skipped. * Beginning empty lines are skipped.
* The tracks ends with the first empty line or the file end.<br> * The tracks ends with the first empty line or the file end.<br>
* An {@link InvalidTrackFormatException} is thrown, if * An {@link InvalidTrackFormatException} is thrown, if
@ -122,7 +122,6 @@ public class Track implements TrackSpecification {
} }
} }
} }
} }
/** /**
@ -473,8 +472,6 @@ public class Track implements TrackSpecification {
} }
break; break;
} }
} }
return 0; return 0;
} }