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