Merge remote-tracking branch 'origin/main'

# Conflicts:
#	src/main/java/ch/zhaw/pm2/racetrack/Game.java
#	src/main/java/ch/zhaw/pm2/racetrack/InvalidFileFormatException.java
This commit is contained in:
Leonardo Brandenberger 2022-03-25 21:53:11 +01:00
commit 77922b71b8
9 changed files with 67 additions and 49 deletions

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 120 KiB

After

Width:  |  Height:  |  Size: 113 KiB

View File

@ -1,3 +1,7 @@
(X:28, Y:22)
(X:31, Y:22)
(X:34, Y:22)
(X:37, Y:22)
(X:40, Y:22)
(X:43, Y:22)
(X:46, Y:21)

View File

@ -35,6 +35,3 @@ UP_RIGHT
UP_RIGHT
RIGHT
RIGHT

View File

@ -80,6 +80,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!");
@ -94,9 +98,13 @@ public class Game implements GameSpecification {
}
if (selectedFile != null) {
try {
moveStrategy = new PathFollowerMoveStrategy(selectedFile, track.getCarPos(currentCarIndex));
moveStrategy = new PathFollowerMoveStrategy(selectedFile, track.getCarPos(i));
} 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!");

View File

@ -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

View File

@ -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
@ -73,7 +81,7 @@ public class PathFollowerMoveStrategy implements MoveStrategy {
// increase pointer variable if the next point is reached.
if (pointList.get(pointer).equals(currentPosition)) {
pointer ++;
pointer++;
}
// calculate Vector from current Position to next Point
@ -81,31 +89,29 @@ public class PathFollowerMoveStrategy implements MoveStrategy {
// select acceleration for X
int accelerationX;
if((movementVector.getX() == 0 && currentVelocity.getX() > 0) || //reduce velocity to 0 if the destination coordinate is reached
(movementVector.getX() > 0 && movementVector.getX()/2.0 <= currentVelocity.getX()) || //increase velocity
(movementVector.getX() < 0 && movementVector.getX()/2.0 < currentVelocity.getX())){ //reduce velocity
if ((movementVector.getX() == 0 && currentVelocity.getX() > 0) || //reduce velocity to 0 if the destination coordinate is reached
(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 if((movementVector.getX() == 0 && currentVelocity.getX() < 0) || //reduce velocity to 0 if the destination coordinate is reached
(movementVector.getX() > 0 && movementVector.getX()/2.0 > currentVelocity.getX()) || //increase velocity
(movementVector.getX() < 0 && movementVector.getX()/2.0 >= currentVelocity.getX())) { //reduce velocity
} else if ((movementVector.getX() == 0 && currentVelocity.getX() < 0) || //reduce velocity to 0 if the destination coordinate is reached
(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;
}
// select acceleration for Y
int accelerationY;
if((movementVector.getY() == 0 && currentVelocity.getY() > 0) || //reduce velocity to 0 if the destination coordinate is reached
(movementVector.getY() > 0 && movementVector.getY()/2.0 <= currentVelocity.getY()) || //increase velocity
(movementVector.getY() < 0 && movementVector.getY()/2.0 < currentVelocity.getY())){ //reduce velocity
if ((movementVector.getY() == 0 && currentVelocity.getY() > 0) || //reduce velocity to 0 if the destination coordinate is reached
(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 if((movementVector.getY() == 0 && currentVelocity.getY() < 0) || //reduce velocity to 0 if the destination coordinate is reached
(movementVector.getY() > 0 && movementVector.getY()/2.0 > currentVelocity.getY()) || //increase velocity
(movementVector.getY() < 0 && movementVector.getY()/2.0 >= currentVelocity.getY())) { //reduce velocity
} else if ((movementVector.getY() == 0 && currentVelocity.getY() < 0) || //reduce velocity to 0 if the destination coordinate is reached
(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;
}

View File

@ -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,13 +187,13 @@ 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);
assertEquals(moveStrategy, car.getMoveStrategy());
moveStrategy = new UserMoveStrategy(new UserInterface("Hello"),0,'a');
moveStrategy = new UserMoveStrategy(new UserInterface("Hello"), 0, 'a');
car.setMoveStrategy(moveStrategy);
assertEquals(moveStrategy, car.getMoveStrategy());
}
@ -204,7 +203,7 @@ class CarTest {
*/
@Test
void getWinPoints() {
assertEquals(0,car.getWinPoints());
assertEquals(0, car.getWinPoints());
}
/**
@ -213,7 +212,7 @@ class CarTest {
@Test
void increaseWinPoints() {
car.increaseWinPoints();
assertEquals(1,car.getWinPoints());
assertEquals(1, car.getWinPoints());
}
/**
@ -222,7 +221,7 @@ class CarTest {
@Test
void deductWinPoints() {
car.deductWinPoints();
assertEquals(-1,car.getWinPoints());
assertEquals(-1, car.getWinPoints());
}
}

View File

@ -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();
}
}