Merge branch 'main' into Strategy

# Conflicts:
#	src/main/java/ch/zhaw/pm2/racetrack/Game.java
#	src/main/java/ch/zhaw/pm2/racetrack/Track.java
#	src/test/java/ch/zhaw/pm2/racetrack/GameTest.java
This commit is contained in:
romanschenk37 2022-03-25 09:21:12 +01:00
commit 7f9b0bafe4
4 changed files with 64 additions and 14 deletions

View File

@ -293,6 +293,48 @@ 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
*/
private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) {
List<PositionVector> path = calculatePath(start, finish);
for (PositionVector point : path) {
switch (track.getSpaceType(point)) {
case FINISH_UP:
if (start.getY() < finish.getY()) {
track.getCar(carIndex).increaseWinPoints();
} else if (start.getY() > finish.getY()) {
track.getCar(carIndex).deductWinPoints();
}
break;
case FINISH_DOWN:
if (start.getY() > finish.getY()) {
track.getCar(carIndex).increaseWinPoints();
} else if (start.getY() < finish.getY()) {
track.getCar(carIndex).deductWinPoints();
}
break;
case FINISH_RIGHT:
if (start.getX() < finish.getX()) {
track.getCar(carIndex).increaseWinPoints();
} else if (start.getX() > finish.getX()) {
track.getCar(carIndex).deductWinPoints();
}
break;
case FINISH_LEFT:
if (start.getX() > finish.getX()) {
track.getCar(carIndex).increaseWinPoints();
} else if (start.getX() < finish.getX()) {
track.getCar(carIndex).increaseWinPoints();
}
break;
}
}
}
/** /**

View File

@ -66,8 +66,8 @@ public class Track implements TrackSpecification {
private ConfigSpecification.SpaceType finishTyp; private ConfigSpecification.SpaceType finishTyp;
/** /**
* Initialize a Track from the given track file. * Initializes the Track from the given track File including the cars.
* * Throws an corresponding error if one of the conditions are not met to build a track.
* @param trackFile Reference to a file containing the track data * @param trackFile Reference to a file containing the track data
* @throws FileNotFoundException if the given track file could not be found * @throws FileNotFoundException if the given track file could not be found
* @throws InvalidTrackFormatException if the track file contains invalid data (no tracklines, ...) * @throws InvalidTrackFormatException if the track file contains invalid data (no tracklines, ...)
@ -126,7 +126,9 @@ public class Track implements TrackSpecification {
//TODO: THIS //TODO: THIS
/** /**
* @throws InvalidTrackFormatException * Determines the finish line and saves it in a list, throws an Exception if none is found.
*
* @throws InvalidTrackFormatException thrown if no finish line is found
*/ */
private void findFinish() throws InvalidTrackFormatException { private void findFinish() throws InvalidTrackFormatException {
for (int i = 0; i < track.size(); i++) { for (int i = 0; i < track.size(); i++) {
@ -238,8 +240,8 @@ public class Track implements TrackSpecification {
/** /**
* This Method will check if the Car would crash at the specific position * This Method will check if the Car would crash at the specific position
* *
* @param positionVector the position to check if the car could crash * @param positionVector the position to check if the car would crash
* @return true if car would crash. Else false. * @return true if crash otherwise false
*/ */
public boolean willCrashAtPosition(int carIndex, PositionVector positionVector) { public boolean willCrashAtPosition(int carIndex, PositionVector positionVector) {
char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX()); char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX());
@ -252,10 +254,10 @@ public class Track implements TrackSpecification {
} }
/** /**
* This Method will make the Car Crash. In Track and in the Car Object * This Method will mark the Car as crashed inside the track and the car Object.
* *
* @param carIndex representing current Car * @param carIndex of car that will be marked as crashed
* @param crashPositionVector where the Crash did happen * @param crashPositionVector of the location of the crash
*/ */
public void carDoesCrash(int carIndex, PositionVector crashPositionVector) { public void carDoesCrash(int carIndex, PositionVector crashPositionVector) {
PositionVector currentCarPosition = getCarPos(carIndex); PositionVector currentCarPosition = getCarPos(carIndex);
@ -271,7 +273,7 @@ public class Track implements TrackSpecification {
* If the location is outside the track bounds, it is considered a wall. * If the location is outside the track bounds, it is considered a wall.
* *
* @param position The coordinates of the position to examine * @param position The coordinates of the position to examine
* @return The type of track position at the given location * @return The type of space at the desired position
*/ */
@Override @Override
public Config.SpaceType getSpaceType(PositionVector position) { public Config.SpaceType getSpaceType(PositionVector position) {
@ -287,9 +289,9 @@ public class Track implements TrackSpecification {
} }
/** /**
* Return the number of cars. * Return the number of cars that are located in a track
* *
* @return Number of cars * @return number of cars as int
*/ */
@Override @Override
public int getCarCount() { public int getCarCount() {
@ -363,6 +365,13 @@ public class Track implements TrackSpecification {
return currentSpace.getValue(); return currentSpace.getValue();
} }
/**
* Determines all points that lie between the two position vectors including the endpoint VectorPosition using the Bresenham algorithm.
*
* @param startPosition PositionVector of the finish coordinate
* @param endPosition PositionVector of the start coordinate
* @return ArrayList containing PositionVectors of all position that are between the start and finish including the finish position.
*/
public ArrayList<PositionVector> calculatePointsOnPath(PositionVector startPosition, PositionVector endPosition) { public ArrayList<PositionVector> calculatePointsOnPath(PositionVector startPosition, PositionVector endPosition) {
ArrayList<PositionVector> pathList = new ArrayList<>(); ArrayList<PositionVector> pathList = new ArrayList<>();
// Use Bresenham's algorithm to determine positions. // Use Bresenham's algorithm to determine positions.

View File

@ -25,7 +25,6 @@ public class UserInterface {
public UserInterface(String welcomeText) { public UserInterface(String welcomeText) {
textIO = TextIoFactory.getTextIO(); textIO = TextIoFactory.getTextIO();
textTerminal = textIO.getTextTerminal(); textTerminal = textIO.getTextTerminal();
textTerminal.println(welcomeText + "\n"); textTerminal.println(welcomeText + "\n");
} }

View File

@ -187,8 +187,8 @@ class GameTest {
private class interFace extends UserInterface { private class interFace extends UserInterface {
private final PositionVector.Direction[] directions; private PositionVector.Direction[] directions;
private final Integer[] instructions; private Integer[] instructions;
private int pointerDir,pointerInstruction; private int pointerDir,pointerInstruction;