Merge remote-tracking branch 'origin/main'
# Conflicts: # src/main/java/ch/zhaw/pm2/racetrack/Track.java
This commit is contained in:
commit
13012c1d4b
File diff suppressed because one or more lines are too long
|
@ -203,7 +203,7 @@ public class Game implements GameSpecification {
|
|||
* for this turn
|
||||
*/
|
||||
@Override
|
||||
public void doCarTurn(Direction acceleration) throws PositionVectorNotValidException {
|
||||
public void doCarTurn(Direction acceleration) {
|
||||
track.getCar(currentCarIndex).accelerate(acceleration);
|
||||
PositionVector crashPosition = null;
|
||||
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());
|
||||
direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove();
|
||||
}
|
||||
try {
|
||||
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.");
|
||||
}
|
||||
doCarTurn(direction);
|
||||
switchToNextActiveCar();
|
||||
}
|
||||
userInterface.printTrack(track);
|
||||
|
@ -295,7 +290,6 @@ public class Game implements GameSpecification {
|
|||
* 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 void calculateWinner(PositionVector start, PositionVector finish, int carIndex) {
|
||||
List<PositionVector> path = calculatePath(start, finish);
|
||||
|
@ -342,7 +336,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 PositionVectorNotValidException {
|
||||
public boolean willCarCrash(int carIndex, PositionVector position) {
|
||||
return track.willCrashAtPosition(carIndex, position);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package ch.zhaw.pm2.racetrack;
|
||||
|
||||
public class PositionVectorNotValidException extends Throwable {
|
||||
public PositionVectorNotValidException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public PositionVectorNotValidException() {}
|
||||
}
|
|
@ -124,8 +124,8 @@ public class Track implements TrackSpecification {
|
|||
|
||||
}
|
||||
//TODO: THIS
|
||||
|
||||
/**
|
||||
*
|
||||
* @throws InvalidTrackFormatException
|
||||
*/
|
||||
private void findFinish() throws InvalidTrackFormatException {
|
||||
|
@ -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 would crash
|
||||
* @return true if crash otherwise 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 of car that will be marked as crashed
|
||||
* @param crashPositionVector of the location of the crash
|
||||
*/
|
||||
public void carDoesCrash(int carIndex, PositionVector crashPositionVector) throws PositionVectorNotValid {
|
||||
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);
|
||||
|
@ -289,7 +271,7 @@ public class Track implements TrackSpecification {
|
|||
* If the location is outside the track bounds, it is considered a wall.
|
||||
*
|
||||
* @param position The coordinates of the position to examine
|
||||
* @return The type of space at the desired position
|
||||
* @return The type of track position at the given location
|
||||
*/
|
||||
@Override
|
||||
public Config.SpaceType getSpaceType(PositionVector position) {
|
||||
|
@ -302,7 +284,7 @@ public class Track implements TrackSpecification {
|
|||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return ConfigSpecification.SpaceType.WALL;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -25,7 +25,6 @@ public class UserInterface {
|
|||
public UserInterface(String welcomeText) {
|
||||
textIO = TextIoFactory.getTextIO();
|
||||
textTerminal = textIO.getTextTerminal();
|
||||
|
||||
textTerminal.println(welcomeText + "\n");
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -112,22 +112,14 @@ class GameTest {
|
|||
|
||||
@Test
|
||||
void carTurnCorrect() {
|
||||
try {
|
||||
game.doCarTurn(RIGHT);
|
||||
Assertions.assertEquals(new PositionVector(1, 0), game.getCarVelocity(0));
|
||||
} catch (PositionVectorNotValidException positionVectorNotValidException) {
|
||||
positionVectorNotValidException.printStackTrace();
|
||||
}
|
||||
game.doCarTurn(RIGHT);
|
||||
Assertions.assertEquals(new PositionVector(1, 0), game.getCarVelocity(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void carCrash() {
|
||||
try {
|
||||
game.doCarTurn(PositionVector.Direction.UP);
|
||||
Assertions.assertTrue(game.onlyOneCarLeft());
|
||||
} catch (PositionVectorNotValidException positionVectorNotValidException) {
|
||||
positionVectorNotValidException.printStackTrace();
|
||||
}
|
||||
game.doCarTurn(PositionVector.Direction.UP);
|
||||
Assertions.assertTrue(game.onlyOneCarLeft());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -141,7 +133,7 @@ class GameTest {
|
|||
|
||||
@Test
|
||||
void winner() {
|
||||
game = new Game(new interFace("Test",new Integer[]{0,2,1},new PositionVector.Direction[]{RIGHT,
|
||||
game = new Game(new interFace("Test",new Integer[]{0,2,0},new PositionVector.Direction[]{RIGHT,
|
||||
RIGHT,
|
||||
RIGHT,
|
||||
NONE,
|
||||
|
@ -186,7 +178,7 @@ class GameTest {
|
|||
|
||||
@Test
|
||||
void crashA() {
|
||||
game = new Game(new interFace("Test",new Integer[]{0,2,2},new PositionVector.Direction[]{UP}));
|
||||
game = new Game(new interFace("Test",new Integer[]{0,1,1},new PositionVector.Direction[]{UP}));
|
||||
game.initPhase();
|
||||
Assertions.assertEquals("b",game.gamePhase());
|
||||
}
|
||||
|
@ -195,8 +187,8 @@ class GameTest {
|
|||
|
||||
private class interFace extends UserInterface {
|
||||
|
||||
private final PositionVector.Direction[] directions;
|
||||
private final Integer[] instructions;
|
||||
private PositionVector.Direction[] directions;
|
||||
private Integer[] instructions;
|
||||
private int pointerDir,pointerInstruction;
|
||||
|
||||
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
//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)));
|
||||
|
||||
}
|
||||
|
||||
@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");
|
||||
}
|
||||
trackObj.carDoesCrash(0, new PositionVector(6, 22));
|
||||
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)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue