refactoring done in several classes

This commit is contained in:
Leonardo Brandenberger
2022-03-24 16:30:59 +01:00
parent bc0dd52360
commit 31ec1e9e64
11 changed files with 129 additions and 88 deletions
+15 -15
View File
@@ -3,14 +3,12 @@ package ch.zhaw.pm2.racetrack;
import ch.zhaw.pm2.racetrack.given.CarSpecification;
import ch.zhaw.pm2.racetrack.strategy.MoveStrategy;
import java.util.Vector;
/**
* Class representing a car on the racetrack.
* Uses {@link PositionVector} to store current position on the track grid and current velocity vector.
* Each car has an identifier character which represents the car on the race track board.
* Each car has an identifier character which represents the car on the racetrack board.
* Also keeps the state, if the car is crashed (not active anymore). The state can not be changed back to uncrashed.
* The velocity is changed by providing an acelleration vector.
* The velocity is changed by providing an acceleration vector.
* The car is able to calculate the endpoint of its next position and on request moves to it.
*/
public class Car implements CarSpecification {
@@ -46,7 +44,8 @@ public class Car implements CarSpecification {
/**
* Constructor for class Car
* @param id unique Car identification
*
* @param id unique Car identification
* @param position initial position of the Car
*/
public Car(char id, PositionVector position) {
@@ -59,16 +58,16 @@ public class Car implements CarSpecification {
*
* @return id of the car.
*/
public char getID(){
public char getID() {
return id;
}
public void increaseWinPoints() {
winPoints ++;
winPoints++;
}
public void deductWinPoints() {
winPoints --;
winPoints--;
}
public int getWinPoints() {
@@ -80,9 +79,10 @@ public class Car implements CarSpecification {
*
* @return velocity current velocity of the car.
*/
public PositionVector getVelocity(){
public PositionVector getVelocity() {
return velocity;
}
/**
* Set this Car position directly, regardless of current position and velocity.
* This should only be used by the game controller in rare cases to set the crash or winning position.
@@ -95,8 +95,7 @@ public class Car implements CarSpecification {
public void setPosition(final PositionVector position) {
if (position.getX() < 0 || position.getY() < 0) {
throw new IllegalArgumentException();
}
else {
} else {
this.position = position;
}
}
@@ -109,7 +108,7 @@ public class Car implements CarSpecification {
*/
@Override
public PositionVector nextPosition() {
return new PositionVector(position.getX() + velocity.getX(),position.getY() + velocity.getY());
return new PositionVector(position.getX() + velocity.getX(), position.getY() + velocity.getY());
}
/**
@@ -123,11 +122,10 @@ public class Car implements CarSpecification {
*/
@Override
public void accelerate(PositionVector.Direction acceleration) {
if(acceleration.vector.getX() < -1 || acceleration.vector.getX() > 1||
if (acceleration.vector.getX() < -1 || acceleration.vector.getX() > 1 ||
acceleration.vector.getY() < -1 || acceleration.vector.getY() > 1) {
throw new IllegalArgumentException();
}
else {
} else {
velocity = new PositionVector(velocity.getX() + acceleration.vector.getX(),
velocity.getY() + acceleration.vector.getY());
}
@@ -161,6 +159,7 @@ public class Car implements CarSpecification {
/**
* Set move strategy
*
* @param moveStrategy Strategy to be implemented
*/
public void setMoveStrategy(MoveStrategy moveStrategy) {
@@ -169,6 +168,7 @@ public class Car implements CarSpecification {
/**
* Get current move strategy
*
* @return MoveStrategy
*/
public MoveStrategy getMoveStrategy() {