Game #23
|
@ -0,0 +1,9 @@
|
||||||
|
package ch.zhaw.pm2.racetrack;
|
||||||
|
|
||||||
|
public class PositionVectorNotValid extends Throwable {
|
||||||
|
public PositionVectorNotValid(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public PositionVectorNotValid() {}
|
||||||
|
}
|
|
@ -59,11 +59,9 @@ import java.util.Scanner;
|
||||||
public class Track implements TrackSpecification {
|
public class Track implements TrackSpecification {
|
||||||
|
|
||||||
public static final char CRASH_INDICATOR = 'X';
|
public static final char CRASH_INDICATOR = 'X';
|
||||||
|
|
||||||
// TODO: Add necessary variables
|
|
||||||
private List<String> track;
|
private List<String> track;
|
||||||
private List<Car> cars;
|
private List<Car> cars;
|
||||||
private List<PositionVector> finishLine;
|
private final List<PositionVector> finishLine;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize a Track from the given track file.
|
* Initialize a Track from the given track file.
|
||||||
|
@ -72,8 +70,7 @@ public class Track implements TrackSpecification {
|
||||||
* @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, ...)
|
||||||
*/
|
*/
|
||||||
public Track(File trackFile) throws FileNotFoundException, InvalidTrackFormatException {
|
public Track(File trackFile) throws FileNotFoundException, InvalidTrackFormatException, PositionVectorNotValid {
|
||||||
|
|
||||||
track = new ArrayList<>();
|
track = new ArrayList<>();
|
||||||
cars = new ArrayList<>();
|
cars = new ArrayList<>();
|
||||||
finishLine = new ArrayList<>();
|
finishLine = new ArrayList<>();
|
||||||
|
@ -161,9 +158,18 @@ public class Track implements TrackSpecification {
|
||||||
private void drawCharOnTrackIndicator(PositionVector positionVector, char symbol) {
|
private void drawCharOnTrackIndicator(PositionVector positionVector, char symbol) {
|
||||||
String line = track.get(positionVector.getY());
|
String line = track.get(positionVector.getY());
|
||||||
line = line.substring(0, positionVector.getX()) + symbol + line.substring(positionVector.getX() + 1);
|
line = line.substring(0, positionVector.getX()) + symbol + line.substring(positionVector.getX() + 1);
|
||||||
|
track.remove(positionVector.getY());
|
||||||
track.add(positionVector.getY(), line);
|
track.add(positionVector.getY(), line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void isPositionVectorOnTrack(PositionVector positionVector) throws PositionVectorNotValid {
|
||||||
|
try{
|
||||||
|
track.get(positionVector.getY()).charAt(positionVector.getX());
|
||||||
|
}catch (IndexOutOfBoundsException e) {
|
||||||
|
throw new PositionVectorNotValid();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return all Cars
|
* @return all Cars
|
||||||
*/
|
*/
|
||||||
|
@ -188,6 +194,7 @@ public class Track implements TrackSpecification {
|
||||||
/**
|
/**
|
||||||
* This Method will update the Car on the track
|
* This Method will update the Car on the track
|
||||||
* and will make the Car move to the next position
|
* and will make the Car move to the next position
|
||||||
|
*
|
||||||
* @param carIndex representing the current Car
|
* @param carIndex representing the current Car
|
||||||
*/
|
*/
|
||||||
public void moveCar(int carIndex) {
|
public void moveCar(int carIndex) {
|
||||||
|
@ -198,7 +205,8 @@ public class Track implements TrackSpecification {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class does change the Position of the car only in the track.
|
* This class does change the Position of the car only in the track.
|
||||||
* @param carIndex
|
*
|
||||||
|
* @param carIndex of the current car
|
||||||
*/
|
*/
|
||||||
private void makeCarMoveInTrack(int carIndex) {
|
private void makeCarMoveInTrack(int carIndex) {
|
||||||
PositionVector positionVector = findChar(getCarId(carIndex));
|
PositionVector positionVector = findChar(getCarId(carIndex));
|
||||||
|
@ -211,25 +219,29 @@ public class Track implements TrackSpecification {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This Method will check if the Car could crash at the specific position
|
* This Method will check if the Car could 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 could crash
|
||||||
* @return true if car would crash. Else false.
|
* @return true if car would crash. Else false.
|
||||||
*/
|
*/
|
||||||
public boolean willCrashAtPosition(PositionVector positionVector) {
|
public boolean willCrashAtPosition(int carIndex, PositionVector positionVector) throws PositionVectorNotValid {
|
||||||
|
isPositionVectorOnTrack(positionVector);
|
||||||
char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX());
|
char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX());
|
||||||
return charAtPosition != ConfigSpecification.SpaceType.TRACK.value;
|
if (getCarId(carIndex) == charAtPosition) return false;
|
||||||
|
return (charAtPosition == ConfigSpecification.SpaceType.WALL.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This Method will make the Car Crash. In Track and in the Car Object
|
* This Method will make the Car Crash. In Track and in the Car Object
|
||||||
|
*
|
||||||
* @param carIndex representing current Car
|
* @param carIndex representing current Car
|
||||||
* @param positionVector where the Crash did happen
|
* @param positionVector where the Crash did happen
|
||||||
*/
|
*/
|
||||||
public void carDoesCrash(int carIndex,PositionVector positionVector) {
|
public void carDoesCrash(int carIndex, PositionVector positionVector) throws PositionVectorNotValid{
|
||||||
|
isPositionVectorOnTrack(positionVector);
|
||||||
Car car = cars.get(carIndex);
|
Car car = cars.get(carIndex);
|
||||||
car.crash();
|
car.crash();
|
||||||
makeCarMoveInTrack(carIndex);
|
car.setPosition(positionVector);
|
||||||
drawCharOnTrackIndicator(new PositionVector(positionVector.getX()+1,positionVector.getY()),CRASH_INDICATOR);
|
drawCharOnTrackIndicator(new PositionVector(positionVector.getX(), positionVector.getY()), CRASH_INDICATOR);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -241,9 +253,8 @@ public class Track implements TrackSpecification {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Config.SpaceType getSpaceType(PositionVector position) {
|
public Config.SpaceType getSpaceType(PositionVector position) {
|
||||||
|
//isPositionVectorOnTrack(position); Should be used but we are not allowed to change method head. We don't use function anyway
|
||||||
char charAtPosition = track.get(position.getY()).charAt(position.getX());
|
char charAtPosition = track.get(position.getY()).charAt(position.getX());
|
||||||
|
|
||||||
ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values();
|
ConfigSpecification.SpaceType[] spaceTypes = ConfigSpecification.SpaceType.values();
|
||||||
for (ConfigSpecification.SpaceType spaceType : spaceTypes) {
|
for (ConfigSpecification.SpaceType spaceType : spaceTypes) {
|
||||||
if (spaceType.getValue() == charAtPosition) {
|
if (spaceType.getValue() == charAtPosition) {
|
||||||
|
@ -321,14 +332,13 @@ public class Track implements TrackSpecification {
|
||||||
@Override
|
@Override
|
||||||
public char getCharAtPosition(int y, int x, Config.SpaceType currentSpace) {
|
public char getCharAtPosition(int y, int x, Config.SpaceType currentSpace) {
|
||||||
char charAtPos = track.get(y).charAt(x);
|
char charAtPos = track.get(y).charAt(x);
|
||||||
|
PositionVector positionVector = new PositionVector(x, y);
|
||||||
for (Car car : cars) {
|
for (Car car : cars) {
|
||||||
if (charAtPos == car.getID()) {
|
if (charAtPos == car.getID()) {
|
||||||
if(car.isCrashed()) {
|
|
||||||
return CRASH_INDICATOR;
|
|
||||||
}
|
|
||||||
return charAtPos;
|
return charAtPos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (positionVector.equals(findChar(CRASH_INDICATOR))) return CRASH_INDICATOR;
|
||||||
return currentSpace.getValue();
|
return currentSpace.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -339,10 +349,8 @@ public class Track implements TrackSpecification {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String str = "";
|
StringBuilder str = new StringBuilder();
|
||||||
for (String line : track) {
|
for (String line : track) str.append(line).append("\n");
|
||||||
str += line + "\n";
|
return str.toString();
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
###############################################################
|
||||||
|
################# #############
|
||||||
|
############### ###########
|
||||||
|
############### #########
|
||||||
|
############### ################## #########
|
||||||
|
############### #################### #########
|
||||||
|
############## ##################### #########
|
||||||
|
############ ###################### ##########
|
||||||
|
######### ###################### ############
|
||||||
|
######### ###################### ##############
|
||||||
|
######### ##################### ################
|
||||||
|
######### ################# ##################
|
||||||
|
######### ################ ##################
|
||||||
|
########## ################## ###############
|
||||||
|
########### #################### #############
|
||||||
|
########### ####################### ##########
|
||||||
|
########## ########################## #########
|
||||||
|
######### ############################ ########
|
||||||
|
######## ############################# ########
|
||||||
|
####### ############################## ########
|
||||||
|
###### ############################# ########
|
||||||
|
###### ############################ #########
|
||||||
|
###### > a ###########
|
||||||
|
###### > a ##############
|
||||||
|
######## > b #################
|
||||||
|
###############################################################
|
|
@ -1,10 +1,7 @@
|
||||||
package ch.zhaw.pm2.racetrack;
|
package ch.zhaw.pm2.racetrack;
|
||||||
|
|
||||||
import ch.zhaw.pm2.racetrack.given.ConfigSpecification;
|
import ch.zhaw.pm2.racetrack.given.ConfigSpecification;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.*;
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
@ -15,48 +12,43 @@ import java.util.List;
|
||||||
public class TrackTest {
|
public class TrackTest {
|
||||||
Track trackObj;
|
Track trackObj;
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Positiv Test Cases")
|
||||||
|
class positivClass {
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void setup() {
|
||||||
File file = new File("C:\\Studium\\Semester2\\PM2\\Projekt1\\racetrack\\tracks\\challenge.txt");
|
File file = new File(".\\tracks\\challenge.txt");
|
||||||
try {
|
try {
|
||||||
trackObj = new Track(file);
|
trackObj = new Track(file);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception | PositionVectorNotValid e) {
|
||||||
System.err.println("Error in Test compareTrack" + e.getMessage());
|
System.err.println("Error in Test compareTrack" + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void canReadFile() {
|
@DisplayName("Create correct amount of Car instance")
|
||||||
File file = new File("C:\\Studium\\Semester2\\PM2\\Projekt1\\racetrack\\tracks\\challenge.txt");
|
void checkCars() {
|
||||||
Assertions.assertThrows(FileNotFoundException.class,() -> new Track(file));
|
Assertions.assertEquals(2, trackObj.getCarCount());
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Dirty test...
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
void printOutTrack() {
|
|
||||||
System.out.println(trackObj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Create Car instance with correct Symbols / Id")
|
||||||
|
void checkCarId() {
|
||||||
|
Assertions.assertEquals('a', trackObj.getCarId(0));
|
||||||
|
Assertions.assertEquals('b', trackObj.getCarId(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Check getSpaceTyp")
|
||||||
void getSpaceTyp() {
|
void getSpaceTyp() {
|
||||||
Assertions.assertEquals(ConfigSpecification.SpaceType.FINISH_RIGHT, trackObj.getSpaceType(new PositionVector(22, 24)));
|
Assertions.assertEquals(ConfigSpecification.SpaceType.FINISH_RIGHT, trackObj.getSpaceType(new PositionVector(22, 24)));
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO:
|
|
||||||
@Test
|
|
||||||
void addCarAtInit() {
|
|
||||||
try {
|
|
||||||
trackObj.addCar();
|
|
||||||
} catch (InvalidTrackFormatException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Find FinishLine")
|
||||||
void findFinish() {
|
void findFinish() {
|
||||||
List<PositionVector> expected = new ArrayList<>();
|
List<PositionVector> expected = new ArrayList<>();
|
||||||
expected.add(new PositionVector(22, 22));
|
expected.add(new PositionVector(22, 22));
|
||||||
|
@ -66,13 +58,120 @@ public class TrackTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void makeTrackObj() {
|
@DisplayName("Converts Trackfile correctly to List<String>")
|
||||||
|
void checkTrack() {
|
||||||
|
Track trackObj;
|
||||||
try {
|
try {
|
||||||
Track t1 = new Track(new File("C:\\Studium\\Semester2\\PM2\\Projekt1\\racetrack\\tracks\\challenge.txt"));
|
trackObj = new Track(new File(".\\tracks\\oval-anticlock-right.txt"));
|
||||||
} catch (FileNotFoundException e) {
|
List<String> track = new ArrayList<>();
|
||||||
e.printStackTrace();
|
track.add("##################################################");
|
||||||
} catch (InvalidTrackFormatException e) {
|
track.add("##################################################");
|
||||||
|
track.add("############## #############");
|
||||||
|
track.add("########## ##########");
|
||||||
|
track.add("####### #######");
|
||||||
|
track.add("###### ################# ######");
|
||||||
|
track.add("##### ################### #####");
|
||||||
|
track.add("##### ################### #####");
|
||||||
|
track.add("###### ################# ######");
|
||||||
|
track.add("####### > a #######");
|
||||||
|
track.add("########## > ##########");
|
||||||
|
track.add("############## > b ##############");
|
||||||
|
track.add("##################################################");
|
||||||
|
track.add("##################################################");
|
||||||
|
Assertions.assertLinesMatch(track, trackObj.getTrack());
|
||||||
|
} catch (FileNotFoundException | InvalidTrackFormatException | PositionVectorNotValid e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Make Car move down on track")
|
||||||
|
void makeCarMoveDown() {
|
||||||
|
PositionVector beforeMove = trackObj.getCarPos(0);
|
||||||
|
trackObj.getCar(0).accelerate(PositionVector.Direction.DOWN);
|
||||||
|
trackObj.moveCar(0);
|
||||||
|
PositionVector afterMove = trackObj.getCarPos(0);
|
||||||
|
Assertions.assertEquals(beforeMove.getY() + 1, afterMove.getY());
|
||||||
|
Assertions.assertEquals(beforeMove.getX(), afterMove.getX());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Make Car move with (0,0) acceleration on track")
|
||||||
|
void makeCarStay() {
|
||||||
|
PositionVector beforeMove = trackObj.getCarPos(0);
|
||||||
|
trackObj.moveCar(0);
|
||||||
|
PositionVector afterMove = trackObj.getCarPos(0);
|
||||||
|
Assertions.assertEquals(beforeMove.getY(), afterMove.getY());
|
||||||
|
Assertions.assertEquals(beforeMove.getX(), afterMove.getX());
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 (PositionVectorNotValid positionVectorNotValid) {
|
||||||
|
positionVectorNotValid.printStackTrace();
|
||||||
|
Assertions.fail("Test should not throw error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Make Car Crash")
|
||||||
|
void makeCarCrash() {
|
||||||
|
try {
|
||||||
|
trackObj.carDoesCrash(0, new PositionVector(6, 22));
|
||||||
|
} catch (PositionVectorNotValid positionVectorNotValid) {
|
||||||
|
positionVectorNotValid.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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@DisplayName("Negative TestCase")
|
||||||
|
class negativeClass {
|
||||||
|
File file;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
file = new File(".\\tracks\\challenge.txt");
|
||||||
|
try {
|
||||||
|
trackObj = new Track(file);
|
||||||
|
|
||||||
|
} catch (Exception | PositionVectorNotValid e) {
|
||||||
|
System.err.println("Error in Test compareTrack" + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Throw error if File not found")
|
||||||
|
void canReadFile() {
|
||||||
|
file = new File(".\\tracks\\NotExisting.txt");
|
||||||
|
Assertions.assertThrows(FileNotFoundException.class, () -> new Track(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Throw error if File is invalid")
|
||||||
|
void invalidTrackFile() {
|
||||||
|
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(PositionVectorNotValid.class, () -> trackObj.willCrashAtPosition(0, new PositionVector(100, 200)));
|
||||||
|
Assertions.assertThrows(PositionVectorNotValid.class, () -> trackObj.carDoesCrash(1,new PositionVector(200,100)));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue