worked on PathFinderMoveStrategy and Refactoring in Game.java
This commit is contained in:
		
							parent
							
								
									4b5802a0d0
								
							
						
					
					
						commit
						ad2e1c3104
					
				| 
						 | 
				
			
			@ -220,7 +220,12 @@ public class Game implements GameSpecification {
 | 
			
		|||
        if (crashPosition != null) {
 | 
			
		||||
            track.carDoesCrash(currentCarIndex, crashPosition);
 | 
			
		||||
        } else {
 | 
			
		||||
            calculateWinner(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition(), currentCarIndex);
 | 
			
		||||
            int newWinPoints = calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition());
 | 
			
		||||
            if(newWinPoints == 1){
 | 
			
		||||
               track.getCar(currentCarIndex).increaseWinPoints();
 | 
			
		||||
            }else{
 | 
			
		||||
                track.getCar(currentCarIndex).deductWinPoints();
 | 
			
		||||
            }
 | 
			
		||||
            track.moveCar(currentCarIndex);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -280,43 +285,46 @@ public class Game implements GameSpecification {
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) {
 | 
			
		||||
    private int calculateNewWinPoints(PositionVector start, PositionVector finish) {
 | 
			
		||||
        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();
 | 
			
		||||
                            return 1;
 | 
			
		||||
                        } else if (start.getY() > finish.getY()) {
 | 
			
		||||
                            track.getCar(carIndex).deductWinPoints();
 | 
			
		||||
                            return -1;
 | 
			
		||||
                        }
 | 
			
		||||
                        break;
 | 
			
		||||
                    case FINISH_DOWN:
 | 
			
		||||
                        if (start.getY() > finish.getY()) {
 | 
			
		||||
                            track.getCar(carIndex).increaseWinPoints();
 | 
			
		||||
                            return 1;
 | 
			
		||||
                        } else if (start.getY() < finish.getY()) {
 | 
			
		||||
                            track.getCar(carIndex).deductWinPoints();
 | 
			
		||||
                            return -1;
 | 
			
		||||
                        }
 | 
			
		||||
                        break;
 | 
			
		||||
                    case FINISH_RIGHT:
 | 
			
		||||
                        if (start.getX() < finish.getX()) {
 | 
			
		||||
                            track.getCar(carIndex).increaseWinPoints();
 | 
			
		||||
                            return 1;
 | 
			
		||||
                        } else if (start.getX() > finish.getX()) {
 | 
			
		||||
                            track.getCar(carIndex).deductWinPoints();
 | 
			
		||||
                            return -1;
 | 
			
		||||
                        }
 | 
			
		||||
                        break;
 | 
			
		||||
                    case FINISH_LEFT:
 | 
			
		||||
                        if (start.getX() > finish.getX()) {
 | 
			
		||||
                            track.getCar(carIndex).increaseWinPoints();
 | 
			
		||||
                            return 1;
 | 
			
		||||
                        } else if (start.getX() < finish.getX()) {
 | 
			
		||||
                            track.getCar(carIndex).increaseWinPoints();
 | 
			
		||||
                            return -1;
 | 
			
		||||
                        }
 | 
			
		||||
                        break;
 | 
			
		||||
                    default:
 | 
			
		||||
                        return 0;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,36 +1,128 @@
 | 
			
		|||
package ch.zhaw.pm2.racetrack.strategy;
 | 
			
		||||
 | 
			
		||||
import ch.zhaw.pm2.racetrack.PositionVector;
 | 
			
		||||
import ch.zhaw.pm2.racetrack.PositionVectorNotValid;
 | 
			
		||||
import ch.zhaw.pm2.racetrack.Track;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
 | 
			
		||||
public class PathFinderMoveStrategy implements MoveStrategy{
 | 
			
		||||
    private Track track;
 | 
			
		||||
    private int carIndex;
 | 
			
		||||
    private List<PositionVector.Direction> moveList;
 | 
			
		||||
    private int pointer;
 | 
			
		||||
    private List<PositionVector.Direction> allDirections;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public PathFinderMoveStrategy(Track track, int carIndex) {
 | 
			
		||||
        this.track = track;
 | 
			
		||||
        this.carIndex = carIndex;
 | 
			
		||||
        allDirections = Arrays.asList(PositionVector.Direction.values());
 | 
			
		||||
        createMoveList();
 | 
			
		||||
        pointer = -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    private void createMoveList(){
 | 
			
		||||
        moveList = new ArrayList<>();
 | 
			
		||||
        PossibleMove finishedMove = null;
 | 
			
		||||
        List<PossibleMove> possibleMoves= new ArrayList<>();
 | 
			
		||||
        for(PositionVector.Direction direction : allDirections){
 | 
			
		||||
            possibleMoves.add(new PossibleMove(null, direction));
 | 
			
		||||
        }
 | 
			
		||||
        while(finishedMove == null){
 | 
			
		||||
            List<PossibleMove> newMoves = new ArrayList<>();
 | 
			
		||||
            for(PossibleMove previousMove : possibleMoves){
 | 
			
		||||
                for(PositionVector.Direction direction : allDirections){
 | 
			
		||||
                    PossibleMove newMove = new PossibleMove(previousMove, direction);
 | 
			
		||||
 | 
			
		||||
                        if(! (newMove.crashed() || newMove.drivingLoop())){
 | 
			
		||||
                            newMoves.add(newMove);
 | 
			
		||||
                        }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            possibleMoves = newMoves;
 | 
			
		||||
            finishedMove = findFinishedMove(possibleMoves);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        moveList = finishedMove.directions;
 | 
			
		||||
 | 
			
		||||
        pointer = moveList.size();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private PossibleMove findFinishedMove(List<PossibleMove> moveList){
 | 
			
		||||
        for(PossibleMove move : moveList){
 | 
			
		||||
            if(move.finished()){
 | 
			
		||||
                return move;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public class PossibleMove {
 | 
			
		||||
        List<PositionVector.Direction> directions;
 | 
			
		||||
        List<PositionVector> positions;
 | 
			
		||||
        PositionVector endVelocity;
 | 
			
		||||
 | 
			
		||||
        public PossibleMove(PossibleMove previousMove, PositionVector.Direction nextDirection){
 | 
			
		||||
            PositionVector startVelocity;
 | 
			
		||||
            PositionVector startPosition;
 | 
			
		||||
            positions = new ArrayList<>();
 | 
			
		||||
            directions = new ArrayList<>();
 | 
			
		||||
            if(previousMove != null){
 | 
			
		||||
                positions.addAll(previousMove.positions);
 | 
			
		||||
                directions.addAll(previousMove.directions);
 | 
			
		||||
                startPosition = positions.get(positions.size()-1);
 | 
			
		||||
                startVelocity = previousMove.endVelocity;
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                startPosition = track.getCarPos(carIndex);
 | 
			
		||||
                startVelocity = track.getCar(carIndex).getVelocity();
 | 
			
		||||
            }
 | 
			
		||||
            directions.add(nextDirection);
 | 
			
		||||
            endVelocity = new PositionVector(startVelocity.getX() + nextDirection.vector.getX(), startVelocity.getY() + nextDirection.vector.getY());
 | 
			
		||||
            positions.add(new PositionVector(startPosition.getX() + endVelocity.getX(), startPosition.getY() + endVelocity.getY()));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public boolean finished(){
 | 
			
		||||
            for(PositionVector finishPosition : track.getFinishLine()){
 | 
			
		||||
                if(finishPosition.equals(positions.get(positions.size()-1))){
 | 
			
		||||
                    return true; //TODO: check direction
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public boolean crashed() {
 | 
			
		||||
            try {
 | 
			
		||||
                if(track.willCrashAtPosition(carIndex, positions.get(positions.size()-1))){
 | 
			
		||||
                    return true;
 | 
			
		||||
                }
 | 
			
		||||
            } catch (PositionVectorNotValid e) {
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public boolean drivingLoop(){
 | 
			
		||||
            for(int i = 0; i < positions.size()-1; i++){
 | 
			
		||||
                if(positions.get(i).equals(positions.get(positions.size()-1))){
 | 
			
		||||
                    return true;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public PositionVector.Direction nextMove() {
 | 
			
		||||
        pointer += 1;
 | 
			
		||||
        pointer -= 1;
 | 
			
		||||
        //TODO: Check if crash. if yes --> createMoveList();
 | 
			
		||||
        if (pointer < moveList.size()) {
 | 
			
		||||
        if (pointer >= 0) {
 | 
			
		||||
            return moveList.get(pointer);
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue