Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
833b122d2b | ||
|
|
837297cd31 | ||
|
|
c30d7a2988 | ||
|
|
21da4feaf5 | ||
|
|
9869e8e74d | ||
|
|
1c618ad09a | ||
|
|
3684b5589e | ||
|
|
fd4513e0d0 |
@@ -1,10 +1,7 @@
|
||||
package ch.zhaw.pm2.racetrack;
|
||||
|
||||
import ch.zhaw.pm2.racetrack.given.GameSpecification;
|
||||
import ch.zhaw.pm2.racetrack.strategy.DoNotMoveStrategy;
|
||||
import ch.zhaw.pm2.racetrack.strategy.MoveListStrategy;
|
||||
import ch.zhaw.pm2.racetrack.strategy.PathFollowerMoveStrategy;
|
||||
import ch.zhaw.pm2.racetrack.strategy.UserMoveStrategy;
|
||||
import ch.zhaw.pm2.racetrack.strategy.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
@@ -50,26 +47,34 @@ public class Game implements GameSpecification {
|
||||
moveStrategies.add("Move List Strategy");
|
||||
moveStrategies.add("Path Follow Move Strategy");
|
||||
for (int i = 0; i < track.getCarCount(); i++) {
|
||||
while(track.getCar(i).getMoveStrategy() == null) {
|
||||
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
|
||||
String path = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt";
|
||||
try {
|
||||
MoveStrategy moveStrategy = new MoveListStrategy(path);
|
||||
track.getCar(i).setMoveStrategy(moveStrategy);
|
||||
} catch (FileNotFoundException e) {
|
||||
userInterface.printInformation("There is no MoveList implemented. Choose another Strategy!");
|
||||
}
|
||||
//TODO: Backslash kompatibel für Linux
|
||||
break;
|
||||
case 4:
|
||||
track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return true;
|
||||
} else {
|
||||
userInterface.printInformation("No Trackfile found!");
|
||||
return false;
|
||||
}
|
||||
@@ -78,6 +83,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 +93,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 +104,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
|
||||
*/
|
||||
@@ -117,14 +125,17 @@ 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
|
||||
public int getWinner() {
|
||||
List<Car> cars = track.getCars();
|
||||
for (Car car: cars) {
|
||||
if(car.getWinPoints() == 1){
|
||||
return car.getID();
|
||||
if (onlyOneCarLeft()) {
|
||||
return currentCarIndex;
|
||||
}
|
||||
for (int i = 0; i < track.getCarCount(); i++) {
|
||||
if (track.getCar(i).getWinPoints() == 1) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return NO_WINNER;
|
||||
@@ -158,36 +169,40 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
public int gamePhase() throws PositionVectorNotValid {
|
||||
while (getWinner() == NO_WINNER) {
|
||||
while (CarsMoving() && getWinner() == NO_WINNER) {
|
||||
userInterface.printTrack(track);
|
||||
Direction direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove();
|
||||
doCarTurn(direction);
|
||||
if(allCarsCrashed()) {
|
||||
return NO_WINNER;
|
||||
Direction direction = null;
|
||||
direction= track.getCar(currentCarIndex).getMoveStrategy().nextMove();
|
||||
if(direction == null) {
|
||||
track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy());
|
||||
direction= track.getCar(currentCarIndex).getMoveStrategy().nextMove();
|
||||
}
|
||||
doCarTurn(direction);
|
||||
switchToNextActiveCar();
|
||||
}
|
||||
userInterface.printTrack(track);
|
||||
return getWinner();
|
||||
}
|
||||
|
||||
@@ -199,8 +214,7 @@ public class Game implements GameSpecification {
|
||||
do {
|
||||
if ((currentCarIndex + 1) == track.getCarCount()) {
|
||||
currentCarIndex = 0;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
currentCarIndex++;
|
||||
}
|
||||
} while (track.getCar(currentCarIndex).isCrashed());
|
||||
@@ -215,6 +229,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 +259,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 +297,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) {
|
||||
if (track.getSpaceType(point) != null)
|
||||
{
|
||||
switch (track.getSpaceType(point)) {
|
||||
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,12 +346,22 @@ public class Game implements GameSpecification {
|
||||
return track.willCrashAtPosition(carIndex, position);
|
||||
}
|
||||
|
||||
public boolean allCarsCrashed() {
|
||||
public boolean onlyOneCarLeft() {
|
||||
int carsLeft = 0;
|
||||
for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) {
|
||||
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 false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ public class Main {
|
||||
|
||||
public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException, PositionVectorNotValid {
|
||||
boolean exit = false;
|
||||
while (!exit) {
|
||||
UserInterface userInterface = new UserInterface("Hello and Welcome");
|
||||
while (!exit) {
|
||||
Game game = new Game(userInterface);
|
||||
int winner = 0;
|
||||
if (game.initPhase()) {
|
||||
@@ -23,6 +23,7 @@ public class Main {
|
||||
exit = true;
|
||||
}
|
||||
}
|
||||
userInterface.printInformation("Thank you and goodbye");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -225,7 +225,11 @@ public class Track implements TrackSpecification {
|
||||
isPositionVectorOnTrack(positionVector);
|
||||
char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX());
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,11 +2,44 @@ package ch.zhaw.pm2.racetrack.strategy;
|
||||
|
||||
import ch.zhaw.pm2.racetrack.PositionVector.Direction;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class MoveListStrategy implements MoveStrategy {
|
||||
|
||||
private List<Direction> moveList;
|
||||
private int pointer;
|
||||
|
||||
public MoveListStrategy(String path) throws FileNotFoundException{
|
||||
moveList = new ArrayList<>();
|
||||
pointer = -1;
|
||||
readFile(new File(path));
|
||||
}
|
||||
|
||||
private void readFile(File trackFile) throws FileNotFoundException {
|
||||
Scanner scanner = new Scanner(new FileInputStream(trackFile), "UTF-8");
|
||||
Direction[] directions = Direction.values();
|
||||
while (scanner.hasNextLine()) {
|
||||
String line = scanner.nextLine();
|
||||
for (Direction dir : directions) {
|
||||
if (dir.toString().equals(line)) {
|
||||
moveList.add(dir);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Direction nextMove() {
|
||||
// TODO: implementation
|
||||
throw new UnsupportedOperationException();
|
||||
pointer += 1;
|
||||
if (pointer < moveList.size()) {
|
||||
return moveList.get(pointer);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package ch.zhaw.pm2.racetrack;
|
||||
|
||||
import ch.zhaw.pm2.racetrack.strategy.MoveListStrategy;
|
||||
import ch.zhaw.pm2.racetrack.strategy.MoveStrategy;
|
||||
import org.junit.jupiter.api.*;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
public class MoveStrategyTest {
|
||||
|
||||
private MoveStrategy moveList;
|
||||
|
||||
@Nested
|
||||
@DisplayName("MoveListStrategy")
|
||||
class MoveList {
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
try {
|
||||
moveList = new MoveListStrategy(".\\moves\\challenge-car-a.txt");
|
||||
}catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkMove() {
|
||||
Assertions.assertEquals(PositionVector.Direction.RIGHT,moveList.nextMove());
|
||||
for (int i = 0; i < 3; i++) {
|
||||
moveList.nextMove();
|
||||
}
|
||||
Assertions.assertEquals(PositionVector.Direction.NONE,moveList.nextMove());
|
||||
for (int i = 0; i < 40; i++) {
|
||||
moveList.nextMove();
|
||||
}
|
||||
Assertions.assertNull(moveList.nextMove());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user