mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final1.git
synced 2024-11-08 18:00:38 +00:00
Checkstyle
This commit is contained in:
parent
fa3c9cf113
commit
f0b35e0707
@ -102,7 +102,7 @@ public final class Terminal {
|
|||||||
if ("breakpoint".equals(line)) {
|
if ("breakpoint".equals(line)) {
|
||||||
return readLine();
|
return readLine();
|
||||||
} else if (line != null && line.startsWith("#")) {
|
} else if (line != null && line.startsWith("#")) {
|
||||||
return readLine();
|
line = IN.readLine();
|
||||||
}
|
}
|
||||||
return line;
|
return line;
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
|
@ -322,10 +322,10 @@ public class ModelRailwaySimulation {
|
|||||||
* Move the trains in this simulation.
|
* Move the trains in this simulation.
|
||||||
*
|
*
|
||||||
* @param speed amount of steps to move the trains
|
* @param speed amount of steps to move the trains
|
||||||
* @return train collisions, null if no trains are placed
|
* @return train collisions, empty if no trains are placed
|
||||||
* @throws InvalidInputException if the simulation is not yet ready
|
* @throws InvalidInputException if the simulation is not yet ready
|
||||||
*/
|
*/
|
||||||
public List<SortedSet<Integer>> step(short speed) throws InvalidInputException {
|
public Optional<List<SortedSet<Integer>>> step(short speed) throws InvalidInputException {
|
||||||
return trainManager.step(speed);
|
return trainManager.step(speed);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -197,7 +197,7 @@ public class RailwayNetwork {
|
|||||||
return null; // there is no rail to move on
|
return null; // there is no rail to move on
|
||||||
} else if (touchingRails.length == 1) { // simply move along the rail
|
} else if (touchingRails.length == 1) { // simply move along the rail
|
||||||
return touchingRails[0].move(position, direction, 1);
|
return touchingRails[0].move(position, direction, 1);
|
||||||
} // else: we check movement along both rails and pick the correct rail
|
} // else: check movement along both rails and pick the correct rail
|
||||||
final Vector2D onRailOne = touchingRails[0].move(position, new Vector2D(direction), steps);
|
final Vector2D onRailOne = touchingRails[0].move(position, new Vector2D(direction), steps);
|
||||||
final Vector2D onRailTwo = touchingRails[1].move(position, new Vector2D(direction), steps);
|
final Vector2D onRailTwo = touchingRails[1].move(position, new Vector2D(direction), steps);
|
||||||
// if we are stuck on the first rail
|
// if we are stuck on the first rail
|
||||||
@ -205,8 +205,8 @@ public class RailwayNetwork {
|
|||||||
// or move in the correct direction on the second rail
|
// or move in the correct direction on the second rail
|
||||||
if (position.equals(onRailOne) || onRailOne == null
|
if (position.equals(onRailOne) || onRailOne == null
|
||||||
|| onRailTwo != null && onRailTwo.subtract(position).directionEquals(direction)) {
|
|| onRailTwo != null && onRailTwo.subtract(position).directionEquals(direction)) {
|
||||||
return onRailTwo; // return that position
|
return onRailTwo; // return movement on rail two
|
||||||
} else if (position.equals(onRailTwo) || onRailTwo == null
|
} else if (position.equals(onRailTwo) || onRailTwo == null // see above, same logic for the other rail
|
||||||
|| onRailOne.subtract(position).directionEquals(direction)) {
|
|| onRailOne.subtract(position).directionEquals(direction)) {
|
||||||
return onRailOne;
|
return onRailOne;
|
||||||
} else {
|
} else {
|
||||||
@ -235,10 +235,10 @@ public class RailwayNetwork {
|
|||||||
// rail should not be orthogonal to the requested direction
|
// rail should not be orthogonal to the requested direction
|
||||||
return touchingRails[0].allowsPlacement(position, direction)
|
return touchingRails[0].allowsPlacement(position, direction)
|
||||||
|| touchingRails[0].allowsMovement(position, direction);
|
|| touchingRails[0].allowsMovement(position, direction);
|
||||||
} else if (!touchingRails[0].allowsPlacement(position, direction)
|
} else if (!touchingRails[0].allowsPlacement(position, direction) // train can not be placed on either rail
|
||||||
&& !touchingRails[1].allowsPlacement(position, direction)
|
&& !touchingRails[1].allowsPlacement(position, direction) // in the specified direction
|
||||||
&& !(touchingRails[0].allowsMovement(position, direction)
|
&& !(touchingRails[0].allowsMovement(position, direction) // *and* it can not move in the specified
|
||||||
|| touchingRails[1].allowsMovement(position, direction))) {
|
|| touchingRails[1].allowsMovement(position, direction))) { // direction afterwards
|
||||||
// rail network does not allow this direction according to
|
// rail network does not allow this direction according to
|
||||||
// https://ilias.studium.kit.edu/goto.php?client_id=produktiv&target=frm_996924_139143_534378
|
// https://ilias.studium.kit.edu/goto.php?client_id=produktiv&target=frm_996924_139143_534378
|
||||||
return false;
|
return false;
|
||||||
|
@ -8,6 +8,7 @@ import java.util.LinkedList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -252,7 +253,7 @@ public final class Train implements Comparable<Train> {
|
|||||||
* @param backPosition position to move back of the train to
|
* @param backPosition position to move back of the train to
|
||||||
*/
|
*/
|
||||||
public void moveBackTo(RailwayNetwork railNetwork, Vector2D backPosition) {
|
public void moveBackTo(RailwayNetwork railNetwork, Vector2D backPosition) {
|
||||||
// TODO: create common method for both directions
|
// a common method for both directions would be a huge mess: requires at least six (!) extra parameters
|
||||||
final Optional<Rail> railUnderFrontOfTrain = railNetwork.findContainingRail(positions.getFirst());
|
final Optional<Rail> railUnderFrontOfTrain = railNetwork.findContainingRail(positions.getFirst());
|
||||||
positions.getFirst().subtractInPlace(getDirection());
|
positions.getFirst().subtractInPlace(getDirection());
|
||||||
if (positions.getFirst().equals(positions.get(1))) {
|
if (positions.getFirst().equals(positions.get(1))) {
|
||||||
@ -372,6 +373,12 @@ public final class Train implements Comparable<Train> {
|
|||||||
return stringBuilder.toString();
|
return stringBuilder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compare two trains based on their identifier.
|
||||||
|
*
|
||||||
|
* @param other train to compare to
|
||||||
|
* @return integer comparison of the identifiers
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int compareTo(Train other) {
|
public int compareTo(Train other) {
|
||||||
return Integer.compare(this.identifier, other.identifier);
|
return Integer.compare(this.identifier, other.identifier);
|
||||||
|
@ -333,24 +333,24 @@ public final class TrainManager {
|
|||||||
* Move the trains in this simulation.
|
* Move the trains in this simulation.
|
||||||
*
|
*
|
||||||
* @param speed amount of steps to move the trains
|
* @param speed amount of steps to move the trains
|
||||||
* @return train collisions, null if no trains are placed
|
* @return train collisions, empty if no trains are placed
|
||||||
* @throws InvalidInputException if simulation is not yet ready
|
* @throws InvalidInputException if simulation is not yet ready
|
||||||
*/
|
*/
|
||||||
public List<SortedSet<Integer>> step(short speed) throws InvalidInputException {
|
public Optional<List<SortedSet<Integer>>> step(short speed) throws InvalidInputException {
|
||||||
if (!railNetwork.isReadyForTrains()) {
|
if (!railNetwork.isReadyForTrains()) {
|
||||||
throw new InvalidInputException("rail tracks/switches not set up");
|
throw new InvalidInputException("rail tracks/switches not set up");
|
||||||
}
|
}
|
||||||
if (trains.values().stream().noneMatch(Train::isPlaced)) {
|
if (trains.values().stream().noneMatch(Train::isPlaced)) {
|
||||||
return null;
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
return IntStream.range(0, Math.abs(speed))
|
return Optional.of(IntStream.range(0, Math.abs(speed))
|
||||||
.mapToObj(step -> speed >= 0 ? getCollisionsOfOneStep() : getCollisionsOfOneReverseStep())
|
.mapToObj(step -> speed >= 0 ? getCollisionsOfOneStep() : getCollisionsOfOneReverseStep())
|
||||||
// replace train references with identifiers to prevent modification by caller
|
// replace train references with identifiers to prevent modification by caller
|
||||||
.flatMap(collisions -> collisions.stream()
|
.flatMap(collisions -> collisions.stream()
|
||||||
.map(collision -> collision.stream().map(Train::getIdentifier)
|
.map(collision -> collision.stream().map(Train::getIdentifier)
|
||||||
.collect(Collectors.toCollection(TreeSet::new))))
|
.collect(Collectors.toCollection(TreeSet::new))))
|
||||||
.sorted(Comparator.comparing(TreeSet::first))
|
.sorted(Comparator.comparing(TreeSet::first))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import edu.kit.informatik.model.Vector2D;
|
|||||||
import edu.kit.informatik.ui.InvalidInputException;
|
import edu.kit.informatik.ui.InvalidInputException;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
|
|
||||||
import static edu.kit.informatik.ui.CommandLine.OK;
|
import static edu.kit.informatik.ui.CommandLine.OK;
|
||||||
@ -26,12 +27,12 @@ public class Step extends Command {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void apply(ModelRailwaySimulation simulation) throws InvalidInputException {
|
public void apply(ModelRailwaySimulation simulation) throws InvalidInputException {
|
||||||
final List<SortedSet<Integer>> collisions = simulation.step(speed);
|
final Optional<List<SortedSet<Integer>>> collisions = simulation.step(speed);
|
||||||
if (collisions == null) {
|
if (!collisions.isPresent()) {
|
||||||
Terminal.printLine(OK);
|
Terminal.printLine(OK);
|
||||||
} else {
|
} else {
|
||||||
for (final int id : simulation.getTrains().keySet()) {
|
for (final int id : simulation.getTrains().keySet()) {
|
||||||
final SortedSet<Integer> collisionSet = collisions.stream()
|
final SortedSet<Integer> collisionSet = collisions.get().stream()
|
||||||
.filter(collision -> collision.first() == id)
|
.filter(collision -> collision.first() == id)
|
||||||
.findFirst().orElse(null);
|
.findFirst().orElse(null);
|
||||||
if (collisionSet != null) { // print collision
|
if (collisionSet != null) { // print collision
|
||||||
|
Loading…
Reference in New Issue
Block a user