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
7c3f57caae
commit
f5cb7e6f80
@ -100,6 +100,8 @@ public final class Terminal {
|
||||
String line = IN.readLine();
|
||||
if ("breakpoint".equals(line)) {
|
||||
return readLine();
|
||||
} else if (line != null && line.startsWith("#")) {
|
||||
return readLine();
|
||||
}
|
||||
return line;
|
||||
} catch (final IOException e) {
|
||||
|
@ -234,7 +234,40 @@ public class RailwayNetwork {
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a rail that *contains* (not touch) this position.
|
||||
* Check whether a train can be placed at the specified position in the specified direction.
|
||||
*
|
||||
* @param position front position of the train
|
||||
* @param direction initial direction of the train
|
||||
* @return whether placement is possible
|
||||
*/
|
||||
public boolean allowsPlacementAt(Vector2D position, Vector2D direction) {
|
||||
// check that the requested direction is equal to the direction of one the tracks
|
||||
final Rail[] touchingRails = findTouchingRails(position);
|
||||
if (touchingRails.length == 0) {
|
||||
if (!findContainingRail(position)
|
||||
.map(rail -> rail.allowsPlacement(position, direction))
|
||||
.orElse(false)) {
|
||||
return false; // containing rail is orthogonal to the requested direction
|
||||
}
|
||||
} else if (touchingRails.length == 1) {
|
||||
if (!(touchingRails[0].allowsPlacement(position, direction)
|
||||
|| touchingRails[0].allowsMovement(position, direction))) {
|
||||
return false; // rail is orthogonal to the requested direction
|
||||
}
|
||||
} else if (!touchingRails[0].allowsPlacement(position, direction)
|
||||
&& !touchingRails[1].allowsPlacement(position, direction)
|
||||
&& !(touchingRails[0].allowsMovement(position, direction)
|
||||
|| touchingRails[1].allowsMovement(position, direction))) {
|
||||
// rail network does not allow this direction according to
|
||||
// https://ilias.studium.kit.edu/goto.php?client_id=produktiv&target=frm_996924_139143_534378
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a rail that contains this position.
|
||||
*
|
||||
* @param position the position to check
|
||||
* @return the rail that contains the position
|
||||
*/
|
||||
@ -242,10 +275,6 @@ public class RailwayNetwork {
|
||||
return rails.values().stream().filter(rail -> rail.contains(position)).findFirst();
|
||||
}
|
||||
|
||||
private Optional<Rail> findRailPotentiallyContaining(Vector2D position) {
|
||||
return rails.values().stream().filter(rail -> rail.canContain(position)).findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a rail that connects to or contains the first position and connects to or contains the second position.
|
||||
*
|
||||
|
@ -116,24 +116,7 @@ public final class Train {
|
||||
positionList.addLast(position);
|
||||
Vector2D rollingStockPosition = position;
|
||||
// check that the requested direction is equal to the direction of one the tracks
|
||||
final Rail[] touchingRails = railNetwork.findTouchingRails(position);
|
||||
if (touchingRails.length == 0) {
|
||||
if (!railNetwork.findContainingRail(position)
|
||||
.map(rail -> rail.allowsPlacement(position, direction))
|
||||
.orElse(false)) {
|
||||
return false; // containing rail is orthogonal to the requested direction
|
||||
}
|
||||
} else if (touchingRails.length == 1) {
|
||||
if (!(touchingRails[0].allowsPlacement(position, direction)
|
||||
|| touchingRails[0].allowsMovement(position, direction))) {
|
||||
return false; // rail is orthogonal to the requested direction
|
||||
}
|
||||
} else if (!touchingRails[0].allowsPlacement(position, direction)
|
||||
&& !touchingRails[1].allowsPlacement(position, direction)
|
||||
&& !(touchingRails[0].allowsMovement(position, direction)
|
||||
|| touchingRails[1].allowsMovement(position, direction))) {
|
||||
// rail network does not allow this direction according to
|
||||
// https://ilias.studium.kit.edu/goto.php?client_id=produktiv&target=frm_996924_139143_534378
|
||||
if (!railNetwork.allowsPlacementAt(position, direction)) {
|
||||
return false;
|
||||
}
|
||||
final Set<Rail> occupiedRailsSet = new HashSet<>();
|
||||
@ -252,11 +235,7 @@ public final class Train {
|
||||
if (nextPosition != null) {
|
||||
// not derailing
|
||||
positions.addFirst(new Vector2D(nextPosition));
|
||||
if (positions.size() >= 3 && nextPosition.subtract(positions.get(1)).normalized()
|
||||
.equals(positions.get(1).subtract(positions.get(2)).normalized())
|
||||
&& railNetwork.findTouchingRails(positions.get(1)).length == 0) {
|
||||
positions.remove(1);
|
||||
}
|
||||
simplifyPositions(railNetwork);
|
||||
occupiedRails.add(railNetwork.findRail(positions.get(1), nextPosition).get());
|
||||
} // else: derailing
|
||||
}
|
||||
@ -282,17 +261,32 @@ public final class Train {
|
||||
if (backPosition != null) {
|
||||
// not derailing
|
||||
positions.addLast(new Vector2D(backPosition));
|
||||
if (positions.size() >= 3
|
||||
&& backPosition.subtract(positions.get(positions.size() - 2)).normalized()
|
||||
.equals(positions.get(positions.size() - 2)
|
||||
.subtract(positions.get(positions.size() - 3)).normalized())
|
||||
&& railNetwork.findTouchingRails(positions.get(positions.size() - 2)).length == 0) {
|
||||
positions.remove(positions.size() - 2);
|
||||
}
|
||||
simplifyPositions(railNetwork);
|
||||
occupiedRails.add(railNetwork.findRail(positions.get(positions.size() - 2), backPosition).get());
|
||||
} // else: derailing
|
||||
}
|
||||
|
||||
/**
|
||||
* Simplify internal position data. Removes positions that are contained between two other positions.
|
||||
*
|
||||
* @param railNetwork railway network to use
|
||||
*/
|
||||
private void simplifyPositions(RailwayNetwork railNetwork) {
|
||||
if (positions.size() >= 3
|
||||
&& positions.getFirst().subtract(positions.get(1)).normalized()
|
||||
.equals(positions.get(1).subtract(positions.get(2)).normalized())
|
||||
&& railNetwork.findTouchingRails(positions.get(1)).length == 0) {
|
||||
positions.remove(1);
|
||||
}
|
||||
if (positions.size() >= 3
|
||||
&& positions.getLast().subtract(positions.get(positions.size() - 2)).normalized()
|
||||
.equals(positions.get(positions.size() - 2)
|
||||
.subtract(positions.get(positions.size() - 3)).normalized())
|
||||
&& railNetwork.findTouchingRails(positions.get(positions.size() - 2)).length == 0) {
|
||||
positions.remove(positions.size() - 2);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove this train from the rail network.
|
||||
*/
|
||||
|
@ -159,7 +159,7 @@ public final class TrainManager {
|
||||
throw new InvalidInputException("train is not valid");
|
||||
} else if (train.isPlaced()) {
|
||||
throw new InvalidInputException("train is already placed");
|
||||
} else if (direction.getX() != 0 && direction.getY() != 0) {
|
||||
} else if (!direction.isDirection()) {
|
||||
throw new InvalidInputException("invalid train direction");
|
||||
} else if (!railNetwork.isReadyForTrains()) {
|
||||
throw new InvalidInputException("switches not set up");
|
||||
|
@ -63,6 +63,16 @@ public class Vector2D {
|
||||
return Math.abs(this.x - other.x) + Math.abs(this.y - other.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this vector represents a valid direction. Is equivalent to checking whether it is parallel to
|
||||
* the x-axis or the y-axis.
|
||||
*
|
||||
* @return whether this vector is a valid direction
|
||||
*/
|
||||
public boolean isDirection() {
|
||||
return (x == 0 || y == 0) && (x != 0 || y != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this vector is parallel to another vector. Note that this method only works for vectors that
|
||||
* are parallel to one of the coordinate axes.
|
||||
|
@ -34,9 +34,6 @@ public final class CommandLine {
|
||||
if (input == null) {
|
||||
break; // FIXME remove
|
||||
}
|
||||
if (input.startsWith("#")) {
|
||||
continue; // FIXME remove
|
||||
}
|
||||
|
||||
if (input.startsWith(EXIT)) {
|
||||
if (input.length() != EXIT.length()) {
|
||||
|
Loading…
Reference in New Issue
Block a user