EOS 2  1.1.0
Einfache Objektbasierte Sprache
Robot.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.robot;
2 
3 import de.lathanda.eos.base.MutableColor;
4 import de.lathanda.eos.base.util.Direction;
5 import de.lathanda.eos.robot.exceptions.CubeImmutableException;
6 import de.lathanda.eos.robot.exceptions.CubeMissingException;
7 import de.lathanda.eos.robot.exceptions.RobotException;
8 import de.lathanda.eos.robot.exceptions.RobotMovementFailedException;
9 import de.lathanda.eos.robot.exceptions.RobotVoidException;
10 import de.lathanda.eos.robot.gui.Configuration;
11 import de.lathanda.eos.robot.gui.Configuration.ConfigurationListener;
12 
19 public class Robot implements ConfigurationListener {
23  public static final int SIZE = 3;
27  private int jump = Configuration.def.getJumpheight();
31  private int fall = Configuration.def.getFallheight();
35  private World world;
39  private MutableColor robotColor = MutableColor.BLUE;
43  private Direction direction = Direction.EAST;
47  private int x = 0;
51  private int y = 0;
55  private int z = 0;
59  private MutableColor stoneColor = MutableColor.RED;
63  public Robot() {
65  world = null;
66  }
75  protected void initialize(World world, int x, int y, int z, Direction d) {
76  this.x = x;
77  this.y = y;
78  this.z = z;
79  this.direction = d;
80  this.world = world;
81  }
82  @Override
83  public void robotConfigurationChanged(int fallheight, int jumpheight) {
84  jump = jumpheight;
85  fall = fallheight;
86  }
87 
92  private Column frontColumn() throws RobotVoidException {
93  return getColumn(direction);
94  }
100  private Column getColumn(Direction d) throws RobotVoidException {
101  if (world != null) {
102  return world.getColumn(new Coordinate(x + d.dx, y + d.dy));
103  } else {
104  throw new RobotVoidException();
105  }
106  }
111  private Column getColumn(int x, int y) throws RobotVoidException {
112  if (world != null) {
113  return world.getColumn(new Coordinate(x, y));
114  } else {
115  throw new RobotVoidException();
116  }
117  }
122  private Column getColumn() throws RobotVoidException {
123  if (world != null) {
124  return world.getColumn(new Coordinate(x, y));
125  } else {
126  throw new RobotVoidException();
127  }
128  }
135  private void setPosition(int x, int y, int z) {
136  this.x = x;
137  this.y = y;
138  this.z = z;
139  }
140 
144  public final void setMark() throws RobotException {
145  getColumn().setMark(true);
146  }
147 
151  public final void setMark(MutableColor c) throws RobotException {
152  getColumn().setMark(c);
153  }
154 
159  public final void removeMark() throws RobotException {
160  getColumn().setMark(false);
161  }
166  public final void dropStone() throws RobotException {
167  frontColumn().dropCube(z, Cube.createStone(stoneColor));
168  }
174  public final void dropStone(MutableColor c) throws RobotException {
175  frontColumn().dropCube(z, Cube.createStone(c));
176  }
181  public final MutableColor stoneColor() throws RobotException {
182  return frontColumn().stoneColor(z);
183  }
189  public final void pickup() throws RobotException {
190  frontColumn().pickup(z);
191  }
199  public final void pickup(int n) throws RobotException {
200  for (int i = n; i --> 0; ) {
201  pickup();
202  }
203  }
209  public final boolean isMarked() throws RobotException {
210  return getColumn().isMarked();
211  }
217  public final boolean isMarked(MutableColor c) throws RobotException {
218  return getColumn().isMarked(c);
219  }
223  public final void turnLeft() {
224  direction = direction.getLeft();
225  }
229  public final void turnRight() {
230  direction = direction.getRight();
231  }
235  public final void turnAround() {
236  direction = direction.getLeft().getLeft();
237  }
245  private void stepDirection(Direction d) throws RobotException {
246  Column columnTo = getColumn(d);
247  int newZ = columnTo.isReachable(z, SIZE, jump, fall);
248  if (newZ >= 0) {
249  setPosition(x + d.dx, y + d.dy, newZ);
250  } else {
251  throw new RobotMovementFailedException();
252  }
253  }
258  public void step() throws RobotException {
259  stepDirection(direction);
260  }
267  public void step(int count) throws RobotException {
268  for(int i = count; i --> 0; ) {
269  step();
270  }
271  }
277  public void stepBack() throws RobotException {
278  stepDirection(direction.getBack());
279  }
284  public void stepLeft() throws RobotException {
285  stepDirection(direction.getLeft());
286  }
292  public void stepRight() throws RobotException {
293  stepDirection(direction.getRight());
294  }
300  private boolean isObstacleDirection(Direction direction) throws RobotException {
301  return getColumn(direction).isReachable(z, SIZE, jump, fall) < 0;
302  }
308  public boolean isObstacle() throws RobotException {
309  return isObstacleDirection(direction);
310  }
316  public boolean isLeftObstacle() throws RobotException {
317  return isObstacleDirection(direction.getLeft());
318  }
324  public boolean isRightObstacle() throws RobotException {
325  return isObstacleDirection(direction.getRight());
326  }
332  public boolean isBackObstacle() throws RobotException {
333  return isObstacleDirection(direction.getBack());
334  }
342  private void flyDirection(int dx, int dy, int dz) throws RobotException {
343  Column column= world.getColumn(new Coordinate(x + dx, y + dx));
344  if (column.isFree(z+dz, SIZE)) {
345  setPosition(x + dx, y + dy, z+dz);
346  } else {
347  throw new RobotMovementFailedException();
348  }
349  }
355  public void flyDown() throws RobotException {
356  flyDirection(0,0,-1);
357  }
363  public void flyUp() throws RobotException {
364  flyDirection(0,0,1);
365  }
371  public void fly() throws RobotException {
372  flyDirection(direction.dx, direction.dy, 0);
373  }
379  public void flyLeft() throws RobotException {
380  flyDirection(direction.getLeft().dx, direction.getLeft().dy, 0);
381  }
387  public void flyRight() throws RobotException {
388  flyDirection(direction.getRight().dx, direction.getRight().dy, 0);
389  }
395  public void flyBack() throws RobotException {
396  flyDirection(direction.getBack().dx, direction.getBack().dy, 0);
397  }
405  private boolean isFreeDirection(int dx, int dy, int dz) throws RobotException {
406  return getColumn(x+dx, y+dy).isFree(z+dz, SIZE);
407  }
412  public boolean isFree() throws RobotException {
413  return isFreeDirection(direction.dx, direction.dy, 0);
414  }
419  public boolean isLeftFree() throws RobotException {
420  return isFreeDirection(direction.getLeft().dx, direction.getLeft().dy, 0);
421  }
426  public boolean isRightFree() throws RobotException {
427  return isFreeDirection(direction.getRight().dx, direction.getRight().dy, 0);
428  }
433  public boolean isBackFree() throws RobotException {
434  return isFreeDirection(direction.getBack().dx, direction.getBack().dy, 0);
435  }
440  public boolean isTopFree() throws RobotException {
441  return isFreeDirection(0,0,1);
442  }
447  public boolean isBottomFree() throws RobotException {
448  return isFreeDirection(0,0,-1);
449  }
456  public void placeStone(int n) throws RobotException {
457  frontColumn().setCube(z+n, Cube.createStone(stoneColor));
458  }
466  public void placeStone(int n, MutableColor c) throws RobotException {
467  frontColumn().setCube(z+n, Cube.createStone(c));
468  }
474  public void removeStone(int n) throws RobotException {
475  frontColumn().removeCube(z+ n);
476  }
481  public void setStoneColor(MutableColor c) {
482  stoneColor = c;
483  }
485  return stoneColor;
486  }
492  public boolean hasStone() throws RobotException {
493  return frontColumn().hasCube();
494  }
501  public boolean hasStone(int n) throws RobotException {
502  return frontColumn().hasCube(n);
503  }
508  public boolean isFacingSouth() {
509  return direction == Direction.SOUTH;
510  }
515  public boolean isFacingWest() {
516  return direction == Direction.WEST;
517  }
522  public boolean isFacingNorth() {
523  return direction == Direction.NORTH;
524  }
529  public boolean isFacingEast() {
530  return direction == Direction.EAST;
531  }
532 
534  return robotColor;
535  }
536  public void setRobotColor(MutableColor robotColor) {
537  this.robotColor = robotColor;
538  }
540  return direction;
541  }
542  public int getX() {
543  return x;
544  }
545  public int getY() {
546  return y;
547  }
548  public int getZ() {
549  return z;
550  }
551 }
static final MutableColor BLUE
static final MutableColor RED
boolean isFree(int level, int size)
Definition: Column.java:150
void setMark(boolean mark)
Definition: Column.java:76
void dropCube(int level, Cube cube)
Definition: Column.java:30
void setCube(int level, Cube cube)
Definition: Column.java:256
void removeCube(int level)
Definition: Column.java:226
MutableColor stoneColor(int level)
Definition: Column.java:59
int isReachable(int level, int size, int climb, int fall)
Definition: Column.java:93
static Cube createStone(MutableColor stoneColor)
Definition: Cube.java:47
final void setMark(MutableColor c)
Definition: Robot.java:151
final void turnRight()
Definition: Robot.java:229
final boolean isMarked()
Definition: Robot.java:209
final boolean isMarked(MutableColor c)
Definition: Robot.java:217
void step(int count)
Definition: Robot.java:267
void setStoneColor(MutableColor c)
Definition: Robot.java:481
static final int SIZE
Definition: Robot.java:23
final void dropStone()
Definition: Robot.java:166
final MutableColor stoneColor()
Definition: Robot.java:181
boolean hasStone(int n)
Definition: Robot.java:501
final void removeMark()
Definition: Robot.java:159
void robotConfigurationChanged(int fallheight, int jumpheight)
Definition: Robot.java:83
final void pickup(int n)
Definition: Robot.java:199
final void dropStone(MutableColor c)
Definition: Robot.java:174
Direction getDirection()
Definition: Robot.java:539
final void turnAround()
Definition: Robot.java:235
void placeStone(int n, MutableColor c)
Definition: Robot.java:466
void initialize(World world, int x, int y, int z, Direction d)
Definition: Robot.java:75
void setRobotColor(MutableColor robotColor)
Definition: Robot.java:536
MutableColor getRobotColor()
Definition: Robot.java:533
MutableColor getStoneColor()
Definition: Robot.java:484
Column getColumn(int x, int y)
Definition: World.java:462
synchronized void addConfigurationListener(ConfigurationListener cf)
Impressum