Merge branch 'main' into Strategy

# Conflicts:
#	src/main/java/ch/zhaw/pm2/racetrack/Game.java
This commit is contained in:
romanschenk37 2022-03-24 17:43:40 +01:00
commit f133f4305d
6 changed files with 47 additions and 78 deletions

File diff suppressed because one or more lines are too long

View File

@ -26,8 +26,11 @@ public class Game implements GameSpecification {
this.userInterface = userInterface;
}
public boolean initPhase() throws InvalidTrackFormatException {
/**
* 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() {
File folder = new File("tracks");
File[] listOfFiles = folder.listFiles();
if (listOfFiles.length > 0) {
@ -35,8 +38,17 @@ public class Game implements GameSpecification {
for (File file : listOfFiles) {
tracks.add(file.getName());
}
File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)];
try {
selectTrack(selectedTrack);
} catch (FileNotFoundException e) {
userInterface.printInformation("There is an unexpected Error with the trackfile Path. Add trackfiles only to tracks path. Exit the Game and Fix the Problem");
return false;
} catch (InvalidTrackFormatException e) {
userInterface.printInformation("There is an unexpected Error with the trackfile. Format does not match specifications! Exit the Game and Fix the Problem");
return false;
}
List<String> moveStrategies = new ArrayList<>();
moveStrategies.add("Do not move Strategy");
moveStrategies.add("User Move Strategy");
@ -64,7 +76,6 @@ public class Game implements GameSpecification {
} catch (FileNotFoundException e) {
userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!");
}
//TODO: Backslash kompatibel für Linux
break;
case 3:
filePath = ".\\follower\\" + selectedTrack.getName().split("\\.")[0] + "_points.txt";
@ -90,17 +101,11 @@ public class Game implements GameSpecification {
/**
* The functionality was taken out of init to automate testing
*
* @param selectedTrack
*/
Track selectTrack(File selectedTrack) {
try {
Track selectTrack(File selectedTrack) throws InvalidTrackFormatException,FileNotFoundException {
track = new Track(selectedTrack);
return track;
} catch (FileNotFoundException | PositionVectorNotValid | InvalidTrackFormatException e) {
e.printStackTrace();
}
return null;
}
/**
@ -202,9 +207,8 @@ public class Game implements GameSpecification {
* for this turn
*/
@Override
public void doCarTurn(Direction acceleration) throws PositionVectorNotValid {
public void doCarTurn(Direction acceleration) {
track.getCar(currentCarIndex).accelerate(acceleration);
PositionVector crashPosition = null;
List<PositionVector> positionList = calculatePath(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition());
for (int i = 0; i < positionList.size(); i++) {
@ -230,7 +234,12 @@ public class Game implements GameSpecification {
}
}
public String gamePhase() throws PositionVectorNotValid {
/**
* This method implements the gameflow in a while loop. If there is a winner. The method will return its carid.
*
* @return the ID of the winning car return null if there is no winner.
*/
public String gamePhase() {
while (carsMoving() && getWinner() == NO_WINNER) {
userInterface.printTrack(track);
Direction direction;
@ -284,7 +293,14 @@ 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
* @param carIndex of the current player.
*/
private int calculateNewWinPoints(PositionVector start, PositionVector finish) {
List<PositionVector> path = calculatePath(start, finish);
for (PositionVector point : path) {
@ -336,7 +352,7 @@ public class Game implements GameSpecification {
* @return A boolean indicator if the car would crash with a WALL or another car.
*/
@Override
public boolean willCarCrash(int carIndex, PositionVector position) throws PositionVectorNotValid {
public boolean willCarCrash(int carIndex, PositionVector position) {
return track.willCrashAtPosition(carIndex, position);
}

View File

@ -1,9 +0,0 @@
package ch.zhaw.pm2.racetrack;
public class PositionVectorNotValidException extends Throwable {
public PositionVectorNotValidException(String message) {
super(message);
}
public PositionVectorNotValidException() {}
}

View File

@ -182,22 +182,6 @@ public class Track implements TrackSpecification {
track.remove(positionVector.getY());
track.add(positionVector.getY(), line);
}
//TODO: check if this method is okay and needed
/**
* Determines if a location is valid PositionVector inside the track
*
* @param positionVector of location that has to be checked
* @throws PositionVectorNotValidException if the PositionVector does not lie on the track.
*/
private void isPositionVectorOnTrack(PositionVector positionVector) throws PositionVectorNotValidException {
try {
track.get(positionVector.getY()).charAt(positionVector.getX());
} catch (IndexOutOfBoundsException e) {
throw new PositionVectorNotValidException();
}
}
/**
* Method that returns the finishline as a List
@ -257,8 +241,7 @@ public class Track implements TrackSpecification {
* @param positionVector the position to check if the car could crash
* @return true if car would crash. Else false.
*/
public boolean willCrashAtPosition(int carIndex, PositionVector positionVector) throws PositionVectorNotValidException {
isPositionVectorOnTrack(positionVector); //TODO: remove this line? Or Method?
public boolean willCrashAtPosition(int carIndex, PositionVector positionVector) {
char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX());
if (getCarId(carIndex) == charAtPosition) return false;
return !(charAtPosition == ConfigSpecification.SpaceType.TRACK.value ||
@ -274,8 +257,7 @@ public class Track implements TrackSpecification {
* @param carIndex representing current Car
* @param crashPositionVector where the Crash did happen
*/
public void carDoesCrash(int carIndex, PositionVector crashPositionVector) throws PositionVectorNotValidException {
isPositionVectorOnTrack(crashPositionVector); //TODO: remove this line? and Method?
public void carDoesCrash(int carIndex, PositionVector crashPositionVector) {
PositionVector currentCarPosition = getCarPos(carIndex);
drawCharOnTrackIndicator(new PositionVector(currentCarPosition.getX(), currentCarPosition.getY()), ConfigSpecification.SpaceType.TRACK.getValue());
Car car = cars.get(carIndex);

View File

@ -1,7 +1,6 @@
package ch.zhaw.pm2.racetrack.given;
import ch.zhaw.pm2.racetrack.PositionVector;
import ch.zhaw.pm2.racetrack.PositionVectorNotValidException;
import java.util.List;
@ -19,11 +18,11 @@ public interface GameSpecification {
int getWinner();
void doCarTurn(PositionVector.Direction acceleration) throws PositionVectorNotValidException;
void doCarTurn(PositionVector.Direction acceleration);
void switchToNextActiveCar();
List<PositionVector> calculatePath(PositionVector startPosition, PositionVector endPosition);
boolean willCarCrash(int carIndex, PositionVector position) throws PositionVectorNotValidException;
boolean willCarCrash(int carIndex, PositionVector position);
}

View File

@ -109,28 +109,19 @@ public class TrackTest {
@Test
@DisplayName("Will Car Crash")
void willCarCrash() {
try {
//Car will Crash
Assertions.assertTrue(trackObj.willCrashAtPosition(0, new PositionVector(25, 21)));
//Car will not Crash and is on track
Assertions.assertFalse(trackObj.willCrashAtPosition(0, new PositionVector(7, 22)));
//Car will not Crash and is on finishLine
Assertions.assertFalse(trackObj.willCrashAtPosition(0, trackObj.getFinishLine().get(0)));
} catch (PositionVectorNotValidException positionVectorNotValidException) {
positionVectorNotValidException.printStackTrace();
Assertions.fail("Test should not throw error");
}
}
@Test
@DisplayName("Make Car Crash")
void makeCarCrash() {
try {
trackObj.carDoesCrash(0, new PositionVector(6, 22));
} catch (PositionVectorNotValidException positionVectorNotValidException) {
positionVectorNotValidException.printStackTrace();
Assertions.fail("Test should not throw exception");
}
Assertions.assertEquals(Track.CRASH_INDICATOR, trackObj.getTrack().get(22).charAt(6));
Assertions.assertTrue(trackObj.getCar(0).isCrashed());
}
@ -140,7 +131,6 @@ public class TrackTest {
@DisplayName("Negative TestCase")
class negativeClass {
File file;
@BeforeEach
void setup() {
file = new File(".\\tracks\\challenge.txt");
@ -165,14 +155,5 @@ public class TrackTest {
File testfile = new File(".\\src\\test\\InvalidTracks\\sameCar.txt");
Assertions.assertThrows(InvalidTrackFormatException.class, () -> new Track(testfile));
}
@Test
@DisplayName("Invalid Position Vector used")
void invalidPositionVector() {
Assertions.assertThrows(PositionVectorNotValidException.class, () -> trackObj.willCrashAtPosition(0, new PositionVector(100, 200)));
Assertions.assertThrows(PositionVectorNotValidException.class, () -> trackObj.carDoesCrash(1,new PositionVector(200,100)));
}
}
}