Merge pull request #23 from PM2-IT21bWIN-ruiz-mach-krea/Game

Game
This commit was merged in pull request #23.
This commit is contained in:
romanschenk37
2022-03-20 16:56:33 +01:00
committed by GitHub Enterprise
13 changed files with 574 additions and 47 deletions
@@ -0,0 +1,104 @@
package ch.zhaw.pm2.racetrack;
import ch.zhaw.pm2.racetrack.strategy.UserMoveStrategy;
import org.junit.jupiter.api.*;
import java.io.File;
import static ch.zhaw.pm2.racetrack.Game.NO_WINNER;
class GameTest {
private UserInterface userInterface;
private Game game;
private Track track;
@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)));
}
@Test
void getCurrentCarIndex() {
Assertions.assertEquals(0,game.getCurrentCarIndex());
game.switchToNextActiveCar();
Assertions.assertEquals(1,game.getCurrentCarIndex());
}
@Test
void getCarId() {
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));
}
@Test
void getCarVelocity() {
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());
}
@Test
void onlyOneCarLeft() {
Assertions.assertFalse(game.onlyOneCarLeft());
}
@Test
void carsMoving() {
Assertions.assertTrue(game.carsMoving());
}
}
@Nested
@DisplayName("Basic 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)));
}
@Test
void carTurnCorrect() {
try {
game.doCarTurn(PositionVector.Direction.RIGHT);
Assertions.assertEquals(new PositionVector(1,0),game.getCarVelocity(0));
} catch (PositionVectorNotValid positionVectorNotValid) {
positionVectorNotValid.printStackTrace();
}
}
@Test
void carCrash() {
try {
game.doCarTurn(PositionVector.Direction.UP);
Assertions.assertTrue(game.onlyOneCarLeft());
} catch (PositionVectorNotValid positionVectorNotValid) {
positionVectorNotValid.printStackTrace();
}
}
}
}
@@ -0,0 +1,40 @@
package ch.zhaw.pm2.racetrack;
import ch.zhaw.pm2.racetrack.strategy.MoveListStrategy;
import ch.zhaw.pm2.racetrack.strategy.MoveStrategy;
import org.junit.jupiter.api.*;
import java.io.FileNotFoundException;
public class MoveStrategyTest {
private MoveStrategy moveList;
@Nested
@DisplayName("MoveListStrategy")
class MoveList {
@BeforeEach
void setup() {
try {
moveList = new MoveListStrategy(".\\moves\\challenge-car-a.txt");
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@Test
void checkMove() {
Assertions.assertEquals(PositionVector.Direction.RIGHT,moveList.nextMove());
for (int i = 0; i < 3; i++) {
moveList.nextMove();
}
Assertions.assertEquals(PositionVector.Direction.NONE,moveList.nextMove());
for (int i = 0; i < 40; i++) {
moveList.nextMove();
}
Assertions.assertNull(moveList.nextMove());
}
}
}