mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final1.git
synced 2024-11-24 01:15:05 +00:00
Checkstyle
This commit is contained in:
parent
b24b7cd83f
commit
3610cfac8d
@ -35,6 +35,9 @@ public class CommandLine {
|
|||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
Terminal.printError("number too big");
|
Terminal.printError("number too big");
|
||||||
continue;
|
continue;
|
||||||
|
} catch (InvalidInputException e) {
|
||||||
|
Terminal.printError(e.getMessage());
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
if (command != null) {
|
if (command != null) {
|
||||||
try {
|
try {
|
||||||
|
13
src/edu/kit/informatik/InvalidInputException.java
Normal file
13
src/edu/kit/informatik/InvalidInputException.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package edu.kit.informatik;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thrown on invalid user input.
|
||||||
|
*
|
||||||
|
* @author Arne Keller
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
public class InvalidInputException extends Exception {
|
||||||
|
public InvalidInputException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
@ -16,7 +16,7 @@ public class ModelRailwaySimulation {
|
|||||||
* @param end
|
* @param end
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public int addTrack(final Point start, final Point end) {
|
public int addTrack(final Vector2D start, final Vector2D end) {
|
||||||
if (start.distanceTo(end) == 0) {
|
if (start.distanceTo(end) == 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -52,7 +52,7 @@ public class ModelRailwaySimulation {
|
|||||||
* @param end2
|
* @param end2
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public int addSwitch(final Point start, final Point end1, final Point end2) {
|
public int addSwitch(final Vector2D start, final Vector2D end1, final Vector2D end2) {
|
||||||
if (start.distanceTo(end1) == 0 || start.distanceTo(end2) == 0 || end1.distanceTo(end2) == 0) {
|
if (start.distanceTo(end1) == 0 || start.distanceTo(end2) == 0 || end1.distanceTo(end2) == 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -173,7 +173,7 @@ public class ModelRailwaySimulation {
|
|||||||
* @param position
|
* @param position
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean setSwitch(int id, Point position) {
|
public boolean setSwitch(int id, Vector2D position) {
|
||||||
// TODO Falls Gleiseweichen auf denen ein Zug bereitssteht, geschaltet werden, entgleist der daraufstehende Zug
|
// TODO Falls Gleiseweichen auf denen ein Zug bereitssteht, geschaltet werden, entgleist der daraufstehende Zug
|
||||||
for (Rail rail : rails) {
|
for (Rail rail : rails) {
|
||||||
if (rail.getIdentifier() == id) {
|
if (rail.getIdentifier() == id) {
|
||||||
@ -428,8 +428,8 @@ public class ModelRailwaySimulation {
|
|||||||
* @param rawDirection
|
* @param rawDirection
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean putTrain(final int trainId, final Point position, final Point rawDirection) {
|
public boolean putTrain(final int trainId, final Vector2D position, final Vector2D rawDirection) {
|
||||||
final Point direction = rawDirection.normalized();
|
final Vector2D direction = rawDirection.normalized();
|
||||||
Train train = getTrain(trainId);
|
Train train = getTrain(trainId);
|
||||||
if (train == null) {
|
if (train == null) {
|
||||||
return false;
|
return false;
|
||||||
@ -439,10 +439,10 @@ public class ModelRailwaySimulation {
|
|||||||
}
|
}
|
||||||
int length = train.getLength();
|
int length = train.getLength();
|
||||||
|
|
||||||
List<Point> positions = new ArrayList<>();
|
List<Vector2D> positions = new ArrayList<>();
|
||||||
positions.add(position);
|
positions.add(position);
|
||||||
Point positioningDirection = direction.negated();
|
Vector2D positioningDirection = direction.negated();
|
||||||
Point rollingStockPosition = position;
|
Vector2D rollingStockPosition = position;
|
||||||
if (length > 10000) {
|
if (length > 10000) {
|
||||||
return false; // TODO: remove!
|
return false; // TODO: remove!
|
||||||
}
|
}
|
||||||
@ -526,9 +526,9 @@ public class ModelRailwaySimulation {
|
|||||||
if (!train.isPlaced()) {
|
if (!train.isPlaced()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Point position = train.getFrontPosition();
|
Vector2D position = train.getFrontPosition();
|
||||||
Point direction = train.getDirection();
|
Vector2D direction = train.getDirection();
|
||||||
Point nextPosition = move(position, direction);
|
Vector2D nextPosition = move(position, direction);
|
||||||
if (nextPosition == null
|
if (nextPosition == null
|
||||||
|| (train.isOnPosition(nextPosition)
|
|| (train.isOnPosition(nextPosition)
|
||||||
&& !train.getBackPosition().equals(train.getFrontPosition()))) {
|
&& !train.getBackPosition().equals(train.getFrontPosition()))) {
|
||||||
@ -593,10 +593,10 @@ public class ModelRailwaySimulation {
|
|||||||
if (!train.isPlaced()) {
|
if (!train.isPlaced()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Point front = train.getFrontPosition();
|
Vector2D front = train.getFrontPosition();
|
||||||
Point position = train.getBackPosition();
|
Vector2D position = train.getBackPosition();
|
||||||
Point direction = train.getBackDirection();
|
Vector2D direction = train.getBackDirection();
|
||||||
Point nextPosition = move(position, direction);
|
Vector2D nextPosition = move(position, direction);
|
||||||
if (nextPosition == null
|
if (nextPosition == null
|
||||||
|| (train.isOnPosition(nextPosition) && !train.getFrontPosition().equals(nextPosition))) {
|
|| (train.isOnPosition(nextPosition) && !train.getFrontPosition().equals(nextPosition))) {
|
||||||
collisions.add(new HashSet<>(Arrays.asList(train)));
|
collisions.add(new HashSet<>(Arrays.asList(train)));
|
||||||
@ -648,10 +648,10 @@ public class ModelRailwaySimulation {
|
|||||||
* @param direction has to be (0,1) (0,-1) (1,0) (-1,0) will be modified if necessary
|
* @param direction has to be (0,1) (0,-1) (1,0) (-1,0) will be modified if necessary
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private Point move(final Point position, final Point direction) {
|
private Vector2D move(final Vector2D position, final Vector2D direction) {
|
||||||
Rail containingRail = findContainingRail(position);
|
Rail containingRail = findContainingRail(position);
|
||||||
if (containingRail != null) {
|
if (containingRail != null) {
|
||||||
return position.moveBy(direction);
|
return position.add(direction);
|
||||||
}
|
}
|
||||||
Rail[] touchingRails = findTouchingRails(position);
|
Rail[] touchingRails = findTouchingRails(position);
|
||||||
if (touchingRails.length == 0) {
|
if (touchingRails.length == 0) {
|
||||||
@ -660,36 +660,36 @@ public class ModelRailwaySimulation {
|
|||||||
if (touchingRails.length == 1) {
|
if (touchingRails.length == 1) {
|
||||||
// at the end of a rail
|
// at the end of a rail
|
||||||
// either derail or move backwards
|
// either derail or move backwards
|
||||||
Point nextPosition = position.moveBy(direction);
|
Vector2D nextPosition = position.add(direction);
|
||||||
if (!touchingRails[0].contains(nextPosition)) {
|
if (!touchingRails[0].contains(nextPosition)) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
return nextPosition;
|
return nextPosition;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Point nextPosition = position.moveBy(direction);
|
Vector2D nextPosition = position.add(direction);
|
||||||
if (touchingRails[0].contains(nextPosition) || touchingRails[0].connectsTo(nextPosition)) {
|
if (touchingRails[0].contains(nextPosition) || touchingRails[0].connectsTo(nextPosition)) {
|
||||||
return nextPosition;
|
return nextPosition;
|
||||||
} else if (touchingRails[1].contains(nextPosition)) {
|
} else if (touchingRails[1].contains(nextPosition)) {
|
||||||
Point nextDirection = touchingRails[1].getDirectionFrom(position);
|
Vector2D nextDirection = touchingRails[1].getDirectionFrom(position);
|
||||||
direction.x = nextDirection.x;
|
direction.setX(nextDirection.getX());
|
||||||
direction.y = nextDirection.y;
|
direction.setY(nextDirection.getY());
|
||||||
return nextPosition;
|
return nextPosition;
|
||||||
}
|
}
|
||||||
Point previousPosition = position.moveBy(direction.negated());
|
Vector2D previousPosition = position.add(direction.negated());
|
||||||
Rail nextRail = (touchingRails[0].contains(previousPosition)
|
Rail nextRail = (touchingRails[0].contains(previousPosition)
|
||||||
|| touchingRails[0].canConnectTo(previousPosition)) ? touchingRails[1] : touchingRails[0];
|
|| touchingRails[0].canConnectTo(previousPosition)) ? touchingRails[1] : touchingRails[0];
|
||||||
Point nextDirection = nextRail.getDirectionFrom(position);
|
Vector2D nextDirection = nextRail.getDirectionFrom(position);
|
||||||
if (nextDirection != null) {
|
if (nextDirection != null) {
|
||||||
direction.x = nextDirection.x;
|
direction.setX(nextDirection.getX());
|
||||||
direction.y = nextDirection.y;
|
direction.setY(nextDirection.getY());
|
||||||
return position.moveBy(direction);
|
return position.add(direction);
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Rail findContainingRail(final Point position) {
|
private Rail findContainingRail(final Vector2D position) {
|
||||||
for (Rail rail : rails) {
|
for (Rail rail : rails) {
|
||||||
if (rail.contains(position)) {
|
if (rail.contains(position)) {
|
||||||
return rail;
|
return rail;
|
||||||
@ -698,7 +698,7 @@ public class ModelRailwaySimulation {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Rail[] findTouchingRails(final Point position) {
|
private Rail[] findTouchingRails(final Vector2D position) {
|
||||||
return rails.stream().filter((rail) -> rail.canConnectTo(position)).toArray(Rail[]::new);
|
return rails.stream().filter((rail) -> rail.canConnectTo(position)).toArray(Rail[]::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,83 +0,0 @@
|
|||||||
package edu.kit.informatik;
|
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public class Point {
|
|
||||||
public int x;
|
|
||||||
public int y;
|
|
||||||
|
|
||||||
public Point(int x, int y) {
|
|
||||||
this.x = x;
|
|
||||||
this.y = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* assumes good input like x,y
|
|
||||||
* @param input
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static Point parse(final String input) {
|
|
||||||
String[] coordinates = input.split(",", 2);
|
|
||||||
int x = Integer.parseInt(coordinates[0]);
|
|
||||||
int y = Integer.parseInt(coordinates[1]);
|
|
||||||
return new Point(x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return String.format("(%d,%d)", x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Taxicab distance to other point
|
|
||||||
* @param other
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public int distanceTo(final Point other) {
|
|
||||||
return Math.abs(this.x - other.x) + Math.abs(this.y - other.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* normalize x and y *separately*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public Point normalized() {
|
|
||||||
if (x >= 0 && x <= 1 && y >= 0 && y <= 1) {
|
|
||||||
return this;
|
|
||||||
} else if (x != 0 && y != 0) {
|
|
||||||
return new Point(x / Math.abs(x), y / Math.abs(y));
|
|
||||||
} else if (x != 0) {
|
|
||||||
return new Point(x / Math.abs(x), 0);
|
|
||||||
} else { // y != 0
|
|
||||||
return new Point(0, y / Math.abs(y));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Point negated() {
|
|
||||||
return new Point(-x, -y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Point subtract(Point other) {
|
|
||||||
return new Point(x - other.x, y - other.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Point moveBy(final Point movement) {
|
|
||||||
return new Point(x + movement.x, y + movement.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(final Object obj) {
|
|
||||||
if (obj != null && getClass().equals(obj.getClass())) {
|
|
||||||
final Point point = (Point) obj;
|
|
||||||
|
|
||||||
return x == point.x && y == point.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(x, y);
|
|
||||||
}
|
|
||||||
}
|
|
@ -17,13 +17,13 @@ public abstract class Rail {
|
|||||||
* @param point point to check for connection
|
* @param point point to check for connection
|
||||||
* @return whether the rail currently connects to the point
|
* @return whether the rail currently connects to the point
|
||||||
*/
|
*/
|
||||||
public abstract boolean connectsTo(Point point);
|
public abstract boolean connectsTo(Vector2D point);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param point point to check for connection
|
* @param point point to check for connection
|
||||||
* @return whether the rail can connect to the point
|
* @return whether the rail can connect to the point
|
||||||
*/
|
*/
|
||||||
public abstract boolean canConnectTo(Point point);
|
public abstract boolean canConnectTo(Vector2D point);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param rail rail to check for connection
|
* @param rail rail to check for connection
|
||||||
@ -36,7 +36,7 @@ public abstract class Rail {
|
|||||||
* @param position
|
* @param position
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public abstract boolean switchTo(Point position);
|
public abstract boolean switchTo(Vector2D position);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return whether the rail is ready for trains running on it
|
* @return whether the rail is ready for trains running on it
|
||||||
@ -47,7 +47,7 @@ public abstract class Rail {
|
|||||||
* @param position the point to check
|
* @param position the point to check
|
||||||
* @return whether the point is inside this rail (not on the edge)
|
* @return whether the point is inside this rail (not on the edge)
|
||||||
*/
|
*/
|
||||||
public abstract boolean contains(Point position);
|
public abstract boolean contains(Vector2D position);
|
||||||
|
|
||||||
public abstract Point getDirectionFrom(Point position);
|
public abstract Vector2D getDirectionFrom(Vector2D position);
|
||||||
}
|
}
|
||||||
|
@ -2,13 +2,43 @@ package edu.kit.informatik;
|
|||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A switch in the rail network. It connects a start position to two other positions.
|
||||||
|
*
|
||||||
|
* @author Arne Keller
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
public final class Switch extends Rail {
|
public final class Switch extends Rail {
|
||||||
public final Point start;
|
/**
|
||||||
public final Point end1;
|
* Start position.
|
||||||
public final Point end2;
|
*/
|
||||||
public Point selection;
|
private final Vector2D start;
|
||||||
|
/**
|
||||||
|
* End position one, has to be in a straight line from start.
|
||||||
|
*/
|
||||||
|
private final Vector2D end1;
|
||||||
|
/**
|
||||||
|
* End position two, has to be in a straight line from start.
|
||||||
|
*/
|
||||||
|
private final Vector2D end2;
|
||||||
|
/**
|
||||||
|
* Currently selected position (either one or two).
|
||||||
|
*/
|
||||||
|
private Vector2D selection;
|
||||||
|
|
||||||
public Switch(Point start, Point end1, Point end2, int id) {
|
/**
|
||||||
|
* Construct a new switch.
|
||||||
|
* @param start start position
|
||||||
|
* @param end1 end position 1
|
||||||
|
* @param end2 end position 2
|
||||||
|
* @param id identifier of this switch
|
||||||
|
* @throws IllegalArgumentException if the switch is not composed of straight lines
|
||||||
|
*/
|
||||||
|
public Switch(Vector2D start, Vector2D end1, Vector2D end2, int id) throws IllegalArgumentException {
|
||||||
|
if (start.getX() != end1.getX() && start.getY() != end1.getY()
|
||||||
|
|| start.getX() != end2.getX() && start.getY() != end2.getY()) {
|
||||||
|
throw new IllegalArgumentException("start has to be connected in straight lines to end positions!");
|
||||||
|
}
|
||||||
this.start = start;
|
this.start = start;
|
||||||
this.end1 = end1;
|
this.end1 = end1;
|
||||||
this.end2 = end2;
|
this.end2 = end2;
|
||||||
@ -16,12 +46,12 @@ public final class Switch extends Rail {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canConnectTo(Point point) {
|
public boolean canConnectTo(Vector2D point) {
|
||||||
return point.equals(start) || point.equals(end1) || point.equals(end2);
|
return point.equals(start) || point.equals(end1) || point.equals(end2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean connectsTo(Point point) {
|
public boolean connectsTo(Vector2D point) {
|
||||||
return point.equals(start) || point.equals(selection);
|
return point.equals(start) || point.equals(selection);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,9 +61,9 @@ public final class Switch extends Rail {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean switchTo(Point position) {
|
public boolean switchTo(Vector2D position) {
|
||||||
if (position.equals(end1) || position.equals(end2)) {
|
if (position.equals(end1) || position.equals(end2)) {
|
||||||
selection = new Point(position.x, position.y);
|
selection = new Vector2D(position.getX(), position.getY());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -45,34 +75,34 @@ public final class Switch extends Rail {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(final Point position) {
|
public boolean contains(final Vector2D position) {
|
||||||
if (selection == null) {
|
if (selection == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (start.x == selection.x && position.x == start.x) {
|
if (start.getX() == selection.getX() && position.getX() == start.getX()) {
|
||||||
if (start.y < selection.y) {
|
if (start.getY() < selection.getY()) {
|
||||||
return start.y < position.y && position.y < selection.y;
|
return start.getY() < position.getY() && position.getY() < selection.getY();
|
||||||
} else {
|
} else {
|
||||||
return start.y > position.y && position.y > selection.y;
|
return start.getY() > position.getY() && position.getY() > selection.getY();
|
||||||
}
|
}
|
||||||
} else if (start.y == selection.y && position.y == start.y) {
|
} else if (start.getY() == selection.getY() && position.getY() == start.getY()) {
|
||||||
if (start.x < selection.x) {
|
if (start.getX() < selection.getX()) {
|
||||||
return start.x < position.x && position.x < selection.x;
|
return start.getX() < position.getX() && position.getX() < selection.getX();
|
||||||
} else {
|
} else {
|
||||||
return start.x > position.x && position.x > selection.x;
|
return start.getX() > position.getX() && position.getX() > selection.getX();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Point getDirectionFrom(Point position) {
|
public Vector2D getDirectionFrom(Vector2D position) {
|
||||||
if (selection == null) {
|
if (selection == null) {
|
||||||
return null;
|
return null;
|
||||||
} else if (start.equals(position)) {
|
} else if (start.equals(position)) {
|
||||||
return new Point(selection.x - start.x, selection.y - start.y).normalized();
|
return new Vector2D(selection.getX() - start.getX(), selection.getY() - start.getY()).normalized();
|
||||||
} else if (selection.equals(position)) {
|
} else if (selection.equals(position)) {
|
||||||
return new Point(start.x - selection.x, start.y - selection.y).normalized();
|
return new Vector2D(start.getX() - selection.getX(), start.getY() - selection.getY()).normalized();
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -3,28 +3,38 @@ package edu.kit.informatik;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Straight track.
|
* A straight track.
|
||||||
*
|
*
|
||||||
* @author Arne Keller
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public final class Track extends Rail {
|
public final class Track extends Rail {
|
||||||
public final Point start;
|
private final Vector2D start;
|
||||||
public final Point end;
|
private final Vector2D end;
|
||||||
|
|
||||||
public Track(final Point start, final Point end, final int id) {
|
/**
|
||||||
|
* Construct a new track. Start and end positions have to be on a straight line!
|
||||||
|
* @param start start position of the track
|
||||||
|
* @param end end position of the track
|
||||||
|
* @param id identifier to use
|
||||||
|
* @throws IllegalArgumentException if the positions are not on a straight line
|
||||||
|
*/
|
||||||
|
public Track(final Vector2D start, final Vector2D end, final int id) throws IllegalArgumentException {
|
||||||
|
if (start.getX() != end.getX() && start.getY() != end.getY()) {
|
||||||
|
throw new IllegalArgumentException("start and end have to be in a straight line!");
|
||||||
|
}
|
||||||
this.start = start;
|
this.start = start;
|
||||||
this.end = end;
|
this.end = end;
|
||||||
super.id = id;
|
super.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canConnectTo(Point point) {
|
public boolean canConnectTo(Vector2D point) {
|
||||||
return point.equals(start) || point.equals(end);
|
return point.equals(start) || point.equals(end);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean connectsTo(Point point) {
|
public boolean connectsTo(Vector2D point) {
|
||||||
return canConnectTo(point);
|
return canConnectTo(point);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +47,7 @@ public final class Track extends Rail {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean switchTo(Point position) {
|
public boolean switchTo(Vector2D position) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,27 +57,27 @@ public final class Track extends Rail {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(Point position) {
|
public boolean contains(Vector2D position) {
|
||||||
if (start.x == end.x && position.x == start.x) {
|
if (start.getX() == end.getX() && position.getX() == start.getX()) {
|
||||||
int dy0 = Math.abs(start.y - end.y);
|
int dy0 = Math.abs(start.getY() - end.getY());
|
||||||
int dy1 = Math.abs(start.y - position.y);
|
int dy1 = Math.abs(start.getY() - position.getY());
|
||||||
int dy2 = Math.abs(end.y - position.y);
|
int dy2 = Math.abs(end.getY() - position.getY());
|
||||||
return dy1 > 0 && dy2 > 0 && dy1 < dy0 && dy2 < dy0;
|
return dy1 > 0 && dy2 > 0 && dy1 < dy0 && dy2 < dy0;
|
||||||
} else if (start.y == end.y && position.y == start.y) {
|
} else if (start.getY() == end.getY() && position.getY() == start.getY()) {
|
||||||
int dx0 = Math.abs(start.x - end.x);
|
int dx0 = Math.abs(start.getX() - end.getX());
|
||||||
int dx1 = Math.abs(start.x - position.x);
|
int dx1 = Math.abs(start.getX() - position.getX());
|
||||||
int dx2 = Math.abs(end.x - position.x);
|
int dx2 = Math.abs(end.getX() - position.getX());
|
||||||
return dx1 > 0 && dx2 > 0 && dx1 < dx0 && dx2 < dx0;
|
return dx1 > 0 && dx2 > 0 && dx1 < dx0 && dx2 < dx0;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Point getDirectionFrom(Point position) {
|
public Vector2D getDirectionFrom(Vector2D position) {
|
||||||
if (start.equals(position)) {
|
if (start.equals(position)) {
|
||||||
return new Point(end.x - start.x, end.y - start.y).normalized();
|
return new Vector2D(end.getX() - start.getX(), end.getY() - start.getY()).normalized();
|
||||||
} else if (end.equals(position)) {
|
} else if (end.equals(position)) {
|
||||||
return new Point(start.x - end.x, start.y - end.y).normalized();
|
return new Vector2D(start.getX() - end.getX(), start.getY() - end.getY()).normalized();
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,8 @@ import java.util.List;
|
|||||||
public final class Train {
|
public final class Train {
|
||||||
private final int identifier;
|
private final int identifier;
|
||||||
private final List<RollingStock> rollingStocks = new ArrayList<>();
|
private final List<RollingStock> rollingStocks = new ArrayList<>();
|
||||||
private List<Point> position;
|
private List<Vector2D> position;
|
||||||
private Point direction;
|
private Vector2D direction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new train.
|
* Construct a new train.
|
||||||
@ -93,7 +93,7 @@ public final class Train {
|
|||||||
return position != null && direction != null;
|
return position != null && direction != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void storePositionAndDirection(final List<Point> positions, final Point direction) {
|
public void storePositionAndDirection(final List<Vector2D> positions, final Vector2D direction) {
|
||||||
this.position = positions;
|
this.position = positions;
|
||||||
this.direction = direction;
|
this.direction = direction;
|
||||||
}
|
}
|
||||||
@ -112,7 +112,7 @@ public final class Train {
|
|||||||
|| position.stream().filter(rail::connectsTo).count() == 2;
|
|| position.stream().filter(rail::connectsTo).count() == 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point getFrontPosition() {
|
public Vector2D getFrontPosition() {
|
||||||
if (position != null) {
|
if (position != null) {
|
||||||
return position.get(0);
|
return position.get(0);
|
||||||
} else {
|
} else {
|
||||||
@ -120,7 +120,7 @@ public final class Train {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point getBackPosition() {
|
public Vector2D getBackPosition() {
|
||||||
if (position != null) {
|
if (position != null) {
|
||||||
return position.get(position.size() - 1);
|
return position.get(position.size() - 1);
|
||||||
} else {
|
} else {
|
||||||
@ -128,11 +128,11 @@ public final class Train {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point getDirection() {
|
public Vector2D getDirection() {
|
||||||
return direction;
|
return direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point getBackDirection() {
|
public Vector2D getBackDirection() {
|
||||||
if (position.size() == 1) {
|
if (position.size() == 1) {
|
||||||
return direction.negated();
|
return direction.negated();
|
||||||
} else {
|
} else {
|
||||||
@ -140,17 +140,17 @@ public final class Train {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void moveTo(Point nextPosition) {
|
public void moveTo(Vector2D nextPosition) {
|
||||||
position.add(0, nextPosition);
|
position.add(0, nextPosition);
|
||||||
position.remove(position.size() - 1);
|
position.remove(position.size() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void moveBackTo(Point backPosition) {
|
public void moveBackTo(Vector2D backPosition) {
|
||||||
position.remove(0);
|
position.remove(0);
|
||||||
position.add(backPosition);
|
position.add(backPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDirection(Point newDirection) {
|
public void setDirection(Vector2D newDirection) {
|
||||||
direction = newDirection;
|
direction = newDirection;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,11 +163,11 @@ public final class Train {
|
|||||||
return other.isOnAnyPosition(position);
|
return other.isOnAnyPosition(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isOnPosition(Point point) {
|
public boolean isOnPosition(Vector2D point) {
|
||||||
return position.contains(point);
|
return position.contains(point);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isOnAnyPosition(List<Point> positions) {
|
public boolean isOnAnyPosition(List<Vector2D> positions) {
|
||||||
return position.stream().anyMatch(positions::contains);
|
return position.stream().anyMatch(positions::contains);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
140
src/edu/kit/informatik/Vector2D.java
Normal file
140
src/edu/kit/informatik/Vector2D.java
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
package edu.kit.informatik;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A two-dimensional vector, usually a position or movement direction vector.
|
||||||
|
*
|
||||||
|
* @author Arne Keller
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
public class Vector2D {
|
||||||
|
/**
|
||||||
|
* First coordinate of the vector, usually referred to as 'x'.
|
||||||
|
*/
|
||||||
|
private int x;
|
||||||
|
/**
|
||||||
|
* Second coordinate of the vector, usally referred to as 'y'.
|
||||||
|
*/
|
||||||
|
private int y;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a new vector.
|
||||||
|
* @param x first component
|
||||||
|
* @param y second component
|
||||||
|
*/
|
||||||
|
public Vector2D(int x, int y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param input two numbers separated by a comma
|
||||||
|
* @return a vector containing the two numbers, null otherwise
|
||||||
|
*/
|
||||||
|
public static Vector2D parse(final String input) {
|
||||||
|
String[] coordinates = input.split(",", 2);
|
||||||
|
int x = Integer.parseInt(coordinates[0]);
|
||||||
|
int y = Integer.parseInt(coordinates[1]);
|
||||||
|
return new Vector2D(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("(%d,%d)", x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the distance between this vector (interpreted as position) and another position.
|
||||||
|
* @param other the point to measure distance to
|
||||||
|
* @return the manhattan distance
|
||||||
|
*/
|
||||||
|
public int distanceTo(final Vector2D other) {
|
||||||
|
return Math.abs(this.x - other.x) + Math.abs(this.y - other.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return a vector with separately (!) normalized components
|
||||||
|
*/
|
||||||
|
public Vector2D normalized() {
|
||||||
|
if (x >= 0 && x <= 1 && y >= 0 && y <= 1) {
|
||||||
|
return this;
|
||||||
|
} else if (x != 0 && y != 0) {
|
||||||
|
return new Vector2D(x / Math.abs(x), y / Math.abs(y));
|
||||||
|
} else if (x != 0) {
|
||||||
|
return new Vector2D(x / Math.abs(x), 0);
|
||||||
|
} else { // y != 0
|
||||||
|
return new Vector2D(0, y / Math.abs(y));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return (-1) * this vector
|
||||||
|
*/
|
||||||
|
public Vector2D negated() {
|
||||||
|
return new Vector2D(-x, -y);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subtract another vector from this one.
|
||||||
|
* @param other another vector
|
||||||
|
* @return the difference of this vector and the other vector
|
||||||
|
*/
|
||||||
|
public Vector2D subtract(Vector2D other) {
|
||||||
|
return new Vector2D(x - other.x, y - other.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param movement vector to add
|
||||||
|
* @return component-wise sum of this vector and the other vector
|
||||||
|
*/
|
||||||
|
public Vector2D add(final Vector2D movement) {
|
||||||
|
return new Vector2D(x + movement.x, y + movement.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the first component of this vector
|
||||||
|
*/
|
||||||
|
public int getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the second component of this vector
|
||||||
|
*/
|
||||||
|
public int getY() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change the value of the first component of the vector
|
||||||
|
* @param x value to change to
|
||||||
|
*/
|
||||||
|
public void setX(int x) {
|
||||||
|
this.x = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change the value of the second component of the vector
|
||||||
|
* @param y value to change to
|
||||||
|
*/
|
||||||
|
public void setY(int y) {
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(final Object obj) {
|
||||||
|
if (obj != null && getClass().equals(obj.getClass())) {
|
||||||
|
final Vector2D point = (Vector2D) obj;
|
||||||
|
|
||||||
|
return x == point.x && y == point.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(x, y);
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package edu.kit.informatik.command;
|
package edu.kit.informatik.command;
|
||||||
|
|
||||||
import edu.kit.informatik.ModelRailwaySimulation;
|
import edu.kit.informatik.ModelRailwaySimulation;
|
||||||
import edu.kit.informatik.Point;
|
import edu.kit.informatik.Vector2D;
|
||||||
import edu.kit.informatik.Terminal;
|
import edu.kit.informatik.Terminal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -11,9 +11,9 @@ import edu.kit.informatik.Terminal;
|
|||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class AddSwitch implements Command {
|
public class AddSwitch implements Command {
|
||||||
private final Point start;
|
private final Vector2D start;
|
||||||
private final Point end1;
|
private final Vector2D end1;
|
||||||
private final Point end2;
|
private final Vector2D end2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new 'add switch' command.
|
* Construct a new 'add switch' command.
|
||||||
@ -21,13 +21,12 @@ public class AddSwitch implements Command {
|
|||||||
* @param end1 end position 1
|
* @param end1 end position 1
|
||||||
* @param end2 end position 2
|
* @param end2 end position 2
|
||||||
*/
|
*/
|
||||||
public AddSwitch(final Point start, final Point end1, final Point end2) {
|
public AddSwitch(final Vector2D start, final Vector2D end1, final Vector2D end2) {
|
||||||
this.start = start;
|
this.start = start;
|
||||||
this.end1 = end1;
|
this.end1 = end1;
|
||||||
this.end2 = end2;
|
this.end2 = end2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void apply(ModelRailwaySimulation simulation) {
|
public void apply(ModelRailwaySimulation simulation) {
|
||||||
int id = simulation.addSwitch(start, end1, end2);
|
int id = simulation.addSwitch(start, end1, end2);
|
||||||
if (id == -1) {
|
if (id == -1) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package edu.kit.informatik.command;
|
package edu.kit.informatik.command;
|
||||||
|
|
||||||
import edu.kit.informatik.ModelRailwaySimulation;
|
import edu.kit.informatik.ModelRailwaySimulation;
|
||||||
import edu.kit.informatik.Point;
|
import edu.kit.informatik.Vector2D;
|
||||||
import edu.kit.informatik.Terminal;
|
import edu.kit.informatik.Terminal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -11,20 +11,19 @@ import edu.kit.informatik.Terminal;
|
|||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class AddTrack implements Command {
|
public class AddTrack implements Command {
|
||||||
private Point start;
|
private Vector2D start;
|
||||||
private Point end;
|
private Vector2D end;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new 'add track' command.
|
* Construct a new 'add track' command.
|
||||||
* @param start position of the start of the track
|
* @param start position of the start of the track
|
||||||
* @param end position of the end of the track
|
* @param end position of the end of the track
|
||||||
*/
|
*/
|
||||||
public AddTrack(final Point start, final Point end) {
|
public AddTrack(final Vector2D start, final Vector2D end) {
|
||||||
this.start = start;
|
this.start = start;
|
||||||
this.end = end;
|
this.end = end;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
if (start.equals(end)) {
|
if (start.equals(end)) {
|
||||||
Terminal.printError("track has length 0");
|
Terminal.printError("track has length 0");
|
||||||
|
@ -24,7 +24,6 @@ public class AddTrain implements Command {
|
|||||||
this.rollingStockId = rollingStockId;
|
this.rollingStockId = rollingStockId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void apply(ModelRailwaySimulation simulation) {
|
public void apply(ModelRailwaySimulation simulation) {
|
||||||
if (simulation.addTrain(trainId, rollingStockId)) {
|
if (simulation.addTrain(trainId, rollingStockId)) {
|
||||||
RollingStock rollingStock = simulation.getRollingStock(rollingStockId);
|
RollingStock rollingStock = simulation.getRollingStock(rollingStockId);
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
package edu.kit.informatik.command;
|
package edu.kit.informatik.command;
|
||||||
|
|
||||||
import edu.kit.informatik.CoachType;
|
import edu.kit.informatik.*;
|
||||||
import edu.kit.informatik.EngineType;
|
|
||||||
import edu.kit.informatik.Point;
|
|
||||||
import edu.kit.informatik.Terminal;
|
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
@ -57,8 +54,9 @@ public class CommandFactory {
|
|||||||
* Parse a single line of user input into one command.
|
* Parse a single line of user input into one command.
|
||||||
* @param command user input line
|
* @param command user input line
|
||||||
* @return a fully specified command object
|
* @return a fully specified command object
|
||||||
|
* @throws InvalidInputException if user input is invalid
|
||||||
*/
|
*/
|
||||||
public static Command getCommand(final String command) {
|
public static Command getCommand(final String command) throws InvalidInputException {
|
||||||
if (command.startsWith(ADD_TRACK)) {
|
if (command.startsWith(ADD_TRACK)) {
|
||||||
String arguments = command.substring(ADD_TRACK.length());
|
String arguments = command.substring(ADD_TRACK.length());
|
||||||
Matcher matcher = ADD_TRACK_ARGUMENTS.matcher(arguments);
|
Matcher matcher = ADD_TRACK_ARGUMENTS.matcher(arguments);
|
||||||
@ -66,9 +64,9 @@ public class CommandFactory {
|
|||||||
Terminal.printError("invalid add track argument syntax");
|
Terminal.printError("invalid add track argument syntax");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Point start = Point.parse(matcher.group(1));
|
Vector2D start = Vector2D.parse(matcher.group(1));
|
||||||
Point end = Point.parse(matcher.group(2));
|
Vector2D end = Vector2D.parse(matcher.group(2));
|
||||||
if (start.x == end.x || start.y == end.y) {
|
if (start.getX() == end.getX() || start.getY() == end.getY()) {
|
||||||
return new AddTrack(start, end);
|
return new AddTrack(start, end);
|
||||||
} else {
|
} else {
|
||||||
Terminal.printError("invalid track segment: not a straight line");
|
Terminal.printError("invalid track segment: not a straight line");
|
||||||
@ -81,14 +79,13 @@ public class CommandFactory {
|
|||||||
Terminal.printError("invalid add switch argument syntax");
|
Terminal.printError("invalid add switch argument syntax");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Point start = Point.parse(matcher.group(1));
|
Vector2D start = Vector2D.parse(matcher.group(1));
|
||||||
Point end1 = Point.parse(matcher.group(2));
|
Vector2D end1 = Vector2D.parse(matcher.group(2));
|
||||||
Point end2 = Point.parse(matcher.group(3));
|
Vector2D end2 = Vector2D.parse(matcher.group(3));
|
||||||
if ((start.x == end1.x || start.y == end1.y) && (start.x == end2.x || start.y == end2.y)) {
|
if ((start.getX() == end1.getX() || start.getY() == end1.getY()) && (start.getX() == end2.getX() || start.getY() == end2.getY())) {
|
||||||
return new AddSwitch(start, end1, end2);
|
return new AddSwitch(start, end1, end2);
|
||||||
} else {
|
} else {
|
||||||
Terminal.printError("invalid switch: not consisting of straight lines");
|
throw new InvalidInputException("switch rails have to be straight lines");
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
} else if (command.startsWith(DELETE_TRACK)) {
|
} else if (command.startsWith(DELETE_TRACK)) {
|
||||||
String argument = command.substring(DELETE_TRACK.length());
|
String argument = command.substring(DELETE_TRACK.length());
|
||||||
@ -112,7 +109,7 @@ public class CommandFactory {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
int id = Integer.parseInt(matcher.group(1));
|
int id = Integer.parseInt(matcher.group(1));
|
||||||
Point point = Point.parse(matcher.group(2));
|
Vector2D point = Vector2D.parse(matcher.group(2));
|
||||||
return new SetSwitch(id, point);
|
return new SetSwitch(id, point);
|
||||||
} else if (command.startsWith(CREATE_ENGINE)) {
|
} else if (command.startsWith(CREATE_ENGINE)) {
|
||||||
String arguments = command.substring(CREATE_ENGINE.length());
|
String arguments = command.substring(CREATE_ENGINE.length());
|
||||||
@ -228,7 +225,7 @@ public class CommandFactory {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
int id = Integer.parseInt(matcher.group(1));
|
int id = Integer.parseInt(matcher.group(1));
|
||||||
Point point = Point.parse(matcher.group(2));
|
Vector2D point = Vector2D.parse(matcher.group(2));
|
||||||
int x = Integer.parseInt(matcher.group(3));
|
int x = Integer.parseInt(matcher.group(3));
|
||||||
int y = Integer.parseInt(matcher.group(4));
|
int y = Integer.parseInt(matcher.group(4));
|
||||||
if (x != 0 && y != 0) {
|
if (x != 0 && y != 0) {
|
||||||
|
@ -31,7 +31,6 @@ public class CreateCoach implements Command {
|
|||||||
this.couplingBack = couplingBack;
|
this.couplingBack = couplingBack;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void apply(ModelRailwaySimulation simulation) {
|
public void apply(ModelRailwaySimulation simulation) {
|
||||||
int id = simulation.createCoach(type, length, couplingFront, couplingBack);
|
int id = simulation.createCoach(type, length, couplingFront, couplingBack);
|
||||||
if (id == -1) {
|
if (id == -1) {
|
||||||
|
@ -35,7 +35,6 @@ public class CreateEngine implements Command {
|
|||||||
this.couplingBack = couplingBack;
|
this.couplingBack = couplingBack;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
final Engine engine;
|
final Engine engine;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
@ -34,7 +34,6 @@ public class CreateTrainSet implements Command {
|
|||||||
this.couplingBack = couplingBack;
|
this.couplingBack = couplingBack;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
TrainSet trainSet = new TrainSet(series, name, length, couplingFront, couplingBack);
|
TrainSet trainSet = new TrainSet(series, name, length, couplingFront, couplingBack);
|
||||||
if (simulation.createTrainSet(trainSet)) {
|
if (simulation.createTrainSet(trainSet)) {
|
||||||
|
@ -20,7 +20,6 @@ public class DeleteRollingStock implements Command {
|
|||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
if (simulation.deleteRollingStock(id)) {
|
if (simulation.deleteRollingStock(id)) {
|
||||||
Terminal.printLine("OK");
|
Terminal.printLine("OK");
|
||||||
|
@ -20,7 +20,6 @@ public class DeleteTrack implements Command {
|
|||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
if (simulation.removeRail(id)) {
|
if (simulation.removeRail(id)) {
|
||||||
Terminal.printLine("OK");
|
Terminal.printLine("OK");
|
||||||
|
@ -20,7 +20,6 @@ public class DeleteTrain implements Command {
|
|||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
if (simulation.deleteTrain(id)) {
|
if (simulation.deleteTrain(id)) {
|
||||||
Terminal.printLine("OK");
|
Terminal.printLine("OK");
|
||||||
|
@ -9,7 +9,6 @@ import edu.kit.informatik.ModelRailwaySimulation;
|
|||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class ListCoaches implements Command {
|
public class ListCoaches implements Command {
|
||||||
@Override
|
|
||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
simulation.printCoaches();
|
simulation.printCoaches();
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,12 @@ package edu.kit.informatik.command;
|
|||||||
import edu.kit.informatik.ModelRailwaySimulation;
|
import edu.kit.informatik.ModelRailwaySimulation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command used to list engines.
|
* Command used to print a list of engines.
|
||||||
*
|
*
|
||||||
* @author Arne Keller
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class ListEngines implements Command {
|
public class ListEngines implements Command {
|
||||||
@Override
|
|
||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
simulation.printEngines();
|
simulation.printEngines();
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,12 @@ package edu.kit.informatik.command;
|
|||||||
import edu.kit.informatik.ModelRailwaySimulation;
|
import edu.kit.informatik.ModelRailwaySimulation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command used to list tracks and switches.
|
* Command used to print a list of tracks and switches.
|
||||||
*
|
*
|
||||||
* @author Arne Keller
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class ListTracks implements Command {
|
public class ListTracks implements Command {
|
||||||
@Override
|
|
||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
simulation.printTracks();
|
simulation.printTracks();
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,12 @@ package edu.kit.informatik.command;
|
|||||||
import edu.kit.informatik.ModelRailwaySimulation;
|
import edu.kit.informatik.ModelRailwaySimulation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command used to list train sets.
|
* Command used to print a list of train sets on the terminal.
|
||||||
*
|
*
|
||||||
* @author Arne Keller
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class ListTrainSets implements Command {
|
public class ListTrainSets implements Command {
|
||||||
@Override
|
|
||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
simulation.printTrainSets();
|
simulation.printTrainSets();
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,12 @@ package edu.kit.informatik.command;
|
|||||||
import edu.kit.informatik.ModelRailwaySimulation;
|
import edu.kit.informatik.ModelRailwaySimulation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command used to list trains.
|
* Command used to print a list of trains on the terminal.
|
||||||
*
|
*
|
||||||
* @author Arne Keller
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class ListTrains implements Command {
|
public class ListTrains implements Command {
|
||||||
@Override
|
|
||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
simulation.printTrains();
|
simulation.printTrains();
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package edu.kit.informatik.command;
|
package edu.kit.informatik.command;
|
||||||
|
|
||||||
import edu.kit.informatik.ModelRailwaySimulation;
|
import edu.kit.informatik.ModelRailwaySimulation;
|
||||||
import edu.kit.informatik.Point;
|
import edu.kit.informatik.Vector2D;
|
||||||
import edu.kit.informatik.Terminal;
|
import edu.kit.informatik.Terminal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -12,7 +12,7 @@ import edu.kit.informatik.Terminal;
|
|||||||
*/
|
*/
|
||||||
public class PutTrain implements Command {
|
public class PutTrain implements Command {
|
||||||
private final int id;
|
private final int id;
|
||||||
private final Point point;
|
private final Vector2D point;
|
||||||
private final int x;
|
private final int x;
|
||||||
private final int y;
|
private final int y;
|
||||||
|
|
||||||
@ -23,16 +23,15 @@ public class PutTrain implements Command {
|
|||||||
* @param x initial x direction
|
* @param x initial x direction
|
||||||
* @param y initial y direction
|
* @param y initial y direction
|
||||||
*/
|
*/
|
||||||
public PutTrain(final int id, final Point point, final int x, final int y) {
|
public PutTrain(final int id, final Vector2D point, final int x, final int y) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.point = point;
|
this.point = point;
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
if (simulation.putTrain(id, point, new Point(x, y))) {
|
if (simulation.putTrain(id, point, new Vector2D(x, y))) {
|
||||||
Terminal.printLine("OK");
|
Terminal.printLine("OK");
|
||||||
} else {
|
} else {
|
||||||
Terminal.printError("could not place train");
|
Terminal.printError("could not place train");
|
||||||
|
@ -1,30 +1,29 @@
|
|||||||
package edu.kit.informatik.command;
|
package edu.kit.informatik.command;
|
||||||
|
|
||||||
import edu.kit.informatik.ModelRailwaySimulation;
|
import edu.kit.informatik.ModelRailwaySimulation;
|
||||||
import edu.kit.informatik.Point;
|
import edu.kit.informatik.Vector2D;
|
||||||
import edu.kit.informatik.Terminal;
|
import edu.kit.informatik.Terminal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command used to set the position a switch is set to.
|
* Command used to specify the position a switch is set to.
|
||||||
*
|
*
|
||||||
* @author Arne Keller
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public class SetSwitch implements Command {
|
public class SetSwitch implements Command {
|
||||||
private final int id;
|
private final int id;
|
||||||
private final Point point;
|
private final Vector2D point;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new 'set switch' command.
|
* Construct a new 'set switch' command.
|
||||||
* @param id identifier of the switch
|
* @param id identifier of the switch
|
||||||
* @param point position to set the switch to
|
* @param point position to set the switch to
|
||||||
*/
|
*/
|
||||||
public SetSwitch(final int id, final Point point) {
|
public SetSwitch(final int id, final Vector2D point) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.point = point;
|
this.point = point;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
if (simulation.setSwitch(id, point)) {
|
if (simulation.setSwitch(id, point)) {
|
||||||
Terminal.printLine("OK");
|
Terminal.printLine("OK");
|
||||||
|
@ -19,7 +19,6 @@ public class ShowTrain implements Command {
|
|||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
simulation.printTrain(id);
|
simulation.printTrain(id);
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package edu.kit.informatik.command;
|
|||||||
import edu.kit.informatik.ModelRailwaySimulation;
|
import edu.kit.informatik.ModelRailwaySimulation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command used to run the simulation.
|
* Command used to advance the simulation.
|
||||||
*
|
*
|
||||||
* @author Arne Keller
|
* @author Arne Keller
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
@ -13,13 +13,12 @@ public class Step implements Command {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new 'step' command.
|
* Construct a new 'step' command.
|
||||||
* @param speed the amount of steps to perform
|
* @param speed the amount of steps to perform, can be any value
|
||||||
*/
|
*/
|
||||||
public Step(final short speed) {
|
public Step(final short speed) {
|
||||||
this.speed = speed;
|
this.speed = speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void apply(final ModelRailwaySimulation simulation) {
|
public void apply(final ModelRailwaySimulation simulation) {
|
||||||
simulation.step(speed);
|
simulation.step(speed);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user