changes in Methods in Game.java
This commit is contained in:
parent
e159274085
commit
7e5d349f3f
|
@ -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)) {
|
||||
return true;
|
||||
public boolean allCarsCrashed() {
|
||||
for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) {
|
||||
if(! track.getCar(carIndex).isCrashed()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue