From 8877f1476f826cf5f4b11b6aefeeeb9b354b4524 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Tue, 22 Mar 2022 18:47:14 +0100 Subject: [PATCH 1/3] GameTest --- .../java/ch/zhaw/pm2/racetrack/GameTest.java | 129 ++++++++++++++++-- 1 file changed, 114 insertions(+), 15 deletions(-) diff --git a/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java index 03cb0f3..ebfb473 100644 --- a/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java +++ b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java @@ -1,11 +1,14 @@ package ch.zhaw.pm2.racetrack; import ch.zhaw.pm2.racetrack.strategy.UserMoveStrategy; +import org.junit.Before; import org.junit.jupiter.api.*; import java.io.File; +import java.util.List; import static ch.zhaw.pm2.racetrack.Game.NO_WINNER; +import static ch.zhaw.pm2.racetrack.PositionVector.Direction.*; class GameTest { private UserInterface userInterface; @@ -20,40 +23,40 @@ class GameTest { userInterface = new UserInterface("Test"); game = new Game(userInterface); track = game.selectTrack(new File(".\\tracks\\challenge.txt")); - game.selectMoveStrategy(track.getCar(0),new UserMoveStrategy(new UserInterface("Testing"),0, track.getCarId(0))); - game.selectMoveStrategy(track.getCar(1),new UserMoveStrategy(new UserInterface("Testing"),1, track.getCarId(1))); + game.selectMoveStrategy(track.getCar(0), new UserMoveStrategy(new UserInterface("Testing"), 0, track.getCarId(0))); + game.selectMoveStrategy(track.getCar(1), new UserMoveStrategy(new UserInterface("Testing"), 1, track.getCarId(1))); } @Test void getCurrentCarIndex() { - Assertions.assertEquals(0,game.getCurrentCarIndex()); + Assertions.assertEquals(0, game.getCurrentCarIndex()); game.switchToNextActiveCar(); - Assertions.assertEquals(1,game.getCurrentCarIndex()); + Assertions.assertEquals(1, game.getCurrentCarIndex()); } @Test void getCarId() { - Assertions.assertEquals('a',game.getCarId(0)); - Assertions.assertEquals('b',game.getCarId(1)); + Assertions.assertEquals('a', game.getCarId(0)); + Assertions.assertEquals('b', game.getCarId(1)); } @Test void getCarPosition() { - Assertions.assertEquals(new PositionVector(24,22),game.getCarPosition(0)); - Assertions.assertEquals(new PositionVector(24,24),game.getCarPosition(1)); + Assertions.assertEquals(new PositionVector(24, 22), game.getCarPosition(0)); + Assertions.assertEquals(new PositionVector(24, 24), game.getCarPosition(1)); } @Test void getCarVelocity() { - Assertions.assertEquals(new PositionVector(0,0),game.getCarVelocity(0)); - Assertions.assertEquals(new PositionVector(0,0),game.getCarVelocity(1)); + Assertions.assertEquals(new PositionVector(0, 0), game.getCarVelocity(0)); + Assertions.assertEquals(new PositionVector(0, 0), game.getCarVelocity(1)); } @Test void getWinner() { - Assertions.assertEquals(NO_WINNER,game.getWinner()); + Assertions.assertEquals(NO_WINNER, game.getWinner()); } @Test @@ -75,16 +78,16 @@ class GameTest { userInterface = new UserInterface("Test"); game = new Game(userInterface); track = game.selectTrack(new File(".\\tracks\\challenge.txt")); - game.selectMoveStrategy(track.getCar(0),new UserMoveStrategy(new UserInterface("Testing"),0, track.getCarId(0))); - game.selectMoveStrategy(track.getCar(1),new UserMoveStrategy(new UserInterface("Testing"),1, track.getCarId(1))); + game.selectMoveStrategy(track.getCar(0), new UserMoveStrategy(new UserInterface("Testing"), 0, track.getCarId(0))); + game.selectMoveStrategy(track.getCar(1), new UserMoveStrategy(new UserInterface("Testing"), 1, track.getCarId(1))); } @Test void carTurnCorrect() { try { - game.doCarTurn(PositionVector.Direction.RIGHT); - Assertions.assertEquals(new PositionVector(1,0),game.getCarVelocity(0)); + game.doCarTurn(RIGHT); + Assertions.assertEquals(new PositionVector(1, 0), game.getCarVelocity(0)); } catch (PositionVectorNotValid positionVectorNotValid) { positionVectorNotValid.printStackTrace(); } @@ -99,6 +102,102 @@ class GameTest { positionVectorNotValid.printStackTrace(); } } + } + + @Nested + @DisplayName("Playtrough") + class Play { + private Game game; + + @Test + void winner() { + game = new Game(new interFace("Test")); + try { + game.initPhase(); + Assertions.assertEquals('a',game.gamePhase()); + } catch (InvalidTrackFormatException | PositionVectorNotValid e) { + e.printStackTrace(); + } + } + + + + } + + private class interFace extends UserInterface { + + private static PositionVector.Direction[] directions = {RIGHT, + RIGHT, + RIGHT, + NONE, + NONE, + NONE, + NONE, + UP, + LEFT, + LEFT, + LEFT, + LEFT, + UP_LEFT, + NONE, + RIGHT, + RIGHT, + RIGHT, + NONE, + LEFT, + DOWN_LEFT, + DOWN_LEFT, + LEFT, + LEFT, + NONE, + RIGHT, + NONE, + DOWN, + DOWN, + RIGHT, + NONE, + RIGHT, + DOWN, + NONE, + UP_RIGHT, + RIGHT, + UP_RIGHT, + UP_RIGHT, + RIGHT, + RIGHT}; + + private static int pointer = -1; + + public interFace(String welcometxt) { + super(welcometxt); + } + + @Override + public int selectOption(String text, List options) { + if (text.equals("Select Track file")) { + return 1; + } else if (text.contains("Select Strategy for Car")) { + return 2; + } + return 0; + } + + public void printInformation(String text) { + } + + public void printTrack(Track track) { + } + + public void quit(String text) { + } + + public PositionVector.Direction selectDirection(int playingCarIndex, char playingCarID) { + pointer += 1; + if(pointer < directions.length) { + return directions[pointer]; + } + return NONE; + } } } From b1a4f3b3dbe5c8244e36653055d40155d1dfae4f Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Wed, 23 Mar 2022 08:53:40 +0100 Subject: [PATCH 2/3] -CarTest added winpoint test -added Klassendiagramm -create Constructor InvalidTrackFormatException --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 2 +- .../java/ch/zhaw/pm2/racetrack/GameTest.java | 164 +++++++++++------- 2 files changed, 98 insertions(+), 68 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 413fef3..44bf317 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -222,7 +222,7 @@ public class Game implements GameSpecification { direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); if (direction == null) { track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy()); - direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); //TODO: Entfernen? + track.getCar(currentCarIndex).getMoveStrategy().nextMove(); }else { doCarTurn(direction); } diff --git a/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java index ebfb473..0c1dea3 100644 --- a/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java +++ b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java @@ -1,39 +1,51 @@ package ch.zhaw.pm2.racetrack; import ch.zhaw.pm2.racetrack.strategy.UserMoveStrategy; -import org.junit.Before; -import org.junit.jupiter.api.*; - +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; import java.io.File; import java.util.List; - import static ch.zhaw.pm2.racetrack.Game.NO_WINNER; import static ch.zhaw.pm2.racetrack.PositionVector.Direction.*; + +/** + * Test for Class Game + */ class GameTest { private UserInterface userInterface; private Game game; private Track track; + private String TRACK_FILE_PATH = ".\\tracks\\challenge.txt"; + private int CAR_INDEX_ONE = 0; + private int CAR_INDEX_TWO = 1; + + /** + * This nested Class tests if the game gets initiatet correctly + */ @Nested @DisplayName("Test correct Setup") class Setup { + @BeforeEach void setup() { userInterface = new UserInterface("Test"); game = new Game(userInterface); - track = game.selectTrack(new File(".\\tracks\\challenge.txt")); - game.selectMoveStrategy(track.getCar(0), new UserMoveStrategy(new UserInterface("Testing"), 0, track.getCarId(0))); - game.selectMoveStrategy(track.getCar(1), new UserMoveStrategy(new UserInterface("Testing"), 1, track.getCarId(1))); + track = game.selectTrack(new File(TRACK_FILE_PATH)); + game.selectMoveStrategy(track.getCar(CAR_INDEX_ONE), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_ONE, track.getCarId(CAR_INDEX_ONE))); + game.selectMoveStrategy(track.getCar(CAR_INDEX_TWO), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_TWO, track.getCarId(CAR_INDEX_TWO))); } - @Test void getCurrentCarIndex() { - Assertions.assertEquals(0, game.getCurrentCarIndex()); + Assertions.assertEquals(CAR_INDEX_ONE, game.getCurrentCarIndex()); game.switchToNextActiveCar(); - Assertions.assertEquals(1, game.getCurrentCarIndex()); + Assertions.assertEquals(CAR_INDEX_TWO, game.getCurrentCarIndex()); } @Test @@ -70,16 +82,20 @@ class GameTest { } } + /** + * This nested Class makes basic manipulation after Game init. + */ @Nested @DisplayName("Basic manipulation") - class manipulation { + class Manipulation { + @BeforeEach void setup() { userInterface = new UserInterface("Test"); game = new Game(userInterface); - track = game.selectTrack(new File(".\\tracks\\challenge.txt")); - game.selectMoveStrategy(track.getCar(0), new UserMoveStrategy(new UserInterface("Testing"), 0, track.getCarId(0))); - game.selectMoveStrategy(track.getCar(1), new UserMoveStrategy(new UserInterface("Testing"), 1, track.getCarId(1))); + track = game.selectTrack(new File(TRACK_FILE_PATH)); + game.selectMoveStrategy(track.getCar(CAR_INDEX_ONE), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_ONE, track.getCarId(CAR_INDEX_ONE))); + game.selectMoveStrategy(track.getCar(CAR_INDEX_TWO), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_TWO, track.getCarId(CAR_INDEX_TWO))); } @@ -104,6 +120,9 @@ class GameTest { } } + /** + * This nested Class tests a Playtrough. And implements a UserInterface interagtion to pretend a real player + */ @Nested @DisplayName("Playtrough") class Play { @@ -111,75 +130,86 @@ class GameTest { @Test void winner() { - game = new Game(new interFace("Test")); + game = new Game(new interFace("Test",new Integer[]{0,2,1},new PositionVector.Direction[]{RIGHT, + RIGHT, + RIGHT, + NONE, + NONE, + NONE, + NONE, + UP, + LEFT, + LEFT, + LEFT, + LEFT, + UP_LEFT, + NONE, + RIGHT, + RIGHT, + RIGHT, + NONE, + LEFT, + DOWN_LEFT, + DOWN_LEFT, + LEFT, + LEFT, + NONE, + RIGHT, + NONE, + DOWN, + DOWN, + RIGHT, + NONE, + RIGHT, + DOWN, + NONE, + UP_RIGHT, + RIGHT, + UP_RIGHT, + UP_RIGHT, + RIGHT, + RIGHT})); try { game.initPhase(); - Assertions.assertEquals('a',game.gamePhase()); + Assertions.assertEquals("a",game.gamePhase()); } catch (InvalidTrackFormatException | PositionVectorNotValid e) { e.printStackTrace(); } } - + @Test + void crashA() { + game = new Game(new interFace("Test",new Integer[]{0,2,2},new PositionVector.Direction[]{UP})); + try { + game.initPhase(); + Assertions.assertEquals("b",game.gamePhase()); + } catch (InvalidTrackFormatException | PositionVectorNotValid e) { + e.printStackTrace(); + } + } } private class interFace extends UserInterface { - private static PositionVector.Direction[] directions = {RIGHT, - RIGHT, - RIGHT, - NONE, - NONE, - NONE, - NONE, - UP, - LEFT, - LEFT, - LEFT, - LEFT, - UP_LEFT, - NONE, - RIGHT, - RIGHT, - RIGHT, - NONE, - LEFT, - DOWN_LEFT, - DOWN_LEFT, - LEFT, - LEFT, - NONE, - RIGHT, - NONE, - DOWN, - DOWN, - RIGHT, - NONE, - RIGHT, - DOWN, - NONE, - UP_RIGHT, - RIGHT, - UP_RIGHT, - UP_RIGHT, - RIGHT, - RIGHT}; + private final PositionVector.Direction[] directions; + private final Integer[] instructions; + private int pointerDir,pointerInstruction; - private static int pointer = -1; - public interFace(String welcometxt) { + public interFace(String welcometxt, Integer[] instructions, PositionVector.Direction[] directions) { super(welcometxt); + pointerDir = -1; + pointerInstruction = -1; + this.instructions = instructions; + this.directions = directions; } @Override public int selectOption(String text, List options) { - if (text.equals("Select Track file")) { - return 1; - } else if (text.contains("Select Strategy for Car")) { - return 2; - } - return 0; + pointerInstruction++; + return instructions[pointerInstruction]; + } public void printInformation(String text) { @@ -192,9 +222,9 @@ class GameTest { } public PositionVector.Direction selectDirection(int playingCarIndex, char playingCarID) { - pointer += 1; - if(pointer < directions.length) { - return directions[pointer]; + pointerDir += 1; + if(pointerDir < directions.length) { + return directions[pointerDir]; } return NONE; } From 826f67d125320c326cfa7d11c65a682dcad0836e Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Wed, 23 Mar 2022 09:10:51 +0100 Subject: [PATCH 3/3] gamePhase() prevent null direction --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 44bf317..fcfd270 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -222,10 +222,9 @@ public class Game implements GameSpecification { direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); if (direction == null) { track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy()); - track.getCar(currentCarIndex).getMoveStrategy().nextMove(); - }else { - doCarTurn(direction); + direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); } + doCarTurn(direction); switchToNextActiveCar(); } userInterface.printTrack(track);