Game #23

Merged
schrom01 merged 43 commits from Game into main 2022-03-20 16:56:34 +01:00
1 changed files with 38 additions and 11 deletions
Showing only changes of commit 7e5d349f3f - Show all commits

View File

@ -25,12 +25,12 @@ public class Game implements GameSpecification {
UserInterface userInterface;
public Game(String welcometext) {
userInterface = new UserInterface(welcometext);
public Game(UserInterface userInterface) {
this.userInterface = userInterface;
}
public boolean initphase() throws InvalidTrackFormatException, FileNotFoundException {
public boolean initPhase() throws InvalidTrackFormatException, FileNotFoundException {
File folder = new File("tracks");
File[] listOfFiles = folder.listFiles();
if(listOfFiles.length > 0) {
@ -154,7 +154,33 @@ public class Game implements GameSpecification {
@Override
public void doCarTurn(Direction acceleration) {
// TODO: implementation
throw new UnsupportedOperationException();
track.getCar(currentCarIndex).accelerate(acceleration);
PositionVector crashPosition = null;
List<PositionVector> positionList = calculatePath(track.getCarPos(currentCarIndex),track.getCar(currentCarIndex).nextPosition());
//TODO: check if Method calculatePath contains endposition
for(PositionVector location : positionList) { //todo: check if order must be reversed
if(willCarCrash(currentCarIndex, location)) {
crashPosition = location;
}
}
if(crashPosition != null) {
track.carDoesCrash(currentCarIndex, crashPosition);
}
else {
track.getCar(currentCarIndex).move();
}
}
public void gamePhase() {
do{
userInterface.printTrack(track);
doCarTurn(userInterface.selectDirection(currentCarIndex, track.getCarId(currentCarIndex)));
if(getWinner() != NO_WINNER) {
return;
}
} while (!allCarsCrashed());
}
/**
@ -190,7 +216,7 @@ public class Game implements GameSpecification {
int y = startPosition.getY();
// Relative Distance (x & y axis) between end- and starting position
int diffX = endPosition.getX() - startPosition.getY();
int diffX = endPosition.getX() - startPosition.getX();
int diffY = endPosition.getY() - startPosition.getY();
// Absolute distance (x & y axis) between end- and starting position
@ -248,14 +274,15 @@ public class Game implements GameSpecification {
*/
@Override
public boolean willCarCrash(int carIndex, PositionVector position) {
return track.willCrashAtPosition(position); //TODO: add carIndex
}
List<PositionVector> positionList = calculatePath(track.getCarPos(carIndex),position);
for(PositionVector location : positionList) {
if(track.willCrashAtPosition(location)) {
public boolean allCarsCrashed() {
for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) {
if(! track.getCar(carIndex).isCrashed()) {
return false;
}
}
return true;
}
}
return false;
}
}