diff --git a/Klassendiagramm.drawio b/Klassendiagramm.drawio new file mode 100644 index 0000000..afde28c --- /dev/null +++ b/Klassendiagramm.drawio @@ -0,0 +1 @@ +7Vtbd6I6GP01PraLi7c+qr3MzOn0OGNPe16jRMgxEE+Itc6vny8QQExVaGVorWt1rZKPkED2zpedDTbsgf98w9Hc+84cTBuW4Tw37MuGZZnwB/9kZBVHOu1mHHA5cVSlLDAiv7AKGiq6IA4OcxUFY1SQeT44YUGAJyIXQ5yzZb7alNF8r3PkYi0wmiCqRx+JI7w42rU6WfwLJq6X9Gy2L+IzPkoqqycJPeSw5VrIvmrYA86YiI/85wGmcvCScXn8unqkt7P2zbcf4f/on/5f93cPZ3Fj12UuSR+B40C8uulfs+n1l4fmf//Oh9c/lsZNb/ztTF1iPCG6UOM1xDxkgXpisUqGMVwSn6IASv0pC8RInYFB6CNK3ACOJ3B3mEPgCXNBAIGeOiHYHKITj1DnFq3YQj5DKNBklpT6HuPkFzSLKJwyIQCnuVBkstq5GiN5JYQNiHIcQp1hMjBmGrpFoVB1JoxSNA/JOLphWcVH3CVBnwnB/KQhtggc7KhSinRUEJzNUu7I6wvCoWCTo4Gf18io4LnBzMeCr6CKOttVaKiZ1lLFZUZbs61i3jpl7a6aLmqquGnLaWc/YWqhwIUhSHuz7Hx3ZrNgf4BHrjtEAfYACdyXgxiukxAO1h40C0XULEFTU6PpHfKxRlIYZ7FGSIqnYisdwzmakMC9jepcNrPIT/WkMsTg2imNqOARx8FBRBWBBIrZJPkxZyQQ0VC0+vAHAzYwzluNFtzQAMpmVoY/WZ2LAQuAVYhE9MFA1SWWdH2BWDsn7X5irfKIlUV2nUg5SMviZ+lpxmOBJOPdwh9D0jg2JHfkE0/4VB1WhXfLqhlvW8P7ykdEXtZzHMjQ4QnwgwLe6dYMeFMDXEOYkkg9qNEwX1xZ98DvA5CyuQTve0mHyzNT44Stc8J+AX+KxpgOWUgEYbJ9Htfd4EVdWds0msVQ3bH8vwnUlp61F3zioVAmbhBdMxje6OgI53NloJp15+a2hupILBz5mAU1v3HS/JVpfrtbreg37Vf2V4Po72wn6rHKxoJZp12YZe9lA9DVsOzB3UvrxjK+w0pydEgeVA+Wxrv2DcDFSQ8eHNSiIr8qOWjqZuFXSPzGFaARrbSWcc9kOeBMtnZkM3pdAjWrwrhbd6I2davtBssVd4R9EiAuAb9HM6w7xB8d36ogrV/xJzewKxvjwOnJty1QGlMmxXYfQkqNm0ZcvCY0Wbn2yOX88ocdFydTB9MxW15lgX4UgBMJU0qL7ZDBrhTvhwy2Fy4usJmTd7sT2JeA5JgiQZ7yL512aPOhJG6myy0jr8stw843ET+kumr9LU/ZhuJR0Bo6mFOvW39DzqY4DJku108bzOo3mLb5yh1fx9hL4hc2mHazQHed97HBNHXXcoQoUhn3E65t6eT9OBtLUzcpNfQ++06jPKxF94+VbTV0k/JTKZYEsXctWTYXls4rJcvmCmV1Nu6lasmiG43b3lKeBEv1gqW1oSAKf5Zi7lhjtguWVivf3bv+DEa3UUeCY6y/uvksgqVTmF3vRrDo1uiAiOOTnAc1wMvjXLsDnnw1mpurkB1OQB8W6No/fbF0xxSEvVzELWPAnBPgBwa88Gcx1SGu+6kDGBNxhMZB3VjbrxRjh8Na9zM1kD+9v1Ae11ZBGVaVwWDp1t8DQOQc4wJdHYrt2jOx7v79vRDzhXQXevJV5a2cBydECyN6UXu+Lef8sXk01uW8vdQWNOt3+tTY7jX6Eiz3On0KZ0mlnIuibuiNpp+98b2ieWHlmyhq+m26PFpDW0w/raGzzRdOG+2w6TTElfiGlu4bGufnCfAZXSMvbi0R7Ek7Y+XIxWt4H01mbsTfAaOMw/mAZf6jMrut4jkhmV/q55bqthrpjxyL+3Rnb2RUQsw/BpdunhXHKtFPHxWsw8z+5JPcPw6d7plReAq5wKOt3ueaHN9EVVfBRQz8VKPvxrqUif5GYmgE2MaV4uvExWGYknyevmHiv4UrUMx+bB1Xz36ybl/9Bg== \ No newline at end of file diff --git a/build.gradle b/build.gradle index 669126f..9b73dff 100644 --- a/build.gradle +++ b/build.gradle @@ -44,7 +44,7 @@ version = '2022.1' application { // Define the main class for the application. - mainClass = 'ch.zhaw.pm2.racetrack.ConsoleApp' + mainClass = 'ch.zhaw.pm2.racetrack.Main' } run { diff --git a/src/main/java/ch/zhaw/pm2/racetrack/InvalidFileFormatException.java b/src/main/java/ch/zhaw/pm2/racetrack/InvalidFileFormatException.java index e7d0c07..6655e69 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/InvalidFileFormatException.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/InvalidFileFormatException.java @@ -1,5 +1,10 @@ package ch.zhaw.pm2.racetrack; +/** + * Class for Exception when invalid Fileformat is used. + */ public class InvalidFileFormatException extends Exception { - // TODO: implementation + public InvalidFileFormatException(String errorMessage) { + super(errorMessage); + } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/InvalidTrackFormatException.java b/src/main/java/ch/zhaw/pm2/racetrack/InvalidTrackFormatException.java index 25de664..fa24c25 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/InvalidTrackFormatException.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/InvalidTrackFormatException.java @@ -1,5 +1,13 @@ package ch.zhaw.pm2.racetrack; +/** + * Class for Exception when invalid track format is used. + */ public class InvalidTrackFormatException extends Exception { - // TODO: implementation + public InvalidTrackFormatException(String errorMessage) { + super(errorMessage); + } + public InvalidTrackFormatException() { + super(); + } } diff --git a/src/test/java/ch/zhaw/pm2/racetrack/CarTest.java b/src/test/java/ch/zhaw/pm2/racetrack/CarTest.java new file mode 100644 index 0000000..6325c9b --- /dev/null +++ b/src/test/java/ch/zhaw/pm2/racetrack/CarTest.java @@ -0,0 +1,224 @@ + +package ch.zhaw.pm2.racetrack; + +import ch.zhaw.pm2.racetrack.strategy.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Tests for Class Car + */ +class CarTest { + + Car car; + + // Default coordinates for tests + int DEFAULT_X = 10; + int DEFAULT_Y = 10; + char DEFAULT_ID = 'f'; + + /** + * Create a new Car Object and set Position to a defined Default Position + */ + @BeforeEach + void setUp() { + car = new Car(DEFAULT_ID, new PositionVector(DEFAULT_X, DEFAULT_Y)); + } + + @Test + void getID() { + assertEquals(DEFAULT_ID, car.getID()); + } + + /** + * Method to check nextPosition with coordinates as int + * + * @param x the expected value for x coordinate + * @param y the expected value for y coordinate + */ + void checkNextPosition(int x, int y) { + assertEquals(new PositionVector(x, y), car.nextPosition()); + } + + /** + * - checks if the position of the car can be set and saved correctly with valid positions. + * - checks if an exception is throwed and position keeps unchanged if invalid coordinates are entered. + */ + @Test + void setPosition() { + checkNextPosition(DEFAULT_X, DEFAULT_Y); + + // List of valid Positions + List validPositions = new ArrayList<>(); + validPositions.add(new PositionVector(20, 20)); + validPositions.add(new PositionVector(0, 0)); + validPositions.add(new PositionVector(20, 0)); + validPositions.add(new PositionVector(0, 20)); + + for (PositionVector positionVector : validPositions) { + car.setPosition(positionVector); + assertEquals(positionVector, car.nextPosition()); + } + + // List of invalid positions. + List invalidPositions = new ArrayList<>(); + invalidPositions.add(new PositionVector(0, -20)); + invalidPositions.add(new PositionVector(-20, 0)); + invalidPositions.add(new PositionVector(-20, -20)); + + for (PositionVector positionVector : invalidPositions) { + boolean exception = false; + setUp(); + try { + car.setPosition(positionVector); + } catch (IllegalArgumentException e) { + exception = true; + } + assertTrue(exception); + + // position should keep unchanged + checkNextPosition(DEFAULT_X, DEFAULT_Y); + } + } + + void checkVelocity(int x, int y) { + assertEquals(new PositionVector(x, y), car.getVelocity()); + } + + + /** + * Checks if the methods accelerate, move and getVelocity are working correctly with acceleration in all directions. + * Checks also if velocity is calculated correctly if method accelerate is called a second time. + */ + @Test + void movement() { + // add all possible directions in a List + List directions = Arrays.asList(PositionVector.Direction.values()); + + //position shouldn't be changed because velocity should be 0. + car.move(); + checkNextPosition(DEFAULT_X, DEFAULT_Y); + + + + for (PositionVector.Direction direction1 : directions) { + for (PositionVector.Direction direction2 : directions) { + + //create a new instance of Car with default coordinates and velocity 0. + setUp(); + + //variables to save the actual expected result of method nextPosition + int expectedNextPosX = DEFAULT_X; + int expectedNextPosY = DEFAULT_Y; + + //variables to save the acutal expected result of method getVelocity + int expectedVelocityX = 0; + int expectedVelocityY = 0; + + car.accelerate(direction1); + expectedVelocityX += direction1.vector.getX(); + expectedVelocityY += direction1.vector.getY(); + expectedNextPosX += direction1.vector.getX(); + expectedNextPosY += direction1.vector.getY(); + checkVelocity(expectedVelocityX, expectedVelocityY); + checkNextPosition(expectedNextPosX, expectedNextPosY); + + car.move(); + expectedNextPosX += direction1.vector.getX(); + expectedNextPosY += direction1.vector.getY(); + checkVelocity(expectedVelocityX, expectedVelocityY); + checkNextPosition(expectedNextPosX, expectedNextPosY); + + + car.accelerate(direction2); + expectedVelocityX += direction2.vector.getX(); + expectedVelocityY += direction2.vector.getY(); + expectedNextPosX += direction2.vector.getX(); + expectedNextPosY += direction2.vector.getY(); + checkVelocity(expectedVelocityX, expectedVelocityY); + checkNextPosition(expectedNextPosX, expectedNextPosY); + + car.move(); + checkVelocity(expectedVelocityX, expectedVelocityY); + expectedNextPosX += (direction1.vector.getX() + direction2.vector.getX()); + expectedNextPosY += (direction1.vector.getY() + direction2.vector.getY()); + checkNextPosition(expectedNextPosX, expectedNextPosY); + } + } + } + + + /** + * test for methods crash and isCrashed. checks if state crashed is set and returned correctly. + */ + @Test + void crash() { + assertFalse(car.isCrashed()); + car.crash(); + assertTrue(car.isCrashed()); + } + + /** + * test for methods setMoveStrategy. Checks if the MoveStrategy Object is saved and returned correctly + * with all Types of MoveStrategy. + */ + @Test + void MoveStrategy() { + MoveStrategy moveStrategy; + + moveStrategy = new DoNotMoveStrategy(); + car.setMoveStrategy(moveStrategy); + assertEquals(moveStrategy, car.getMoveStrategy()); + + try { + moveStrategy = new MoveListStrategy(".\\moves\\challenge-car-a.txt"); + } catch (FileNotFoundException e) { + Assertions.fail(); + } + car.setMoveStrategy(moveStrategy); + assertEquals(moveStrategy, car.getMoveStrategy()); + + moveStrategy = new PathFollowerMoveStrategy(); + car.setMoveStrategy(moveStrategy); + assertEquals(moveStrategy, car.getMoveStrategy()); + + moveStrategy = new UserMoveStrategy(new UserInterface("Hello"),0,'a'); + car.setMoveStrategy(moveStrategy); + assertEquals(moveStrategy, car.getMoveStrategy()); + } + + /** + * Test for get WinPoints + */ + @Test + void getWinPoints() { + assertEquals(0,car.getWinPoints()); + } + + /** + * Test for increase WinPoints + */ + @Test + void increaseWinPoints() { + car.increaseWinPoints(); + assertEquals(1,car.getWinPoints()); + } + + /** + * Test for deduct WinPoints + */ + @Test + void deductWinPoints() { + car.deductWinPoints(); + assertEquals(-1,car.getWinPoints()); + } + +}