mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final1.git
synced 2024-11-27 18:55:55 +00:00
Checkstyle
This commit is contained in:
parent
0a640cbabe
commit
5f5162f003
@ -47,18 +47,17 @@ public class RailwayNetwork {
|
|||||||
rails.put(1, newTrack);
|
rails.put(1, newTrack);
|
||||||
return newTrack.getIdentifier();
|
return newTrack.getIdentifier();
|
||||||
} else {
|
} else {
|
||||||
for (final Rail rail : rails.values()) {
|
if (rails.values().stream().anyMatch(rail -> rail.canConnectTo(start) || rail.canConnectTo(end))) {
|
||||||
if (rail.canConnectTo(start) || rail.canConnectTo(end)) {
|
final int id = getNextRailIdentifier();
|
||||||
final int id = getNextRailIdentifier();
|
if (id < 0) {
|
||||||
if (id < 0) {
|
return -1;
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
final Track newTrack = new Track(start, end, id);
|
|
||||||
rails.put(id, newTrack);
|
|
||||||
return newTrack.getIdentifier();
|
|
||||||
}
|
}
|
||||||
|
final Track newTrack = new Track(start, end, id);
|
||||||
|
rails.put(id, newTrack);
|
||||||
|
return newTrack.getIdentifier();
|
||||||
|
} else {
|
||||||
|
throw new InvalidInputException("track is not connected to other tracks");
|
||||||
}
|
}
|
||||||
throw new InvalidInputException("track is not connected to other tracks");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,29 +121,20 @@ public class RailwayNetwork {
|
|||||||
if (toRemove == null) {
|
if (toRemove == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// locate one connected rail
|
// locate one connected rail
|
||||||
Rail otherRail = null;
|
final Optional<Rail> otherRail = rails.values().stream()
|
||||||
for (final Rail anotherRail : rails.values()) {
|
.filter(rail -> rail.getIdentifier() != toRemove.getIdentifier() && rail.canConnectToRail(toRemove))
|
||||||
if (anotherRail.getIdentifier() != id && anotherRail.canConnectToRail(toRemove)) {
|
.findFirst();
|
||||||
otherRail = anotherRail;
|
if (!otherRail.isPresent()) {
|
||||||
break;
|
rails.clear(); // last rail left, can always remove
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// last rail left, can always remove
|
|
||||||
if (otherRail == null) {
|
|
||||||
rails.clear();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check that rails would still be connected after removing this rail
|
// check that rails would still be connected after removing this rail
|
||||||
final Set<Rail> connectedRails = new HashSet<>();
|
final Set<Rail> connectedRails = new HashSet<>();
|
||||||
connectedRails.add(otherRail);
|
connectedRails.add(otherRail.get());
|
||||||
final Queue<Rail> toProcess = new LinkedList<>();
|
final Queue<Rail> toProcess = new LinkedList<>();
|
||||||
toProcess.add(otherRail);
|
toProcess.add(otherRail.get());
|
||||||
|
// basically breadth-first search (using a queue)
|
||||||
// basically breadth-first search, using a queue
|
|
||||||
while (!toProcess.isEmpty()) {
|
while (!toProcess.isEmpty()) {
|
||||||
final Rail connected = toProcess.poll();
|
final Rail connected = toProcess.poll();
|
||||||
|
|
||||||
@ -155,13 +145,7 @@ public class RailwayNetwork {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return connectedRails.size() == rails.size() - 1 && rails.remove(id) != null;
|
||||||
if (connectedRails.size() == rails.size() - 1) {
|
|
||||||
rails.remove(id);
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -173,10 +157,7 @@ public class RailwayNetwork {
|
|||||||
*/
|
*/
|
||||||
public boolean setSwitch(int id, Vector2D position) {
|
public boolean setSwitch(int id, Vector2D position) {
|
||||||
final Rail toSwitch = rails.get(id);
|
final Rail toSwitch = rails.get(id);
|
||||||
if (toSwitch != null) {
|
return toSwitch != null && toSwitch.switchTo(position);
|
||||||
return toSwitch.switchTo(position);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -202,33 +183,31 @@ public class RailwayNetwork {
|
|||||||
* Move a position along the rails of this rail network in the specified direction.
|
* Move a position along the rails of this rail network in the specified direction.
|
||||||
*
|
*
|
||||||
* @param position the position to move
|
* @param position the position to move
|
||||||
* @param direction has to be (0,1) (0,-1) (1,0) (-1,0), will be modified if turn is necessary
|
* @param direction has to be (0,1) (0,-1) (1,0) or (-1,0)
|
||||||
* @param steps amount of steps to go
|
* @param steps amount of steps to go
|
||||||
* @return next position, null if off the rails after single step
|
* @return next position, null if off the rails after single step
|
||||||
*/
|
*/
|
||||||
public Vector2D move(Vector2D position, Vector2D direction, long steps) {
|
public Vector2D move(Vector2D position, Vector2D direction, long steps) {
|
||||||
final Optional<Rail> containingRail = findContainingRail(position);
|
final Optional<Rail> containingRail = findContainingRail(position);
|
||||||
if (containingRail.isPresent()) {
|
if (containingRail.isPresent()) { // if position is on a rail, simply move along that rail
|
||||||
return containingRail.get().move(position, direction, steps);
|
return containingRail.get().move(position, direction, steps);
|
||||||
}
|
}
|
||||||
final Rail[] touchingRails = findTouchingRails(position);
|
final Rail[] touchingRails = findTouchingRails(position);
|
||||||
if (touchingRails.length == 0) {
|
if (touchingRails.length == 0) {
|
||||||
return null;
|
return null; // there is no rail to move on
|
||||||
} else if (touchingRails.length == 1) {
|
} 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
|
||||||
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
|
||||||
|
// or derail on the first 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)) {
|
||||||
// we are moving on rail two
|
return onRailTwo; // return that position
|
||||||
final Vector2D newDirection = touchingRails[1].getDirectionFrom(position);
|
|
||||||
direction.copyFrom(newDirection);
|
|
||||||
return onRailTwo;
|
|
||||||
} else if (position.equals(onRailTwo) || onRailTwo == null
|
} else if (position.equals(onRailTwo) || onRailTwo == null
|
||||||
|| onRailOne.subtract(position).directionEquals(direction)) {
|
|| onRailOne.subtract(position).directionEquals(direction)) {
|
||||||
final Vector2D newDirection = touchingRails[0].getDirectionFrom(position);
|
|
||||||
direction.copyFrom(newDirection);
|
|
||||||
return onRailOne;
|
return onRailOne;
|
||||||
} else {
|
} else {
|
||||||
// this case: https://ilias.studium.kit.edu/goto.php?client_id=produktiv&target=frm_996924_139074_534267
|
// this case: https://ilias.studium.kit.edu/goto.php?client_id=produktiv&target=frm_996924_139074_534267
|
||||||
@ -277,6 +256,16 @@ public class RailwayNetwork {
|
|||||||
return rails.values().stream().filter(rail -> rail.contains(position)).findFirst();
|
return rails.values().stream().filter(rail -> rail.contains(position)).findFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find all rails that *touch* (not contain) this position.
|
||||||
|
*
|
||||||
|
* @param position the position to check
|
||||||
|
* @return the rail(s) that touch this position
|
||||||
|
*/
|
||||||
|
public Rail[] findTouchingRails(Vector2D position) {
|
||||||
|
return rails.values().stream().filter(rail -> rail.canConnectTo(position)).toArray(Rail[]::new);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a rail that connects to or contains the first position and connects to or contains the second position.
|
* Find a rail that connects to or contains the first position and connects to or contains the second position.
|
||||||
*
|
*
|
||||||
@ -291,16 +280,6 @@ public class RailwayNetwork {
|
|||||||
.findFirst();
|
.findFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Find all rails that *touch* (not contain) this position.
|
|
||||||
*
|
|
||||||
* @param position the position to check
|
|
||||||
* @return the rail(s) that touch this position
|
|
||||||
*/
|
|
||||||
public Rail[] findTouchingRails(Vector2D position) {
|
|
||||||
return rails.values().stream().filter(rail -> rail.canConnectTo(position)).toArray(Rail[]::new);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether the rail network is ready for trains (all switches set, etc.).
|
* Check whether the rail network is ready for trains (all switches set, etc.).
|
||||||
*
|
*
|
||||||
|
@ -82,10 +82,8 @@ public final class Track extends Rail {
|
|||||||
} else if (direction.equals(getDirectionFrom(end))) {
|
} else if (direction.equals(getDirectionFrom(end))) {
|
||||||
return new Vector2D(start);
|
return new Vector2D(start);
|
||||||
} else if (position.equals(end) && !direction.equals(getDirectionFrom(start))) {
|
} else if (position.equals(end) && !direction.equals(getDirectionFrom(start))) {
|
||||||
direction.copyFrom(getDirectionFrom(end));
|
|
||||||
return move(position, getDirectionFrom(end), steps);
|
return move(position, getDirectionFrom(end), steps);
|
||||||
} else if (position.equals(start) && !direction.equals(getDirectionFrom(end))) {
|
} else if (position.equals(start) && !direction.equals(getDirectionFrom(end))) {
|
||||||
direction.copyFrom(getDirectionFrom(start));
|
|
||||||
return move(position, getDirectionFrom(start), steps);
|
return move(position, getDirectionFrom(start), steps);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -136,6 +136,8 @@ public final class Train implements Comparable<Train> {
|
|||||||
|
|
||||||
// successfully placed part of the train
|
// successfully placed part of the train
|
||||||
length -= distanceToLastPosition;
|
length -= distanceToLastPosition;
|
||||||
|
// update direction
|
||||||
|
positioningDirection.copyFrom(rollingStockPosition.subtract(positionList.getLast()).normalized());
|
||||||
|
|
||||||
final Rail occupiedRail = railNetwork.findRail(positionList.getLast(), rollingStockPosition).get();
|
final Rail occupiedRail = railNetwork.findRail(positionList.getLast(), rollingStockPosition).get();
|
||||||
if (occupiedRailsSet.contains(occupiedRail) && positionList.size() > 4) {
|
if (occupiedRailsSet.contains(occupiedRail) && positionList.size() > 4) {
|
||||||
|
Loading…
Reference in New Issue
Block a user