code cleanup
This commit is contained in:
parent
e2c6b5cb49
commit
65d7ea6e7e
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 120 KiB After Width: | Height: | Size: 113 KiB |
|
@ -35,6 +35,3 @@ UP_RIGHT
|
|||
UP_RIGHT
|
||||
RIGHT
|
||||
RIGHT
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ public class Game implements GameSpecification {
|
|||
|
||||
/**
|
||||
* This method will initialize the game. Therefore it interacts with the user via UserInterface
|
||||
*
|
||||
* @return true if the initialization is completed. Returns false if there is an error.
|
||||
*/
|
||||
public boolean initPhase() {
|
||||
|
@ -80,6 +81,10 @@ public class Game implements GameSpecification {
|
|||
moveStrategy = new MoveListStrategy(selectedFile);
|
||||
} catch (FileNotFoundException e) {
|
||||
userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!");
|
||||
e.printStackTrace();
|
||||
} catch (InvalidFileFormatException e) {
|
||||
userInterface.printInformation("Invalid Data in Move-List. Choose another Strategy and clean the File");
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!");
|
||||
|
@ -96,7 +101,11 @@ public class Game implements GameSpecification {
|
|||
try {
|
||||
moveStrategy = new PathFollowerMoveStrategy(selectedFile, track.getCarPos(currentCarIndex));
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!");
|
||||
} catch (InvalidFileFormatException e) {
|
||||
e.printStackTrace();
|
||||
userInterface.printInformation("Invalid Point-List format. Change Strategy and clean Point-List");
|
||||
}
|
||||
} else {
|
||||
userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!");
|
||||
|
@ -119,6 +128,7 @@ public class Game implements GameSpecification {
|
|||
|
||||
/**
|
||||
* The functionality was taken out of init to automate testing
|
||||
*
|
||||
* @param selectedTrack the Track which was selected by user
|
||||
*/
|
||||
Track selectTrack(File selectedTrack) throws InvalidTrackFormatException, FileNotFoundException {
|
||||
|
@ -322,6 +332,7 @@ public class Game implements GameSpecification {
|
|||
* This method will check if a car is passing the finishline.
|
||||
* If the car is passing the finishline in the wrong direction, the car will lose a winpoint.
|
||||
* If the car is passing the finishline in the correct direction, the car will gain a winpoint.
|
||||
*
|
||||
* @param start the startposition of the car
|
||||
* @param finish the expected finishpositon of the car after the move
|
||||
*/
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package ch.zhaw.pm2.racetrack.strategy;
|
||||
|
||||
import ch.zhaw.pm2.racetrack.InvalidFileFormatException;
|
||||
import ch.zhaw.pm2.racetrack.PositionVector.Direction;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -13,14 +14,13 @@ import java.util.Scanner;
|
|||
/**
|
||||
* This Class represent the MoveListStrategy. The Directions returned by the
|
||||
* nextMove() are written down in a List.
|
||||
*
|
||||
*/
|
||||
public class MoveListStrategy implements MoveStrategy {
|
||||
|
||||
private final List<Direction> moveList;
|
||||
private int pointer;
|
||||
|
||||
public MoveListStrategy(File moveListFile) throws FileNotFoundException{
|
||||
public MoveListStrategy(File moveListFile) throws FileNotFoundException, InvalidFileFormatException {
|
||||
moveList = new ArrayList<>();
|
||||
pointer = -1;
|
||||
readFile(moveListFile);
|
||||
|
@ -29,25 +29,32 @@ public class MoveListStrategy implements MoveStrategy {
|
|||
/**
|
||||
* This Method will read in a File and checks line by line if the file contains a valid direction.
|
||||
* If so the direction will be added to the moveList.
|
||||
*
|
||||
* @param trackFile the file to read in the directions
|
||||
* @throws FileNotFoundException if the file does not exist.
|
||||
*/
|
||||
private void readFile(File trackFile) throws FileNotFoundException {
|
||||
private void readFile(File trackFile) throws FileNotFoundException, InvalidFileFormatException {
|
||||
Scanner scanner = new Scanner(new FileInputStream(trackFile), StandardCharsets.UTF_8);
|
||||
Direction[] directions = Direction.values();
|
||||
while (scanner.hasNextLine()) {
|
||||
boolean validLine = false;
|
||||
String line = scanner.nextLine();
|
||||
for (Direction direction : directions) {
|
||||
if (direction.toString().equals(line)) {
|
||||
moveList.add(direction);
|
||||
validLine = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!(validLine || line == "")) {
|
||||
throw new InvalidFileFormatException("The File contains invalid data! Please ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will be used to return the next Direction for the car.
|
||||
*
|
||||
* @return the next direction from the list. Returns null if the list is empty.
|
||||
*/
|
||||
@Override
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package ch.zhaw.pm2.racetrack.strategy;
|
||||
|
||||
import ch.zhaw.pm2.racetrack.InvalidFileFormatException;
|
||||
import ch.zhaw.pm2.racetrack.PositionVector;
|
||||
import ch.zhaw.pm2.racetrack.PositionVector.Direction;
|
||||
|
||||
|
@ -34,11 +35,12 @@ public class PathFollowerMoveStrategy implements MoveStrategy {
|
|||
|
||||
/**
|
||||
* Constructor to create a new PathFollowerMoveStrategy for a car.
|
||||
*
|
||||
* @param trackFile The location where the file is saved
|
||||
* @param startPosition The start position of the car
|
||||
* @throws FileNotFoundException If the file with the given path does not exist.
|
||||
*/
|
||||
public PathFollowerMoveStrategy(File trackFile, PositionVector startPosition) throws FileNotFoundException {
|
||||
public PathFollowerMoveStrategy(File trackFile, PositionVector startPosition) throws FileNotFoundException, InvalidFileFormatException {
|
||||
pointList = new ArrayList<>();
|
||||
pointer = 0;
|
||||
readFile(trackFile);
|
||||
|
@ -48,20 +50,26 @@ public class PathFollowerMoveStrategy implements MoveStrategy {
|
|||
|
||||
/**
|
||||
* Method to read the given File and add the points to the pointList
|
||||
*
|
||||
* @param trackFile the File Object which should be read
|
||||
* @throws FileNotFoundException If the file with the given path does not exist.
|
||||
*/
|
||||
public void readFile(File trackFile) throws FileNotFoundException {
|
||||
public void readFile(File trackFile) throws FileNotFoundException, InvalidFileFormatException {
|
||||
Scanner scanner = new Scanner(new FileInputStream(trackFile), StandardCharsets.UTF_8);
|
||||
while (scanner.hasNextLine()) {
|
||||
String line = scanner.nextLine();
|
||||
String[] coordinates = line.split("(\\(X:|, Y:|\\))");
|
||||
try {
|
||||
pointList.add(new PositionVector(Integer.parseInt(coordinates[1]), Integer.parseInt(coordinates[2])));
|
||||
} catch (NumberFormatException e) {
|
||||
throw new InvalidFileFormatException("Invalid File Format");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to select the direction for the next move.
|
||||
*
|
||||
* @return The direction for the next move. null if there are no points left in the list.
|
||||
*/
|
||||
@Override
|
||||
|
@ -89,8 +97,7 @@ public class PathFollowerMoveStrategy implements MoveStrategy {
|
|||
(movementVector.getX() > 0 && movementVector.getX() / 2.0 > currentVelocity.getX()) || //increase velocity
|
||||
(movementVector.getX() < 0 && movementVector.getX() / 2.0 >= currentVelocity.getX())) { //reduce velocity
|
||||
accelerationX = 1;
|
||||
}
|
||||
else { //no acceleration
|
||||
} else { //no acceleration
|
||||
accelerationX = 0;
|
||||
}
|
||||
|
||||
|
@ -104,8 +111,7 @@ public class PathFollowerMoveStrategy implements MoveStrategy {
|
|||
(movementVector.getY() > 0 && movementVector.getY() / 2.0 > currentVelocity.getY()) || //increase velocity
|
||||
(movementVector.getY() < 0 && movementVector.getY() / 2.0 >= currentVelocity.getY())) { //reduce velocity
|
||||
accelerationY = 1;
|
||||
}
|
||||
else { //no acceleration
|
||||
} else { //no acceleration
|
||||
accelerationY = 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -108,7 +108,6 @@ class CarTest {
|
|||
checkNextPosition(DEFAULT_X, DEFAULT_Y);
|
||||
|
||||
|
||||
|
||||
for (PositionVector.Direction direction1 : directions) {
|
||||
for (PositionVector.Direction direction2 : directions) {
|
||||
|
||||
|
@ -180,7 +179,7 @@ class CarTest {
|
|||
|
||||
try {
|
||||
moveStrategy = new MoveListStrategy(new File(".\\moves\\challenge-car-a.txt"));
|
||||
} catch (FileNotFoundException e) {
|
||||
} catch (FileNotFoundException | InvalidFileFormatException e) {
|
||||
Assertions.fail();
|
||||
}
|
||||
car.setMoveStrategy(moveStrategy);
|
||||
|
@ -188,7 +187,7 @@ class CarTest {
|
|||
|
||||
try {
|
||||
moveStrategy = new PathFollowerMoveStrategy(new File(".\\follower\\challenge_points.txt"), new PositionVector(0, 0));
|
||||
} catch (FileNotFoundException e) {
|
||||
} catch (FileNotFoundException | InvalidFileFormatException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
car.setMoveStrategy(moveStrategy);
|
||||
|
|
|
@ -20,7 +20,7 @@ public class MoveStrategyTest {
|
|||
void setup() {
|
||||
try {
|
||||
moveList = new MoveListStrategy(new File(".\\moves\\challenge-car-a.txt"));
|
||||
}catch (FileNotFoundException e) {
|
||||
}catch (FileNotFoundException | InvalidFileFormatException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue