Compare commits

...
3 changed files with 129 additions and 90 deletions
+62 -39
View File
@@ -52,12 +52,12 @@ public class Game implements GameSpecification {
for (int i = 0; i < track.getCarCount(); i++) { for (int i = 0; i < track.getCarCount(); i++) {
int moveStrategie = userInterface.selectOption( int moveStrategie = userInterface.selectOption(
"Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies); "Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies);
switch (moveStrategie + 1) { //TODO: set Movestrategy with method in Track switch (moveStrategie + 1) {
case 1: case 1:
track.getCar(i).setMoveStrategy(new DoNotMoveStrategy()); //TODO: add Arguments track.getCar(i).setMoveStrategy(new DoNotMoveStrategy());
break; break;
case 2: 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; break;
case 3: case 3:
track.getCar(i).setMoveStrategy(new MoveListStrategy()); //TODO: add Arguments track.getCar(i).setMoveStrategy(new MoveListStrategy()); //TODO: add Arguments
@@ -68,8 +68,7 @@ public class Game implements GameSpecification {
} }
} }
return true; return true;
} } else {
else{
userInterface.printInformation("No Trackfile found!"); userInterface.printInformation("No Trackfile found!");
return false; return false;
} }
@@ -78,6 +77,7 @@ public class Game implements GameSpecification {
/** /**
* Return the index of the current active car. * 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. * 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 * @return The zero-based number of the current car
*/ */
@Override @Override
@@ -87,6 +87,7 @@ public class Game implements GameSpecification {
/** /**
* Get the id of the specified car. * Get the id of the specified car.
*
* @param carIndex The zero-based carIndex number * @param carIndex The zero-based carIndex number
* @return A char containing the id of the car * @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. * Get the position of the specified car.
*
* @param carIndex The zero-based carIndex number * @param carIndex The zero-based carIndex number
* @return A PositionVector containing the car's current position * @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. * Get the velocity of the specified car.
*
* @param carIndex The zero-based carIndex number * @param carIndex The zero-based carIndex number
* @return A PositionVector containing the car's current velocity * @return A PositionVector containing the car's current velocity
*/ */
@@ -117,14 +120,17 @@ public class Game implements GameSpecification {
/** /**
* Return the winner of the game. If the game is still in progress, returns NO_WINNER. * 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 * @return The winning car's index (zero-based, see getCurrentCar()), or NO_WINNER if the game is still in progress
*/ */
@Override @Override
public int getWinner() { public int getWinner() {
List<Car> cars = track.getCars(); if (onlyOneCarLeft()) {
for (Car car: cars) { return currentCarIndex;
if(car.getWinPoints() == 1){ }
return car.getID(); for (int i = 0; i < track.getCarCount(); i++) {
if (track.getCar(i).getWinPoints() == 1) {
return i;
} }
} }
return NO_WINNER; return NO_WINNER;
@@ -158,36 +164,40 @@ public class Game implements GameSpecification {
*/ */
@Override @Override
public void doCarTurn(Direction acceleration) throws PositionVectorNotValid { public void doCarTurn(Direction acceleration) throws PositionVectorNotValid {
// TODO: implementation
track.getCar(currentCarIndex).accelerate(acceleration); track.getCar(currentCarIndex).accelerate(acceleration);
PositionVector crashPosition = null; PositionVector crashPosition = null;
List<PositionVector> positionList = calculatePath(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition()); List<PositionVector> positionList = calculatePath(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition());
//TODO: check if Method calculatePath contains endposition for (int i = 0; i < positionList.size(); i++) {
for(PositionVector location : positionList) { //todo: check if order must be reversed if (willCarCrash(currentCarIndex, positionList.get(i))) {
if(willCarCrash(currentCarIndex, location)) { if (i == 0) {
crashPosition = location; crashPosition = track.getCarPos(currentCarIndex);
} else {
crashPosition = positionList.get(i - 1);
}
} }
} }
if (crashPosition != null) { if (crashPosition != null) {
track.carDoesCrash(currentCarIndex, crashPosition); track.carDoesCrash(currentCarIndex, crashPosition);
} } else {
else {
track.moveCar(currentCarIndex);
calculateWinner(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition(), currentCarIndex); calculateWinner(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition(), currentCarIndex);
track.moveCar(currentCarIndex);
} }
} }
public int gamePhase() throws PositionVectorNotValid { public int gamePhase() throws PositionVectorNotValid {
while (getWinner() == NO_WINNER) { while (CarsMoving() && getWinner() == NO_WINNER) {
userInterface.printTrack(track); userInterface.printTrack(track);
Direction direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); Direction direction = null;
doCarTurn(direction); direction= track.getCar(currentCarIndex).getMoveStrategy().nextMove();
if(allCarsCrashed()) { if(direction == null) {
return NO_WINNER; track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy());
direction= track.getCar(currentCarIndex).getMoveStrategy().nextMove();
} }
doCarTurn(direction);
switchToNextActiveCar(); switchToNextActiveCar();
} }
userInterface.printTrack(track);
return getWinner(); return getWinner();
} }
@@ -199,8 +209,7 @@ public class Game implements GameSpecification {
do { do {
if ((currentCarIndex + 1) == track.getCarCount()) { if ((currentCarIndex + 1) == track.getCarCount()) {
currentCarIndex = 0; currentCarIndex = 0;
} } else {
else {
currentCarIndex++; currentCarIndex++;
} }
} while (track.getCar(currentCarIndex).isCrashed()); } while (track.getCar(currentCarIndex).isCrashed());
@@ -215,6 +224,7 @@ public class Game implements GameSpecification {
* - Detect which axis of the distance vector is longer (faster movement) * - 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. * - for each pixel on the 'faster' axis calculate the position on the 'slower' axis.
* Direction of the movement has to correctly considered * Direction of the movement has to correctly considered
*
* @param startPosition Starting position as a PositionVector * @param startPosition Starting position as a PositionVector
* @param endPosition Ending 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. * @return Intervening grid positions as a List of PositionVector's, including the starting and ending positions.
@@ -244,14 +254,18 @@ public class Game implements GameSpecification {
int distanceSlowAxis, distanceFastAxis; int distanceSlowAxis, distanceFastAxis;
if (distX > distY) { if (distX > distY) {
// x axis is the 'fast' direction // x axis is the 'fast' direction
parallelStepX = dirX; parallelStepY = 0; // parallel step only moves in x direction parallelStepX = dirX;
diagonalStepX = dirX; diagonalStepY = dirY; // diagonal step moves in both directions parallelStepY = 0; // parallel step only moves in x direction
diagonalStepX = dirX;
diagonalStepY = dirY; // diagonal step moves in both directions
distanceSlowAxis = distY; distanceSlowAxis = distY;
distanceFastAxis = distX; distanceFastAxis = distX;
} else { } else {
// y axis is the 'fast' direction // y axis is the 'fast' direction
parallelStepX = 0; parallelStepY = dirY; // parallel step only moves in y direction parallelStepX = 0;
diagonalStepX = dirX; diagonalStepY = dirY; // diagonal step moves in both directions parallelStepY = dirY; // parallel step only moves in y direction
diagonalStepX = dirX;
diagonalStepY = dirY; // diagonal step moves in both directions
distanceSlowAxis = distX; distanceSlowAxis = distX;
distanceFastAxis = distY; distanceFastAxis = distY;
} }
@@ -278,43 +292,42 @@ public class Game implements GameSpecification {
private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) { private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) {
List<PositionVector> path = calculatePath(start, finish); List<PositionVector> path = calculatePath(start, finish);
for (PositionVector point : path) { for (PositionVector point : path) {
if (track.getSpaceType(point) != null)
{
switch (track.getSpaceType(point)) { switch (track.getSpaceType(point)) {
case FINISH_UP: case FINISH_UP:
if (start.getY() < finish.getY()) { if (start.getY() < finish.getY()) {
track.getCar(carIndex).increaseWinPoints(); track.getCar(carIndex).increaseWinPoints();
} } else if (start.getY() > finish.getY()) {
else if(start.getY() > finish.getY()) {
track.getCar(carIndex).deductWinPoints(); track.getCar(carIndex).deductWinPoints();
} }
break; break;
case FINISH_DOWN: case FINISH_DOWN:
if (start.getY() > finish.getY()) { if (start.getY() > finish.getY()) {
track.getCar(carIndex).increaseWinPoints(); track.getCar(carIndex).increaseWinPoints();
} } else if (start.getY() < finish.getY()) {
else if (start.getY() < finish.getY()){
track.getCar(carIndex).deductWinPoints(); track.getCar(carIndex).deductWinPoints();
} }
break; break;
case FINISH_RIGHT: case FINISH_RIGHT:
if (start.getX() < finish.getX()) { if (start.getX() < finish.getX()) {
track.getCar(carIndex).increaseWinPoints(); track.getCar(carIndex).increaseWinPoints();
} } else if (start.getX() > finish.getX()) {
else if (start.getX() > finish.getX()){
track.getCar(carIndex).deductWinPoints(); track.getCar(carIndex).deductWinPoints();
} }
break; break;
case FINISH_LEFT: case FINISH_LEFT:
if (start.getX() > finish.getX()) { if (start.getX() > finish.getX()) {
track.getCar(carIndex).increaseWinPoints(); track.getCar(carIndex).increaseWinPoints();
} } else if (start.getX() < finish.getX()) {
else if (start.getX() < finish.getX()){
track.getCar(carIndex).increaseWinPoints(); track.getCar(carIndex).increaseWinPoints();
} }
break; break;
} }
}
} }
}
}
/** /**
@@ -328,12 +341,22 @@ public class Game implements GameSpecification {
return track.willCrashAtPosition(carIndex, position); return track.willCrashAtPosition(carIndex, position);
} }
public boolean allCarsCrashed() { public boolean onlyOneCarLeft() {
int carsLeft = 0;
for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) { for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) {
if(! track.getCar(carIndex).isCrashed()) { if(! track.getCar(carIndex).isCrashed()) {
return false; carsLeft++;
} }
} }
return !(carsLeft > 1);
}
public boolean CarsMoving() {
for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) {
if(! (track.getCar(carIndex).isCrashed() || track.getCar(carIndex).getMoveStrategy().getClass() == DoNotMoveStrategy.class)) {
return true; return true;
} }
} }
return false;
}
}
@@ -8,8 +8,8 @@ public class Main {
public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException, PositionVectorNotValid { public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException, PositionVectorNotValid {
boolean exit = false; boolean exit = false;
while (!exit) {
UserInterface userInterface = new UserInterface("Hello and Welcome"); UserInterface userInterface = new UserInterface("Hello and Welcome");
while (!exit) {
Game game = new Game(userInterface); Game game = new Game(userInterface);
int winner = 0; int winner = 0;
if (game.initPhase()) { if (game.initPhase()) {
@@ -23,6 +23,7 @@ public class Main {
exit = true; exit = true;
} }
} }
userInterface.printInformation("Thank you and goodbye");
} }
} }
+27 -12
View File
@@ -60,6 +60,7 @@ public class Track implements TrackSpecification {
private List<String> track; private List<String> track;
private List<Car> cars; private List<Car> cars;
private final List<PositionVector> finishLine; private final List<PositionVector> finishLine;
private ConfigSpecification.SpaceType finishTyp;
/** /**
* Initialize a Track from the given track file. * Initialize a Track from the given track file.
@@ -132,7 +133,7 @@ public class Track implements TrackSpecification {
if (finishLine.size() == 0) { if (finishLine.size() == 0) {
throw new InvalidTrackFormatException(); throw new InvalidTrackFormatException();
} }
ConfigSpecification.SpaceType finishTyp = getSpaceType(finishLine.get(0)); finishTyp = getSpaceType(finishLine.get(0));
for (PositionVector positionVector : finishLine) { for (PositionVector positionVector : finishLine) {
if (getSpaceType(positionVector) != finishTyp) { if (getSpaceType(positionVector) != finishTyp) {
throw new InvalidTrackFormatException(); throw new InvalidTrackFormatException();
@@ -207,12 +208,20 @@ public class Track implements TrackSpecification {
* @param carIndex of the current car * @param carIndex of the current car
*/ */
private void makeCarMoveInTrack(int carIndex) { private void makeCarMoveInTrack(int carIndex) {
PositionVector positionVector = findChar(getCarId(carIndex)); PositionVector carPositionVector = findChar(getCarId(carIndex));
//Removes the Car at Current Pos //Removes the Car at Current Pos
drawCharOnTrackIndicator(positionVector, ConfigSpecification.SpaceType.TRACK.getValue()); drawCharOnTrackIndicator(carPositionVector, ConfigSpecification.SpaceType.TRACK.getValue());
//Redraw finishline if Car was on finish-line Position
for(PositionVector finishLinePositionVector : finishLine){
if(finishLinePositionVector.equals(carPositionVector)){
drawCharOnTrackIndicator(carPositionVector, finishTyp.getValue());
}
}
//Adds Car at new Position //Adds Car at new Position
positionVector = cars.get(carIndex).nextPosition(); carPositionVector = cars.get(carIndex).nextPosition();
drawCharOnTrackIndicator(positionVector, cars.get(carIndex).getID()); drawCharOnTrackIndicator(carPositionVector, cars.get(carIndex).getID());
} }
/** /**
@@ -222,24 +231,30 @@ public class Track implements TrackSpecification {
* @return true if car would crash. Else false. * @return true if car would crash. Else false.
*/ */
public boolean willCrashAtPosition(int carIndex, PositionVector positionVector) throws PositionVectorNotValid { public boolean willCrashAtPosition(int carIndex, PositionVector positionVector) throws PositionVectorNotValid {
isPositionVectorOnTrack(positionVector); isPositionVectorOnTrack(positionVector); //TODO: remove this line? Or Method?
char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX()); char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX());
if (getCarId(carIndex) == charAtPosition) return false; if (getCarId(carIndex) == charAtPosition) return false;
return (charAtPosition == ConfigSpecification.SpaceType.WALL.value); return !(charAtPosition == ConfigSpecification.SpaceType.TRACK.value ||
charAtPosition == ConfigSpecification.SpaceType.FINISH_RIGHT.value ||
charAtPosition == ConfigSpecification.SpaceType.FINISH_LEFT.value ||
charAtPosition == ConfigSpecification.SpaceType.FINISH_UP.value ||
charAtPosition == ConfigSpecification.SpaceType.FINISH_DOWN.value);
} }
/** /**
* This Method will make the Car Crash. In Track and in the Car Object * This Method will make the Car Crash. In Track and in the Car Object
* *
* @param carIndex representing current Car * @param carIndex representing current Car
* @param positionVector where the Crash did happen * @param crashPositionVector where the Crash did happen
*/ */
public void carDoesCrash(int carIndex, PositionVector positionVector) throws PositionVectorNotValid{ public void carDoesCrash(int carIndex, PositionVector crashPositionVector) throws PositionVectorNotValid{
isPositionVectorOnTrack(positionVector); isPositionVectorOnTrack(crashPositionVector); //TODO: remove this line? and Method?
PositionVector currentCarPosition = getCarPos(carIndex);
drawCharOnTrackIndicator(new PositionVector(currentCarPosition.getX(), currentCarPosition.getY()), ConfigSpecification.SpaceType.TRACK.getValue());
Car car = cars.get(carIndex); Car car = cars.get(carIndex);
car.crash(); car.crash();
car.setPosition(positionVector); car.setPosition(crashPositionVector);
drawCharOnTrackIndicator(new PositionVector(positionVector.getX(), positionVector.getY()), CRASH_INDICATOR); drawCharOnTrackIndicator(new PositionVector(crashPositionVector.getX(), crashPositionVector.getY()), CRASH_INDICATOR);
} }
/** /**