1 package de.lathanda.eos.robot;
3 import java.util.ArrayList;
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;
18 private CubeArray cubes =
new CubeArray();
31 if(cubes.get(level).isFree()) {
33 while (cubes.get(n-1).isFree()) { n--; }
37 while (!cubes.get(n).isFree()) {
60 if(cubes.get(level).isFree()) {
62 while (cubes.get(n-1).isFree()) { n--; }
63 return cubes.get(n-1).getColor();
66 while (!cubes.get(n).isFree()) {
69 return cubes.get(n-1).getColor();
77 this.mark = (mark)?MARK:
null;
94 int maxDifference = Math.min(fall, climb);
96 while (difference <= maxDifference) {
97 if (isWalkable(level - difference, size)) {
98 return level - difference;
100 if (isWalkable(level + difference, size)) {
101 return level + difference;
106 while (difference <= climb) {
107 if (isWalkable(level + difference, size)) {
108 return level + difference;
113 while (difference <= fall) {
114 if (isWalkable(level - difference, size)) {
115 return level - difference;
130 private boolean isWalkable(
int level,
int size)
throws RobotException {
131 if (cubes.get(level - 1).isFree()) {
134 for (
int i = size; i-- > 0;) {
135 if (!cubes.get(level + i).isFree()) {
151 for (
int i = size; i-- > 0;) {
152 if (!cubes.get(level + i).isFree()) {
172 if (cubes.get(level).isFree()) {
173 for(
int n = level; n --> 0;) {
174 if(!cubes.get(n).isEmpty()) {
180 while (!cubes.get(n+1).isFree()) {n++;}
196 return (mark ==
null)?
false:mark.equals(c);
227 cubes.get(level).setEmpty();
234 for (
int i = 0; i < cubes.size(); i++) {
235 if (!cubes.get(i).isFree()) {
247 int numberOfCubes = 0;
248 for (
int i = 0; i < cubes.size(); i++) {
249 if (!cubes.get(i).isFree()) {
253 return numberOfCubes == n;
257 cubes.set(level, cube);
260 return cubes.getAll();
269 private static class CubeArray {
270 private ArrayList<Cube> cubes =
new ArrayList<>();
283 if (level < cubes.size()) {
284 return cubes.get(level);
294 public synchronized void set(
int level, Cube cube)
throws RobotException {
296 for (
int n = level - cubes.size() + 1; n --> 0;) {
297 cubes.add(Cube.createEmpty());
300 cubes.set(level, cube);
302 int n = cubes.size() - 1;
303 while ( n >= 0 && cubes.get(n).isEmpty()) {
314 public synchronized Cube[] getAll() throws RobotException {
315 Cube[] cubeArray =
new Cube[cubes.size()];
316 return cubes.toArray(cubeArray);
static final MutableColor YELLOW
boolean isFree(int level, int size)
void setMark(boolean mark)
void dropCube(int level, Cube cube)
void setCube(int level, Cube cube)
void removeCube(int level)
void setMark(MutableColor c)
boolean isMarked(MutableColor c)
MutableColor stoneColor(int level)
void toggleMark(MutableColor c)
int isReachable(int level, int size, int climb, int fall)
static Cube createEmpty()
static Cube createGround()