diff options
Diffstat (limited to 'src/jrummikub/model/StoneTray.java')
-rw-r--r-- | src/jrummikub/model/StoneTray.java | 90 |
1 files changed, 47 insertions, 43 deletions
diff --git a/src/jrummikub/model/StoneTray.java b/src/jrummikub/model/StoneTray.java index 21412a9..080786b 100644 --- a/src/jrummikub/model/StoneTray.java +++ b/src/jrummikub/model/StoneTray.java @@ -11,10 +11,9 @@ import jrummikub.util.Pair; * or {@link StoneSet}s. * * @param <E> - * Type of positioned objects (must implement Sizeable) + * Type of positioned objects (must implement Sizeable) */ -public class StoneTray<E extends Sizeable> implements - Iterable<Pair<E, Position>> { +public class StoneTray<E extends Sizeable> implements IStoneTray<E> { protected HashMap<E, Position> objects = new HashMap<E, Position>(); /** Possible move directions in case of overlapping Stones/Sets */ @@ -23,13 +22,12 @@ public class StoneTray<E extends Sizeable> implements LEFT, RIGHT, TOP, BOTTOM; } - /** - * Removes object from tray and returns it + /* + * (non-Javadoc) * - * @param position - * position of the object that will be removed - * @return the picked up stone + * @see jrummikub.model.IStoneTray#pickUp(jrummikub.model.Position) */ + @Override public E pickUp(Position position) { for (Map.Entry<E, Position> i : objects.entrySet()) { Position currentPosition = i.getValue(); @@ -41,12 +39,10 @@ public class StoneTray<E extends Sizeable> implements if (position.getY() < currentPosition.getY()) { continue; } - if (position.getX() > currentPosition.getX() - + currentObject.getWidth()) { + if (position.getX() > currentPosition.getX() + currentObject.getWidth()) { continue; } - if (position.getY() > currentPosition.getY() - + currentObject.getHeight()) { + if (position.getY() > currentPosition.getY() + currentObject.getHeight()) { continue; } // Position is inside the current object @@ -56,14 +52,12 @@ public class StoneTray<E extends Sizeable> implements return null; } - /** - * Adds object to the tray + /* + * (non-Javadoc) * - * @param object - * object to add to Hand - * @param position - * {@link Position} to put the object + * @see jrummikub.model.IStoneTray#drop(E, jrummikub.model.Position) */ + @Override public void drop(E object, Position position) { if (object != null) { drop(object, position, null); @@ -72,11 +66,11 @@ public class StoneTray<E extends Sizeable> implements @SuppressWarnings("unchecked") private void drop(E object, Position position, Direction direction) { - for (Map.Entry<E, Position> i : ((Map<E,Position>)objects.clone()).entrySet()) { + for (Map.Entry<E, Position> i : ((Map<E, Position>) objects.clone()) + .entrySet()) { Position currentPosition = i.getValue(); E currentObject = i.getKey(); - if (!objectsOverlap(object, position, currentObject, - currentPosition)) { + if (!objectsOverlap(object, position, currentObject, currentPosition)) { continue; } // Object would be placed inside the current object @@ -87,23 +81,23 @@ public class StoneTray<E extends Sizeable> implements // Move object to avoid overlap switch (direction) { case TOP: - newPosition = new Position(currentPosition.getX(), - position.getY() - currentObject.getHeight()); + newPosition = new Position(currentPosition.getX(), position.getY() + - currentObject.getHeight()); break; case BOTTOM: - newPosition = new Position(currentPosition.getX(), - position.getY() + object.getHeight()); + newPosition = new Position(currentPosition.getX(), position.getY() + + object.getHeight()); break; case LEFT: - newPosition = new Position(position.getX() - - currentObject.getWidth(), currentPosition.getY()); + newPosition = new Position(position.getX() - currentObject.getWidth(), + currentPosition.getY()); break; case RIGHT: newPosition = new Position(position.getX() + object.getWidth(), currentPosition.getY()); break; } - + objects.remove(i.getKey()); drop(currentObject, newPosition, direction); } @@ -159,37 +153,36 @@ public class StoneTray<E extends Sizeable> implements float blockingRight = blocking.getValue().getX() + blocking.getKey().getWidth(); float overlapRight = Math.min(objectRight, blockingRight); - float overlapLeft = Math.max(position.getX(), blocking.getValue() - .getX()); + float overlapLeft = Math.max(position.getX(), blocking.getValue().getX()); float overlapX = overlapRight - overlapLeft; float objectBottom = position.getY() + object.getHeight(); float blockingBottom = blocking.getValue().getY() + blocking.getKey().getHeight(); float overlapBottom = Math.min(objectBottom, blockingBottom); - float overlapTop = Math.max(position.getY(), blocking.getValue() - .getY()); + float overlapTop = Math.max(position.getY(), blocking.getValue().getY()); float overlapY = overlapBottom - overlapTop; // vertical or horizontal Shift // TODO magic factor return overlapX > overlapY; } - /** - * Returns the position of an object that is already on the tray + /* + * (non-Javadoc) * - * @param object - * object whose position is requested - * @return position of the object or null when the object is not on the tray + * @see jrummikub.model.IStoneTray#getPosition(E) */ + @Override public Position getPosition(E object) { return objects.get(object); } @Override public Iterator<Pair<E, Position>> iterator() { - final Iterator<Map.Entry<E, Position>> entryIterator = objects.entrySet().iterator(); + final Iterator<Map.Entry<E, Position>> entryIterator = objects.entrySet() + .iterator(); return new Iterator<Pair<E, Position>>() { Iterator<Map.Entry<E, Position>> iterator = entryIterator; + @Override public boolean hasNext() { return iterator.hasNext(); @@ -205,18 +198,29 @@ public class StoneTray<E extends Sizeable> implements public void remove() { iterator.remove(); } - + }; } + /* + * (non-Javadoc) + * + * @see jrummikub.model.IStoneTray#pickUp(E) + */ + @Override public void pickUp(E object) { objects.remove(object); } - - + + /* + * (non-Javadoc) + * + * @see jrummikub.model.IStoneTray#clone() + */ + @SuppressWarnings("unchecked") @Override - public StoneTray<E> clone() { - StoneTray<E> copy = new StoneTray(); + public IStoneTray<E> clone() { + StoneTray<E> copy = new StoneTray<E>(); copy.objects = (HashMap<E, Position>) objects.clone(); return copy; } |