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
|
* 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);
|
||||||
|
@ -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.
|
* 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 start the startposition of the car
|
||||||
* @param finish the expected finishpositon of the car after the move
|
* @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) {
|
private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) {
|
||||||
List<PositionVector> path = calculatePath(start, finish);
|
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.
|
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
//TODO: THIS
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @throws InvalidTrackFormatException
|
* @throws InvalidTrackFormatException
|
||||||
*/
|
*/
|
||||||
private void findFinish() throws InvalidTrackFormatException {
|
private void findFinish() throws InvalidTrackFormatException {
|
||||||
|
@ -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 would crash
|
* @param positionVector the position to check if the car would crash
|
||||||
* @return true if crash otherwise false
|
* @return true if crash otherwise 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 of car that will be marked as crashed
|
* @param carIndex of car that will be marked as crashed
|
||||||
* @param crashPositionVector of the location of the crash
|
* @param crashPositionVector of the location of the crash
|
||||||
*/
|
*/
|
||||||
public void carDoesCrash(int carIndex, PositionVector crashPositionVector) throws PositionVectorNotValid {
|
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);
|
||||||
|
@ -289,7 +271,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 space at the desired position
|
* @return The type of track position at the given location
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Config.SpaceType getSpaceType(PositionVector position) {
|
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) {
|
public UserInterface(String welcomeText) {
|
||||||
textIO = TextIoFactory.getTextIO();
|
textIO = TextIoFactory.getTextIO();
|
||||||
textTerminal = textIO.getTextTerminal();
|
textTerminal = textIO.getTextTerminal();
|
||||||
|
|
||||||
textTerminal.println(welcomeText + "\n");
|
textTerminal.println(welcomeText + "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -112,22 +112,14 @@ class GameTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void carTurnCorrect() {
|
void carTurnCorrect() {
|
||||||
try {
|
|
||||||
game.doCarTurn(RIGHT);
|
game.doCarTurn(RIGHT);
|
||||||
Assertions.assertEquals(new PositionVector(1, 0), game.getCarVelocity(0));
|
Assertions.assertEquals(new PositionVector(1, 0), game.getCarVelocity(0));
|
||||||
} catch (PositionVectorNotValidException positionVectorNotValidException) {
|
|
||||||
positionVectorNotValidException.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void carCrash() {
|
void carCrash() {
|
||||||
try {
|
|
||||||
game.doCarTurn(PositionVector.Direction.UP);
|
game.doCarTurn(PositionVector.Direction.UP);
|
||||||
Assertions.assertTrue(game.onlyOneCarLeft());
|
Assertions.assertTrue(game.onlyOneCarLeft());
|
||||||
} catch (PositionVectorNotValidException positionVectorNotValidException) {
|
|
||||||
positionVectorNotValidException.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,7 +133,7 @@ class GameTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void winner() {
|
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,
|
||||||
RIGHT,
|
RIGHT,
|
||||||
NONE,
|
NONE,
|
||||||
|
@ -186,7 +178,7 @@ class GameTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void crashA() {
|
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();
|
game.initPhase();
|
||||||
Assertions.assertEquals("b",game.gamePhase());
|
Assertions.assertEquals("b",game.gamePhase());
|
||||||
}
|
}
|
||||||
|
@ -195,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;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue