removed PositionVectorNotValidException.java

This commit is contained in:
Andrin Fassbind 2022-03-24 17:27:30 +01:00
parent eca2e2eb9b
commit f6a16181cf
6 changed files with 16 additions and 68 deletions

File diff suppressed because one or more lines are too long

View File

@ -203,7 +203,7 @@ public class Game implements GameSpecification {
* for this turn * for this turn
*/ */
@Override @Override
public void doCarTurn(Direction acceleration) throws PositionVectorNotValidException { public void doCarTurn(Direction acceleration) {
track.getCar(currentCarIndex).accelerate(acceleration); track.getCar(currentCarIndex).accelerate(acceleration);
PositionVector crashPosition = null; PositionVector crashPosition = null;
List<PositionVector> positionList = calculatePath(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition()); List<PositionVector> positionList = calculatePath(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition());
@ -239,12 +239,7 @@ public class Game implements GameSpecification {
track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy()); track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy());
direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove();
} }
try { doCarTurn(direction);
doCarTurn(direction);
} catch (PositionVectorNotValidException e) {
e.printStackTrace();
userInterface.printInformation("There has been an unexpected Error. It seems that the trackfile is not Valid. Please do only use the given trackfiles. Otherwise please check that your selfmade tracks have borders arround the track.");
}
switchToNextActiveCar(); switchToNextActiveCar();
} }
userInterface.printTrack(track); userInterface.printTrack(track);
@ -342,7 +337,7 @@ public class Game implements GameSpecification {
* @return A boolean indicator if the car would crash with a WALL or another car. * @return A boolean indicator if the car would crash with a WALL or another car.
*/ */
@Override @Override
public boolean willCarCrash(int carIndex, PositionVector position) throws PositionVectorNotValidException { public boolean willCarCrash(int carIndex, PositionVector position) {
return track.willCrashAtPosition(carIndex, 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.remove(positionVector.getY());
track.add(positionVector.getY(), line); 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 * 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 * @param positionVector the position to check if the car could crash
* @return true if car would crash. Else false. * @return true if car would crash. Else false.
*/ */
public boolean willCrashAtPosition(int carIndex, PositionVector positionVector) throws PositionVectorNotValidException { public boolean willCrashAtPosition(int carIndex, PositionVector positionVector) {
isPositionVectorOnTrack(positionVector); //TODO: remove this line? Or Method?
char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX()); char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX());
if (getCarId(carIndex) == charAtPosition) return false; if (getCarId(carIndex) == charAtPosition) return false;
return !(charAtPosition == ConfigSpecification.SpaceType.TRACK.value || return !(charAtPosition == ConfigSpecification.SpaceType.TRACK.value ||
@ -274,8 +257,7 @@ public class Track implements TrackSpecification {
* @param carIndex representing current Car * @param carIndex representing current Car
* @param crashPositionVector where the Crash did happen * @param crashPositionVector where the Crash did happen
*/ */
public void carDoesCrash(int carIndex, PositionVector crashPositionVector) throws PositionVectorNotValidException { public void carDoesCrash(int carIndex, PositionVector crashPositionVector) {
isPositionVectorOnTrack(crashPositionVector); //TODO: remove this line? and Method?
PositionVector currentCarPosition = getCarPos(carIndex); PositionVector currentCarPosition = getCarPos(carIndex);
drawCharOnTrackIndicator(new PositionVector(currentCarPosition.getX(), currentCarPosition.getY()), ConfigSpecification.SpaceType.TRACK.getValue()); drawCharOnTrackIndicator(new PositionVector(currentCarPosition.getX(), currentCarPosition.getY()), ConfigSpecification.SpaceType.TRACK.getValue());
Car car = cars.get(carIndex); Car car = cars.get(carIndex);

View File

@ -1,7 +1,6 @@
package ch.zhaw.pm2.racetrack.given; package ch.zhaw.pm2.racetrack.given;
import ch.zhaw.pm2.racetrack.PositionVector; import ch.zhaw.pm2.racetrack.PositionVector;
import ch.zhaw.pm2.racetrack.PositionVectorNotValidException;
import java.util.List; import java.util.List;
@ -19,11 +18,11 @@ public interface GameSpecification {
int getWinner(); int getWinner();
void doCarTurn(PositionVector.Direction acceleration) throws PositionVectorNotValidException; void doCarTurn(PositionVector.Direction acceleration);
void switchToNextActiveCar(); void switchToNextActiveCar();
List<PositionVector> calculatePath(PositionVector startPosition, PositionVector endPosition); 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 @Test
@DisplayName("Will Car Crash") @DisplayName("Will Car Crash")
void willCarCrash() { void willCarCrash() {
try { //Car will Crash
//Car will Crash Assertions.assertTrue(trackObj.willCrashAtPosition(0, new PositionVector(25, 21)));
Assertions.assertTrue(trackObj.willCrashAtPosition(0, new PositionVector(25, 21))); //Car will not Crash and is on track
//Car will not Crash and is on track Assertions.assertFalse(trackObj.willCrashAtPosition(0, new PositionVector(7, 22)));
Assertions.assertFalse(trackObj.willCrashAtPosition(0, new PositionVector(7, 22))); //Car will not Crash and is on finishLine
//Car will not Crash and is on finishLine Assertions.assertFalse(trackObj.willCrashAtPosition(0, trackObj.getFinishLine().get(0)));
Assertions.assertFalse(trackObj.willCrashAtPosition(0, trackObj.getFinishLine().get(0)));
} catch (PositionVectorNotValidException positionVectorNotValidException) {
positionVectorNotValidException.printStackTrace();
Assertions.fail("Test should not throw error");
}
} }
@Test @Test
@DisplayName("Make Car Crash") @DisplayName("Make Car Crash")
void makeCarCrash() { void makeCarCrash() {
try { trackObj.carDoesCrash(0, new PositionVector(6, 22));
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.assertEquals(Track.CRASH_INDICATOR, trackObj.getTrack().get(22).charAt(6));
Assertions.assertTrue(trackObj.getCar(0).isCrashed()); Assertions.assertTrue(trackObj.getCar(0).isCrashed());
} }
@ -140,7 +131,6 @@ public class TrackTest {
@DisplayName("Negative TestCase") @DisplayName("Negative TestCase")
class negativeClass { class negativeClass {
File file; File file;
@BeforeEach @BeforeEach
void setup() { void setup() {
file = new File(".\\tracks\\challenge.txt"); file = new File(".\\tracks\\challenge.txt");
@ -165,14 +155,5 @@ public class TrackTest {
File testfile = new File(".\\src\\test\\InvalidTracks\\sameCar.txt"); File testfile = new File(".\\src\\test\\InvalidTracks\\sameCar.txt");
Assertions.assertThrows(InvalidTrackFormatException.class, () -> new Track(testfile)); 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)));
}
} }
} }