Game #23
|
@ -49,15 +49,15 @@ 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:
|
||||||
String path = ".\\moves\\ " + selectedTrack.getName().split(".")[0] + "-car-" + track.getCar(i).getID() + ".txt";
|
String path = ".\\moves\\ " + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt";
|
||||||
try {
|
try {
|
||||||
MoveStrategy moveStrategy = new MoveListStrategy(path);
|
MoveStrategy moveStrategy = new MoveListStrategy(path);
|
||||||
track.getCar(i).setMoveStrategy(moveStrategy);
|
track.getCar(i).setMoveStrategy(moveStrategy);
|
||||||
|
@ -113,7 +113,6 @@ 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
|
||||||
*/
|
*/
|
||||||
|
@ -129,10 +128,12 @@ public class Game implements GameSpecification {
|
||||||
*/
|
*/
|
||||||
@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;
|
||||||
|
@ -166,35 +167,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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -289,6 +295,8 @@ 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()) {
|
||||||
|
@ -319,14 +327,14 @@ public class Game implements GameSpecification {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Does indicate if a car would have a crash with a WALL space or another car at the given position.
|
* Does indicate if a car would have a crash with a WALL space or another car at the given position.
|
||||||
*
|
|
||||||
* @param carIndex The zero-based carIndex number
|
* @param carIndex The zero-based carIndex number
|
||||||
* @param position A PositionVector of the possible crash position
|
* @param position A PositionVector of the possible crash position
|
||||||
* @return A boolean indicator if the car would crash with a WALL or another car.
|
* @return A boolean indicator if the car would crash with a WALL or another car.
|
||||||
|
@ -336,12 +344,22 @@ public class Game implements GameSpecification {
|
||||||
return track.willCrashAtPosition(carIndex, position);
|
return track.willCrashAtPosition(carIndex, position);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean allCarsCrashed() {
|
public boolean onlyOneCarLeft() {
|
||||||
for (int carIndex = 0; carIndex < track.getCarCount(); carIndex++) {
|
int carsLeft = 0;
|
||||||
if (!track.getCar(carIndex).isCrashed()) {
|
for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) {
|
||||||
return false;
|
if(! track.getCar(carIndex).isCrashed()) {
|
||||||
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -225,7 +225,11 @@ public class Track implements TrackSpecification {
|
||||||
isPositionVectorOnTrack(positionVector);
|
isPositionVectorOnTrack(positionVector);
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue