EOS 2  1.1.0
Einfache Objektbasierte Sprache
Column.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.robot;
2 
3 import java.util.ArrayList;
4 
5 import de.lathanda.eos.base.MutableColor;
6 import de.lathanda.eos.robot.exceptions.CubeImmutableException;
7 import de.lathanda.eos.robot.exceptions.CubeMissingException;
8 import de.lathanda.eos.robot.exceptions.CubeNoSpaceException;
9 import de.lathanda.eos.robot.exceptions.RobotException;
10 
17 public class Column {
18  private CubeArray cubes = new CubeArray();
19  private MutableColor mark = null;
20  private static final MutableColor MARK = MutableColor.YELLOW;
21 
30  public void dropCube(int level, Cube cube) throws RobotException {
31  if(cubes.get(level).isFree()) {
32  int n = level;
33  while (cubes.get(n-1).isFree()) { n--; }
34  cubes.set(n, cube);
35  } else {
36  int n = level+1;
37  while (!cubes.get(n).isFree()) {
38  n++;
39  }
40  cubes.set(n, cube);
41  }
42  }
49  public void dropCube(Cube cube) throws RobotException {
50  //drop from above
51  dropCube(cubes.size(), cube);
52  }
59  public MutableColor stoneColor(int level) throws RobotException {
60  if(cubes.get(level).isFree()) {
61  int n = level;
62  while (cubes.get(n-1).isFree()) { n--; }
63  return cubes.get(n-1).getColor();
64  } else {
65  int n = level+1;
66  while (!cubes.get(n).isFree()) {
67  n++;
68  }
69  return cubes.get(n-1).getColor();
70  }
71  }
76  public void setMark(boolean mark) throws RobotException {
77  this.mark = (mark)?MARK:null;
78  }
79 
93  public int isReachable(int level, int size, int climb, int fall) throws RobotException {
94  int maxDifference = Math.min(fall, climb);
95  int difference = 0;
96  while (difference <= maxDifference) {
97  if (isWalkable(level - difference, size)) {
98  return level - difference;
99  }
100  if (isWalkable(level + difference, size)) {
101  return level + difference;
102  }
103  difference++;
104  }
105  if (fall < climb) {
106  while (difference <= climb) {
107  if (isWalkable(level + difference, size)) {
108  return level + difference;
109  }
110  difference++;
111  }
112  } else {
113  while (difference <= fall) {
114  if (isWalkable(level - difference, size)) {
115  return level - difference;
116  }
117  difference++;
118  }
119  }
120  return -1;
121  }
130  private boolean isWalkable(int level, int size) throws RobotException {
131  if (cubes.get(level - 1).isFree()) {
132  return false; //check solid ground
133  }
134  for (int i = size; i-- > 0;) {
135  if (!cubes.get(level + i).isFree()) {
136  return false; //check free space
137  }
138  }
139  return true;
140  }
150  public boolean isFree(int level, int size) throws RobotException {
151  for (int i = size; i-- > 0;) {
152  if (!cubes.get(level + i).isFree()) {
153  return false; //check free space
154  }
155  }
156  return true;
157  }
158 
162  public void pickup() throws RobotException {
163  pickup(cubes.size());
164  }
171  void pickup(int level) throws RobotException {
172  if (cubes.get(level).isFree()) {
173  for(int n = level; n --> 0;) {
174  if(!cubes.get(n).isEmpty()) {
175  removeCube(n);
176  }
177  }
178  } else {
179  int n = level;
180  while (!cubes.get(n+1).isFree()) {n++;}
181  removeCube(n);
182  }
183  }
188  public boolean isMarked() throws RobotException {
189  return mark != null;
190  }
195  public boolean isMarked(MutableColor c) throws RobotException {
196  return (mark == null)?false:mark.equals(c);
197  }
199  return mark;
200  }
201  public void setMark() throws RobotException {
202  mark = MARK;
203  }
204  public void setMark(MutableColor c) throws RobotException {
205  mark = c;
206  }
207  public void toggleMark() throws RobotException {
208  if (mark == null) {
209  mark = MARK;
210  } else {
211  mark = null;
212  }
213  }
214  public void toggleMark(MutableColor c) throws RobotException {
215  if (mark == null) {
216  mark = c;
217  } else {
218  mark = null;
219  }
220  }
226  public void removeCube(int level) throws RobotException {
227  cubes.get(level).setEmpty();
228  }
233  public boolean hasCube() throws RobotException {
234  for (int i = 0; i < cubes.size(); i++) {
235  if (!cubes.get(i).isFree()) {
236  return true;
237  }
238  }
239  return false;
240  }
246  public boolean hasCube(int n) throws RobotException {
247  int numberOfCubes = 0;
248  for (int i = 0; i < cubes.size(); i++) {
249  if (!cubes.get(i).isFree()) {
250  numberOfCubes++;
251  }
252  }
253  return numberOfCubes == n;
254  }
255 
256  public void setCube(int level, Cube cube) throws RobotException {
257  cubes.set(level, cube);
258  }
259  public Cube[] getCubes() throws RobotException {
260  return cubes.getAll();
261  }
266  public void remove(int level) throws RobotException {
267  cubes.set(level, Cube.createEmpty());
268  }
269  private static class CubeArray {
270  private ArrayList<Cube> cubes = new ArrayList<>();
279  private synchronized Cube get(int level) throws RobotException {
280  if (level < 0) {
281  return Cube.createGround();
282  }
283  if (level < cubes.size()) {
284  return cubes.get(level);
285  } else {
286  return Cube.createEmpty();
287  }
288  }
294  public synchronized void set(int level, Cube cube) throws RobotException {
295  //fill up
296  for (int n = level - cubes.size() + 1; n --> 0;) {
297  cubes.add(Cube.createEmpty());
298  }
299  //set cube
300  cubes.set(level, cube);
301  //clean up
302  int n = cubes.size() - 1;
303  while ( n >= 0 && cubes.get(n).isEmpty()) {
304  cubes.remove(n);
305  n--;
306  }
307  }
308 
314  public synchronized Cube[] getAll() throws RobotException {
315  Cube[] cubeArray = new Cube[cubes.size()];
316  return cubes.toArray(cubeArray);
317  }
318  public int size() {
319  return cubes.size();
320  }
321  }
322 }
static final MutableColor YELLOW
void dropCube(Cube cube)
Definition: Column.java:49
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
boolean hasCube(int n)
Definition: Column.java:246
MutableColor getMark()
Definition: Column.java:198
void setMark(MutableColor c)
Definition: Column.java:204
boolean isMarked(MutableColor c)
Definition: Column.java:195
MutableColor stoneColor(int level)
Definition: Column.java:59
void toggleMark(MutableColor c)
Definition: Column.java:214
int isReachable(int level, int size, int climb, int fall)
Definition: Column.java:93
static Cube createEmpty()
Definition: Cube.java:59
static Cube createGround()
Definition: Cube.java:65
Impressum