mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final1.git
synced 2024-11-24 09:24:58 +00:00
Checkstyle
This commit is contained in:
parent
4d23894509
commit
48b4562bde
@ -3,7 +3,14 @@ package edu.kit.informatik.model;
|
||||
import edu.kit.informatik.ui.CoachType;
|
||||
import edu.kit.informatik.Terminal;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ModelRailwaySimulation {
|
||||
@ -321,15 +328,19 @@ public class ModelRailwaySimulation {
|
||||
public boolean putTrain(final int trainId, final Vector2D position, final Vector2D direction) {
|
||||
Train train = getTrain(trainId);
|
||||
if (train == null || !train.isProperTrain() || train.isPlaced()) {
|
||||
// can only place valid trains that are not already placed
|
||||
return false;
|
||||
}
|
||||
|
||||
// attempt to place train
|
||||
boolean placed = train.placeOn(railNetwork, position, direction);
|
||||
|
||||
if (placed) {
|
||||
// now, check for collisions
|
||||
List<HashSet<Train>> collisions = getStaticCollisions(false);
|
||||
if (!collisions.isEmpty()) {
|
||||
train.storePositionDirectionBlocks(null, null, null);
|
||||
// remove if colliding
|
||||
train.removeFromRails();
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
@ -403,7 +414,7 @@ public class ModelRailwaySimulation {
|
||||
Vector2D nextPosition = railNetwork.move(position, direction);
|
||||
if (nextPosition == null
|
||||
|| (train.isOnPosition(nextPosition)
|
||||
&& !train.getBackPosition().equals(train.getFrontPosition()))) {
|
||||
&& !train.getRearPosition().equals(train.getFrontPosition()))) {
|
||||
collisions.add(new HashSet<>(Arrays.asList(train)));
|
||||
train.removeFromRails();
|
||||
nextOccupiedRails.put(train, occupiedRails.get(train));
|
||||
@ -465,8 +476,8 @@ public class ModelRailwaySimulation {
|
||||
continue;
|
||||
}
|
||||
Vector2D front = train.getFrontPosition();
|
||||
Vector2D position = train.getBackPosition();
|
||||
Vector2D direction = train.getBackDirection();
|
||||
Vector2D position = train.getRearPosition();
|
||||
Vector2D direction = train.getRearDirection();
|
||||
Vector2D nextPosition = railNetwork.move(position, direction);
|
||||
if (nextPosition == null
|
||||
|| (train.isOnPosition(nextPosition) && !train.getFrontPosition().equals(nextPosition))) {
|
||||
|
@ -2,7 +2,12 @@ package edu.kit.informatik.model;
|
||||
|
||||
import edu.kit.informatik.Terminal;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A rail network consisting of tracks and switches. Is only concerned with rails, does not process trains.
|
||||
|
@ -2,7 +2,11 @@ package edu.kit.informatik.model;
|
||||
|
||||
import edu.kit.informatik.Terminal;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A train consisting of one or more rolling stock. Can be placed on a rail network.
|
||||
@ -102,9 +106,10 @@ public final class Train {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
* Check whether this train is on a particular rail. Note that at least one rolling stock has to be on the rail,
|
||||
* just touching the rail is not enough.
|
||||
* @param id identifier of a rail
|
||||
* @return whether this train is on that rail
|
||||
*/
|
||||
public boolean isOnRail(final int id) {
|
||||
return occupiedRails != null && occupiedRails.stream().anyMatch(rail -> rail.getIdentifier() == id);
|
||||
@ -121,7 +126,7 @@ public final class Train {
|
||||
Vector2D direction = rawDirection.normalized();
|
||||
int length = getLength();
|
||||
|
||||
List<Vector2D> positions = new ArrayList<>();
|
||||
LinkedList<Vector2D> positions = new LinkedList<>();
|
||||
positions.add(position);
|
||||
Vector2D positioningDirection = direction.negated();
|
||||
Vector2D rollingStockPosition = position;
|
||||
@ -154,7 +159,9 @@ public final class Train {
|
||||
}
|
||||
}
|
||||
}
|
||||
storePositionDirectionBlocks(positions, direction, occupiedRails);
|
||||
this.position = positions;
|
||||
this.direction = direction;
|
||||
this.occupiedRails = occupiedRails;
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -167,21 +174,9 @@ public final class Train {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param positions
|
||||
* @param direction
|
||||
* @param occupiedRails
|
||||
*/
|
||||
public void storePositionDirectionBlocks(final List<Vector2D> positions, final Vector2D direction,
|
||||
final Set<Rail> occupiedRails) {
|
||||
this.position = positions;
|
||||
this.direction = direction;
|
||||
this.occupiedRails = occupiedRails;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
* Check whether this train is correctly set up. More specifically, it either consists of train sets or has an
|
||||
* engine as first or last rolling stock.
|
||||
* @return whether this train is valid
|
||||
*/
|
||||
public boolean isProperTrain() {
|
||||
RollingStock first = rollingStocks.get(0);
|
||||
@ -193,52 +188,40 @@ public final class Train {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param rail
|
||||
* @return
|
||||
*/
|
||||
public boolean isPartiallyOn(Rail rail) {
|
||||
return position.stream().anyMatch(rail::contains)
|
||||
|| position.stream().filter(rail::connectsTo).count() == 2;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
* @return position of the train head
|
||||
*/
|
||||
public Vector2D getFrontPosition() {
|
||||
if (position != null) {
|
||||
return position.get(0);
|
||||
// make sure caller can not modify internal data
|
||||
return new Vector2D(position.get(0));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
* @return position of the rear of the train
|
||||
*/
|
||||
public Vector2D getBackPosition() {
|
||||
public Vector2D getRearPosition() {
|
||||
if (position != null) {
|
||||
return position.get(position.size() - 1);
|
||||
// make sure caller can not modify internal data
|
||||
return new Vector2D(position.get(position.size() - 1));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
* @return the direction of this train
|
||||
*/
|
||||
public Vector2D getDirection() {
|
||||
return direction;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
* @return the rear direction of this train
|
||||
*/
|
||||
public Vector2D getBackDirection() {
|
||||
public Vector2D getRearDirection() {
|
||||
if (position.size() == 1) {
|
||||
return direction.negated();
|
||||
} else {
|
||||
@ -247,8 +230,7 @@ public final class Train {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
* @return a set of the rails this train occupies
|
||||
*/
|
||||
public Set<Rail> getOccupiedRails() {
|
||||
// make sure caller can not modify internal state
|
||||
@ -256,9 +238,9 @@ public final class Train {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param railNetwork
|
||||
* @param nextPosition
|
||||
* Move this train on a train network to the specified position.
|
||||
* @param railNetwork rail network to move train on
|
||||
* @param nextPosition position to move train to
|
||||
*/
|
||||
public void moveTo(final RailwayNetwork railNetwork, final Vector2D nextPosition) {
|
||||
position.add(0, nextPosition);
|
||||
@ -267,15 +249,23 @@ public final class Train {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param backPosition
|
||||
* Move this train on a train network backwards to the specified position.
|
||||
* @param railNetwork rail network to move train on
|
||||
* @param rearPosition position to move rear of the train to
|
||||
*/
|
||||
public void moveBackTo(final RailwayNetwork railNetwork, final Vector2D backPosition) {
|
||||
public void moveBackTo(final RailwayNetwork railNetwork, final Vector2D rearPosition) {
|
||||
Vector2D last = position.remove(0);
|
||||
position.add(backPosition);
|
||||
updateOccupiedRails(railNetwork, backPosition, last, position.get(0));
|
||||
position.add(rearPosition);
|
||||
updateOccupiedRails(railNetwork, rearPosition, last, position.get(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update set of occupied rails after modifying position.
|
||||
* @param railNetwork rail network to use
|
||||
* @param nextPosition where the train just moved
|
||||
* @param lastPosition where the train just was
|
||||
* @param secondToLastPosition where the end of the train is now
|
||||
*/
|
||||
private void updateOccupiedRails(final RailwayNetwork railNetwork, final Vector2D nextPosition,
|
||||
final Vector2D lastPosition, final Vector2D secondToLastPosition) {
|
||||
if (nextPosition.equals(secondToLastPosition)) {
|
||||
@ -322,51 +312,51 @@ public final class Train {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param newDirection
|
||||
* Update the direction of this train.
|
||||
* @param newDirection new direction of the train
|
||||
*/
|
||||
public void setDirection(Vector2D newDirection) {
|
||||
direction = newDirection;
|
||||
public void setDirection(final Vector2D newDirection) {
|
||||
// make sure the caller can not modify internal data afterwards
|
||||
direction = new Vector2D(newDirection);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Remove this train from the rail network.
|
||||
*/
|
||||
public void removeFromRails() {
|
||||
position = null;
|
||||
direction = null;
|
||||
occupiedRails = null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param other
|
||||
* @return
|
||||
* Check whether this train touches another train. It is considered touching if two trains are behind each other.
|
||||
* @param other other train
|
||||
* @return whether this train touches the other train
|
||||
*/
|
||||
public boolean touches(Train other) {
|
||||
return other.isOnAnyPosition(position);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param point
|
||||
* @return
|
||||
* @param point position to check
|
||||
* @return whether this train is on the specified position
|
||||
*/
|
||||
public boolean isOnPosition(Vector2D point) {
|
||||
return position.contains(point);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param positions
|
||||
* @return
|
||||
* @param positions list of positions to check
|
||||
* @return whether this train is on any of the specified positions
|
||||
*/
|
||||
public boolean isOnAnyPosition(List<Vector2D> positions) {
|
||||
return position.stream().anyMatch(positions::contains);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
* Calculate the length of this train, summing the length of all rolling stocks.
|
||||
* @return total length of this train
|
||||
*/
|
||||
public int getLength() {
|
||||
return rollingStocks.stream().mapToInt((RollingStock::getLength)).sum();
|
||||
|
@ -28,6 +28,15 @@ public class Vector2D {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a vector, creating a new object.
|
||||
* @param other a vector
|
||||
*/
|
||||
public Vector2D(final Vector2D other) {
|
||||
this.x = other.x;
|
||||
this.y = other.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param input two numbers separated by a comma
|
||||
* @return a vector containing the two numbers, null otherwise
|
||||
|
@ -1,6 +1,6 @@
|
||||
package edu.kit.informatik.ui.command;
|
||||
|
||||
import edu.kit.informatik.*;
|
||||
import edu.kit.informatik.Terminal;
|
||||
import edu.kit.informatik.model.Vector2D;
|
||||
import edu.kit.informatik.ui.CoachType;
|
||||
import edu.kit.informatik.ui.EngineType;
|
||||
|
@ -1,7 +1,11 @@
|
||||
package edu.kit.informatik.ui.command;
|
||||
|
||||
import edu.kit.informatik.*;
|
||||
import edu.kit.informatik.model.*;
|
||||
import edu.kit.informatik.Terminal;
|
||||
import edu.kit.informatik.model.DieselEngine;
|
||||
import edu.kit.informatik.model.ElectricalEngine;
|
||||
import edu.kit.informatik.model.Engine;
|
||||
import edu.kit.informatik.model.ModelRailwaySimulation;
|
||||
import edu.kit.informatik.model.SteamEngine;
|
||||
import edu.kit.informatik.ui.EngineType;
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user