fixes in Game.java
This commit is contained in:
parent
504d5c62ef
commit
fd4513e0d0
|
@ -52,12 +52,12 @@ public class Game implements GameSpecification {
|
|||
for (int i = 0; i < track.getCarCount(); i++) {
|
||||
int moveStrategie = userInterface.selectOption(
|
||||
"Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies);
|
||||
switch (moveStrategie + 1) { //TODO: set Movestrategy with method in Track
|
||||
switch (moveStrategie + 1) {
|
||||
case 1:
|
||||
track.getCar(i).setMoveStrategy(new DoNotMoveStrategy()); //TODO: add Arguments
|
||||
track.getCar(i).setMoveStrategy(new DoNotMoveStrategy());
|
||||
break;
|
||||
case 2:
|
||||
track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i))); //TODO: add Arguments
|
||||
track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i)));
|
||||
break;
|
||||
case 3:
|
||||
track.getCar(i).setMoveStrategy(new MoveListStrategy()); //TODO: add Arguments
|
||||
|
@ -68,8 +68,7 @@ public class Game implements GameSpecification {
|
|||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
userInterface.printInformation("No Trackfile found!");
|
||||
return false;
|
||||
}
|
||||
|
@ -78,6 +77,7 @@ public class Game implements GameSpecification {
|
|||
/**
|
||||
* Return the index of the current active car.
|
||||
* Car indexes are zero-based, so the first car is 0, and the last car is getCarCount() - 1.
|
||||
*
|
||||
* @return The zero-based number of the current car
|
||||
*/
|
||||
@Override
|
||||
|
@ -87,6 +87,7 @@ public class Game implements GameSpecification {
|
|||
|
||||
/**
|
||||
* Get the id of the specified car.
|
||||
*
|
||||
* @param carIndex The zero-based carIndex number
|
||||
* @return A char containing the id of the car
|
||||
*/
|
||||
|
@ -97,6 +98,7 @@ public class Game implements GameSpecification {
|
|||
|
||||
/**
|
||||
* Get the position of the specified car.
|
||||
*
|
||||
* @param carIndex The zero-based carIndex number
|
||||
* @return A PositionVector containing the car's current position
|
||||
*/
|
||||
|
@ -107,6 +109,7 @@ public class Game implements GameSpecification {
|
|||
|
||||
/**
|
||||
* Get the velocity of the specified car.
|
||||
*
|
||||
* @param carIndex The zero-based carIndex number
|
||||
* @return A PositionVector containing the car's current velocity
|
||||
*/
|
||||
|
@ -117,6 +120,7 @@ public class Game implements GameSpecification {
|
|||
|
||||
/**
|
||||
* Return the winner of the game. If the game is still in progress, returns NO_WINNER.
|
||||
*
|
||||
* @return The winning car's index (zero-based, see getCurrentCar()), or NO_WINNER if the game is still in progress
|
||||
*/
|
||||
@Override
|
||||
|
@ -124,7 +128,7 @@ public class Game implements GameSpecification {
|
|||
List<Car> cars = track.getCars();
|
||||
for (Car car : cars) {
|
||||
if (car.getWinPoints() == 1) {
|
||||
return car.getID();
|
||||
return car.getID(); // TODO: Index not ID
|
||||
}
|
||||
}
|
||||
return NO_WINNER;
|
||||
|
@ -158,23 +162,24 @@ public class Game implements GameSpecification {
|
|||
*/
|
||||
@Override
|
||||
public void doCarTurn(Direction acceleration) throws PositionVectorNotValid {
|
||||
// TODO: implementation
|
||||
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;
|
||||
for (int i = 0; i < positionList.size(); i++) {
|
||||
if (willCarCrash(currentCarIndex, positionList.get(i))) {
|
||||
if (i == 0) {
|
||||
crashPosition = track.getCarPos(currentCarIndex);
|
||||
} else {
|
||||
crashPosition = positionList.get(i - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (crashPosition != null) {
|
||||
track.carDoesCrash(currentCarIndex, crashPosition);
|
||||
}
|
||||
else {
|
||||
track.moveCar(currentCarIndex);
|
||||
} else {
|
||||
calculateWinner(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition(), currentCarIndex);
|
||||
track.moveCar(currentCarIndex);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -199,8 +204,7 @@ public class Game implements GameSpecification {
|
|||
do {
|
||||
if ((currentCarIndex + 1) == track.getCarCount()) {
|
||||
currentCarIndex = 0;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
currentCarIndex++;
|
||||
}
|
||||
} while (track.getCar(currentCarIndex).isCrashed());
|
||||
|
@ -215,6 +219,7 @@ public class Game implements GameSpecification {
|
|||
* - Detect which axis of the distance vector is longer (faster movement)
|
||||
* - for each pixel on the 'faster' axis calculate the position on the 'slower' axis.
|
||||
* Direction of the movement has to correctly considered
|
||||
*
|
||||
* @param startPosition Starting position as a PositionVector
|
||||
* @param endPosition Ending position as a PositionVector
|
||||
* @return Intervening grid positions as a List of PositionVector's, including the starting and ending positions.
|
||||
|
@ -244,14 +249,18 @@ public class Game implements GameSpecification {
|
|||
int distanceSlowAxis, distanceFastAxis;
|
||||
if (distX > distY) {
|
||||
// x axis is the 'fast' direction
|
||||
parallelStepX = dirX; parallelStepY = 0; // parallel step only moves in x direction
|
||||
diagonalStepX = dirX; diagonalStepY = dirY; // diagonal step moves in both directions
|
||||
parallelStepX = dirX;
|
||||
parallelStepY = 0; // parallel step only moves in x direction
|
||||
diagonalStepX = dirX;
|
||||
diagonalStepY = dirY; // diagonal step moves in both directions
|
||||
distanceSlowAxis = distY;
|
||||
distanceFastAxis = distX;
|
||||
} else {
|
||||
// y axis is the 'fast' direction
|
||||
parallelStepX = 0; parallelStepY = dirY; // parallel step only moves in y direction
|
||||
diagonalStepX = dirX; diagonalStepY = dirY; // diagonal step moves in both directions
|
||||
parallelStepX = 0;
|
||||
parallelStepY = dirY; // parallel step only moves in y direction
|
||||
diagonalStepX = dirX;
|
||||
diagonalStepY = dirY; // diagonal step moves in both directions
|
||||
distanceSlowAxis = distX;
|
||||
distanceFastAxis = distY;
|
||||
}
|
||||
|
@ -278,43 +287,42 @@ public class Game implements GameSpecification {
|
|||
private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) {
|
||||
List<PositionVector> path = calculatePath(start, finish);
|
||||
for (PositionVector point : path) {
|
||||
switch (track.getSpaceType(point)) {
|
||||
if (track.getSpaceType(point) != null)
|
||||
{
|
||||
switch (track.getSpaceType(point)) { //TODO: Case null
|
||||
case FINISH_UP:
|
||||
if (start.getY() < finish.getY()) {
|
||||
track.getCar(carIndex).increaseWinPoints();
|
||||
}
|
||||
else if(start.getY() > finish.getY()) {
|
||||
} else if (start.getY() > finish.getY()) {
|
||||
track.getCar(carIndex).deductWinPoints();
|
||||
}
|
||||
break;
|
||||
case FINISH_DOWN:
|
||||
if (start.getY() > finish.getY()) {
|
||||
track.getCar(carIndex).increaseWinPoints();
|
||||
}
|
||||
else if (start.getY() < finish.getY()){
|
||||
} else if (start.getY() < finish.getY()) {
|
||||
track.getCar(carIndex).deductWinPoints();
|
||||
}
|
||||
break;
|
||||
case FINISH_RIGHT:
|
||||
if (start.getX() < finish.getX()) {
|
||||
track.getCar(carIndex).increaseWinPoints();
|
||||
}
|
||||
else if (start.getX() > finish.getX()){
|
||||
} else if (start.getX() > finish.getX()) {
|
||||
track.getCar(carIndex).deductWinPoints();
|
||||
}
|
||||
break;
|
||||
case FINISH_LEFT:
|
||||
if (start.getX() > finish.getX()) {
|
||||
track.getCar(carIndex).increaseWinPoints();
|
||||
}
|
||||
else if (start.getX() < finish.getX()){
|
||||
} else if (start.getX() < finish.getX()) {
|
||||
track.getCar(carIndex).increaseWinPoints();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
@ -328,7 +336,7 @@ public class Game implements GameSpecification {
|
|||
return track.willCrashAtPosition(carIndex, position);
|
||||
}
|
||||
|
||||
public boolean allCarsCrashed() {
|
||||
public boolean allCarsCrashed() { //TODO: Finish game when only one car left? or if all cars crashed?
|
||||
for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) {
|
||||
if(! track.getCar(carIndex).isCrashed()) {
|
||||
return false;
|
||||
|
|
Loading…
Reference in New Issue