dev MoveListStrategy and test
This commit is contained in:
parent
504d5c62ef
commit
21da4feaf5
|
@ -1,10 +1,7 @@
|
||||||
package ch.zhaw.pm2.racetrack;
|
package ch.zhaw.pm2.racetrack;
|
||||||
|
|
||||||
import ch.zhaw.pm2.racetrack.given.GameSpecification;
|
import ch.zhaw.pm2.racetrack.given.GameSpecification;
|
||||||
import ch.zhaw.pm2.racetrack.strategy.DoNotMoveStrategy;
|
import ch.zhaw.pm2.racetrack.strategy.*;
|
||||||
import ch.zhaw.pm2.racetrack.strategy.MoveListStrategy;
|
|
||||||
import ch.zhaw.pm2.racetrack.strategy.PathFollowerMoveStrategy;
|
|
||||||
import ch.zhaw.pm2.racetrack.strategy.UserMoveStrategy;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
@ -33,9 +30,9 @@ public class Game implements GameSpecification {
|
||||||
public boolean initPhase() throws InvalidTrackFormatException, FileNotFoundException {
|
public boolean initPhase() throws InvalidTrackFormatException, FileNotFoundException {
|
||||||
File folder = new File("tracks");
|
File folder = new File("tracks");
|
||||||
File[] listOfFiles = folder.listFiles();
|
File[] listOfFiles = folder.listFiles();
|
||||||
if(listOfFiles.length > 0) {
|
if (listOfFiles.length > 0) {
|
||||||
List<String> tracks = new ArrayList<>();
|
List<String> tracks = new ArrayList<>();
|
||||||
for(File file : listOfFiles){
|
for (File file : listOfFiles) {
|
||||||
tracks.add(file.getName());
|
tracks.add(file.getName());
|
||||||
}
|
}
|
||||||
File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)];
|
File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)];
|
||||||
|
@ -49,7 +46,7 @@ public class Game implements GameSpecification {
|
||||||
moveStrategies.add("User Move Strategy");
|
moveStrategies.add("User Move Strategy");
|
||||||
moveStrategies.add("Move List Strategy");
|
moveStrategies.add("Move List Strategy");
|
||||||
moveStrategies.add("Path Follow Move Strategy");
|
moveStrategies.add("Path Follow Move Strategy");
|
||||||
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) { //TODO: set Movestrategy with method in Track
|
||||||
|
@ -60,7 +57,14 @@ public class Game implements GameSpecification {
|
||||||
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))); //TODO: add Arguments
|
||||||
break;
|
break;
|
||||||
case 3:
|
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) {
|
||||||
|
//TODO: what if not valid
|
||||||
|
}
|
||||||
|
//TODO: Backslash kompatibel für Linux
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments
|
track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments
|
||||||
|
@ -68,8 +72,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 +81,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 +91,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 +102,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 +113,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,13 +124,14 @@ 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();
|
List<Car> cars = track.getCars();
|
||||||
for (Car car: cars) {
|
for (Car car : cars) {
|
||||||
if(car.getWinPoints() == 1){
|
if (car.getWinPoints() == 1) {
|
||||||
return car.getID();
|
return car.getID();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -162,17 +170,16 @@ public class Game implements GameSpecification {
|
||||||
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
|
//TODO: check if Method calculatePath contains endposition
|
||||||
for(PositionVector location : positionList) { //todo: check if order must be reversed
|
for (PositionVector location : positionList) { //todo: check if order must be reversed
|
||||||
if(willCarCrash(currentCarIndex, location)) {
|
if (willCarCrash(currentCarIndex, location)) {
|
||||||
crashPosition = location;
|
crashPosition = location;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(crashPosition != null) {
|
if (crashPosition != null) {
|
||||||
track.carDoesCrash(currentCarIndex, crashPosition);
|
track.carDoesCrash(currentCarIndex, crashPosition);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
track.moveCar(currentCarIndex);
|
track.moveCar(currentCarIndex);
|
||||||
calculateWinner(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition(), currentCarIndex);
|
calculateWinner(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition(), currentCarIndex);
|
||||||
}
|
}
|
||||||
|
@ -183,7 +190,7 @@ public class Game implements GameSpecification {
|
||||||
userInterface.printTrack(track);
|
userInterface.printTrack(track);
|
||||||
Direction direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove();
|
Direction direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove();
|
||||||
doCarTurn(direction);
|
doCarTurn(direction);
|
||||||
if(allCarsCrashed()) {
|
if (allCarsCrashed()) {
|
||||||
return NO_WINNER;
|
return NO_WINNER;
|
||||||
}
|
}
|
||||||
switchToNextActiveCar();
|
switchToNextActiveCar();
|
||||||
|
@ -199,9 +206,8 @@ 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());
|
||||||
// TODO: evtl andere Kapselung
|
// TODO: evtl andere Kapselung
|
||||||
|
@ -215,6 +221,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,20 +251,24 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
int error = distanceFastAxis/2;
|
int error = distanceFastAxis / 2;
|
||||||
for(int step = 0; step < distanceFastAxis; step ++) {
|
for (int step = 0; step < distanceFastAxis; step++) {
|
||||||
error -= distanceSlowAxis;
|
error -= distanceSlowAxis;
|
||||||
if (error < 0) {
|
if (error < 0) {
|
||||||
error += distanceFastAxis; // correct error value to be positive again
|
error += distanceFastAxis; // correct error value to be positive again
|
||||||
|
@ -270,44 +281,40 @@ public class Game implements GameSpecification {
|
||||||
y += parallelStepY;
|
y += parallelStepY;
|
||||||
}
|
}
|
||||||
|
|
||||||
pathList.add(new PositionVector(x,y));
|
pathList.add(new PositionVector(x, y));
|
||||||
}
|
}
|
||||||
return pathList;
|
return pathList;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
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;
|
||||||
|
@ -319,6 +326,7 @@ public class Game implements GameSpecification {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
|
@ -329,8 +337,8 @@ public class Game implements GameSpecification {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean allCarsCrashed() {
|
public boolean allCarsCrashed() {
|
||||||
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;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,44 @@ package ch.zhaw.pm2.racetrack.strategy;
|
||||||
|
|
||||||
import ch.zhaw.pm2.racetrack.PositionVector.Direction;
|
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 {
|
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
|
@Override
|
||||||
public Direction nextMove() {
|
public Direction nextMove() {
|
||||||
// TODO: implementation
|
pointer += 1;
|
||||||
throw new UnsupportedOperationException();
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue