implemented PathFollowerMoveStrategy.java
This commit is contained in:
		
							parent
							
								
									b467a79596
								
							
						
					
					
						commit
						eddf9d3daa
					
				| 
						 | 
					@ -0,0 +1,19 @@
 | 
				
			||||||
 | 
					(X:40, Y:22)
 | 
				
			||||||
 | 
					(X:43, Y:22)
 | 
				
			||||||
 | 
					(X:46, Y:21)
 | 
				
			||||||
 | 
					(X:48, Y:19)
 | 
				
			||||||
 | 
					(X:48, Y:17)
 | 
				
			||||||
 | 
					(X:46, Y:15)
 | 
				
			||||||
 | 
					(X:41, Y:13)
 | 
				
			||||||
 | 
					(X:41, Y:10)
 | 
				
			||||||
 | 
					(X:46, Y:9)
 | 
				
			||||||
 | 
					(X:49, Y:4)
 | 
				
			||||||
 | 
					(X:40, Y:2)
 | 
				
			||||||
 | 
					(X:30, Y:2)
 | 
				
			||||||
 | 
					(X:21, Y:3)
 | 
				
			||||||
 | 
					(X:16, Y:7)
 | 
				
			||||||
 | 
					(X:13, Y:10)
 | 
				
			||||||
 | 
					(X:14, Y:14)
 | 
				
			||||||
 | 
					(X:11, Y:19)
 | 
				
			||||||
 | 
					(X:13, Y:22)
 | 
				
			||||||
 | 
					(X:24, Y:22)
 | 
				
			||||||
| 
						 | 
					@ -44,32 +44,38 @@ public class Game implements GameSpecification {
 | 
				
			||||||
            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++) {
 | 
				
			||||||
                Car car = track.getCar(i);
 | 
					                Car car = track.getCar(i);
 | 
				
			||||||
                while (car.getMoveStrategy() == null) {
 | 
					                MoveStrategy moveStrategy = null;
 | 
				
			||||||
 | 
					                while (moveStrategy == null) {
 | 
				
			||||||
 | 
					                    String filePath;
 | 
				
			||||||
                    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) {
 | 
					                    switch (moveStrategie + 1) {
 | 
				
			||||||
                        case 1:
 | 
					                        case 1:
 | 
				
			||||||
                            selectMoveStrategy(car, new DoNotMoveStrategy());
 | 
					                            moveStrategy = new DoNotMoveStrategy();
 | 
				
			||||||
                            break;
 | 
					                            break;
 | 
				
			||||||
                        case 2:
 | 
					                        case 2:
 | 
				
			||||||
                            selectMoveStrategy(car, new UserMoveStrategy(userInterface, i, track.getCarId(i)));
 | 
					                            moveStrategy = 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";
 | 
					                            filePath = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt";
 | 
				
			||||||
                            try {
 | 
					                            try {
 | 
				
			||||||
                                MoveStrategy moveStrategy = new MoveListStrategy(path);
 | 
					                                moveStrategy = new MoveListStrategy(filePath);
 | 
				
			||||||
                                selectMoveStrategy(car, moveStrategy);
 | 
					 | 
				
			||||||
                            } catch (FileNotFoundException e) {
 | 
					                            } catch (FileNotFoundException e) {
 | 
				
			||||||
                                userInterface.printInformation("There is no MoveList implemented. Choose another Strategy!");
 | 
					                                userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!");
 | 
				
			||||||
                            }
 | 
					                            }
 | 
				
			||||||
                            //TODO: Backslash kompatibel für Linux
 | 
					                            //TODO: Backslash kompatibel für Linux
 | 
				
			||||||
                            break;
 | 
					                            break;
 | 
				
			||||||
                        case 4:
 | 
					                        case 4:
 | 
				
			||||||
                            //TODO: add Arguments
 | 
					                            filePath = ".\\follower\\" + selectedTrack.getName().split("\\.")[0] + "_points.txt";
 | 
				
			||||||
                            selectMoveStrategy(car, new PathFollowerMoveStrategy());
 | 
					                            try {
 | 
				
			||||||
 | 
					                                moveStrategy = new PathFollowerMoveStrategy(filePath, track.getCarPos(i));
 | 
				
			||||||
 | 
					                            } catch (FileNotFoundException e) {
 | 
				
			||||||
 | 
					                                userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!");
 | 
				
			||||||
 | 
					                            }
 | 
				
			||||||
                            break;
 | 
					                            break;
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					                selectMoveStrategy(car, moveStrategy);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            return true;
 | 
					            return true;
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,14 +20,14 @@ public class MoveListStrategy implements MoveStrategy {
 | 
				
			||||||
        readFile(new File(path));
 | 
					        readFile(new File(path));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void readFile(File trackFile) throws FileNotFoundException {
 | 
					    protected void readFile(File trackFile) throws FileNotFoundException {
 | 
				
			||||||
        Scanner scanner = new Scanner(new FileInputStream(trackFile), "UTF-8");
 | 
					        Scanner scanner = new Scanner(new FileInputStream(trackFile), "UTF-8");
 | 
				
			||||||
        Direction[] directions = Direction.values();
 | 
					        Direction[] directions = Direction.values();
 | 
				
			||||||
        while (scanner.hasNextLine()) {
 | 
					        while (scanner.hasNextLine()) {
 | 
				
			||||||
            String line = scanner.nextLine();
 | 
					            String line = scanner.nextLine();
 | 
				
			||||||
            for (Direction dir : directions) {
 | 
					            for (Direction direction : directions) {
 | 
				
			||||||
                if (dir.toString().equals(line)) {
 | 
					                if (direction.toString().equals(line)) {
 | 
				
			||||||
                    moveList.add(dir);
 | 
					                    moveList.add(direction);
 | 
				
			||||||
                    break;
 | 
					                    break;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,15 +1,104 @@
 | 
				
			||||||
package ch.zhaw.pm2.racetrack.strategy;
 | 
					package ch.zhaw.pm2.racetrack.strategy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import ch.zhaw.pm2.racetrack.PositionVector;
 | 
				
			||||||
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.Scanner;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * The PathFollowerMoveStrategy class determines the next move based on a file containing points on a path.
 | 
					 * The PathFollowerMoveStrategy class determines the next move based on a file containing points on a path.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class PathFollowerMoveStrategy implements MoveStrategy {
 | 
					public class PathFollowerMoveStrategy implements MoveStrategy {
 | 
				
			||||||
 | 
					    private PositionVector currentPosition;
 | 
				
			||||||
 | 
					    private PositionVector currentVelocity;
 | 
				
			||||||
 | 
					    private ArrayList<PositionVector> pointList;
 | 
				
			||||||
 | 
					    private int pointer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public PathFollowerMoveStrategy(String path, PositionVector startPosition) throws FileNotFoundException {
 | 
				
			||||||
 | 
					        pointList = new ArrayList<>();
 | 
				
			||||||
 | 
					        pointer = 0;
 | 
				
			||||||
 | 
					        readFile(new File(path));
 | 
				
			||||||
 | 
					        currentPosition = startPosition;
 | 
				
			||||||
 | 
					        currentVelocity = new PositionVector(0, 0);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void readFile(File trackFile) throws FileNotFoundException {
 | 
				
			||||||
 | 
					        Scanner scanner = new Scanner(new FileInputStream(trackFile), "UTF-8");
 | 
				
			||||||
 | 
					        while (scanner.hasNextLine()) {
 | 
				
			||||||
 | 
					            String line = scanner.nextLine();
 | 
				
			||||||
 | 
					            String[] coordinates = line.split("(\\(X:|, Y:|\\))");
 | 
				
			||||||
 | 
					            pointList.add(new PositionVector(Integer.parseInt(coordinates[1]), Integer.parseInt(coordinates[2])));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public Direction nextMove() {
 | 
					    public Direction nextMove() {
 | 
				
			||||||
        // TODO: implementation
 | 
					        if (pointer >= pointList.size()) {
 | 
				
			||||||
        throw new UnsupportedOperationException();
 | 
					            return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        int accelerationX = 0;
 | 
				
			||||||
 | 
					        int accelerationY = 0;
 | 
				
			||||||
 | 
					        if (pointList.get(pointer).equals(currentPosition)) {
 | 
				
			||||||
 | 
					            pointer ++;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if(currentVelocity.getX() > 0){
 | 
				
			||||||
 | 
					            accelerationX = -1;
 | 
				
			||||||
 | 
					        } else if(currentVelocity.getX() < 0) {
 | 
				
			||||||
 | 
					            accelerationX = 1;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if(currentVelocity.getY() > 0){
 | 
				
			||||||
 | 
					            accelerationY = -1;
 | 
				
			||||||
 | 
					        } else if(currentVelocity.getY() < 0) {
 | 
				
			||||||
 | 
					            accelerationY = 1;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        PositionVector movementVector = new PositionVector(pointList.get(pointer).getX() - currentPosition.getX(), pointList.get(pointer).getY() - currentPosition.getY());
 | 
				
			||||||
 | 
					        if(movementVector.getX() > 0 && movementVector.getX()/2.0 > currentVelocity.getX()) {
 | 
				
			||||||
 | 
					            accelerationX = 1;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else if(movementVector.getX() > 0 && movementVector.getX()/2.0 <= currentVelocity.getX()) {
 | 
				
			||||||
 | 
					            accelerationX = -1;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else if(movementVector.getX() < 0 && movementVector.getX()/2.0 < currentVelocity.getX()) {
 | 
				
			||||||
 | 
					            accelerationX = -1;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else if(movementVector.getX() < 0 && movementVector.getX()/2.0 >= currentVelocity.getX()) {
 | 
				
			||||||
 | 
					            accelerationX = 1;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if(movementVector.getY() > 0 && movementVector.getY()/2.0 > currentVelocity.getY()) {
 | 
				
			||||||
 | 
					            accelerationY = 1;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else if(movementVector.getY() > 0 && movementVector.getY()/2.0 <= currentVelocity.getY()) {
 | 
				
			||||||
 | 
					            accelerationY = -1;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else if(movementVector.getY() < 0 && movementVector.getY()/2.0 < currentVelocity.getY()) {
 | 
				
			||||||
 | 
					            accelerationY = -1;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else if(movementVector.getY() < 0 && movementVector.getY()/2.0 >= currentVelocity.getY()) {
 | 
				
			||||||
 | 
					            accelerationY = 1;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        currentVelocity = new PositionVector(currentVelocity.getX() + accelerationX, currentVelocity.getY() + accelerationY);
 | 
				
			||||||
 | 
					        currentPosition = new PositionVector(currentPosition.getX() + currentVelocity.getX(), currentPosition.getY() + currentVelocity.getY());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        PositionVector acceleration = new PositionVector(accelerationX, accelerationY);
 | 
				
			||||||
 | 
					        Direction[] directions = Direction.values();
 | 
				
			||||||
 | 
					        for (Direction direction : directions) {
 | 
				
			||||||
 | 
					            if (direction.vector.equals(acceleration)) {
 | 
				
			||||||
 | 
					                return direction;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    return null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue