diff options
Diffstat (limited to 'src/jrummikub/model')
-rw-r--r-- | src/jrummikub/model/StoneTray.java | 76 |
1 files changed, 46 insertions, 30 deletions
diff --git a/src/jrummikub/model/StoneTray.java b/src/jrummikub/model/StoneTray.java index 5bb9566..945df13 100644 --- a/src/jrummikub/model/StoneTray.java +++ b/src/jrummikub/model/StoneTray.java @@ -1,8 +1,10 @@ package jrummikub.model; import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import jrummikub.util.Pair; @@ -15,7 +17,7 @@ import jrummikub.util.Pair; */ public class StoneTray<E extends Sizeable> implements Iterable<Pair<E, Position>> { - protected ArrayList<Pair<E, Position>> objects = new ArrayList<Pair<E, Position>>(); + protected HashMap<E, Position> objects = new HashMap<E, Position>(); /** Possible move directions in case of overlapping Stones/Sets */ @@ -31,9 +33,9 @@ public class StoneTray<E extends Sizeable> implements * @return the picked up stone */ public E pickUp(Position position) { - for (Pair<E, Position> i : objects) { - Position currentPosition = i.getSecond(); - E currentObject = i.getFirst(); + for (Map.Entry<E, Position> i : objects.entrySet()) { + Position currentPosition = i.getValue(); + E currentObject = i.getKey(); // Tests if position is left of, above ... the current object if (position.getX() < currentPosition.getX()) { continue; @@ -50,7 +52,7 @@ public class StoneTray<E extends Sizeable> implements continue; } // Position is inside the current object - objects.remove(i); + objects.remove(i.getKey()); return currentObject; } return null; @@ -70,9 +72,9 @@ public class StoneTray<E extends Sizeable> implements @SuppressWarnings("unchecked") private void drop(E object, Position position, Direction direction) { - for (Pair<E, Position> i : (List<Pair<E, Position>>)objects.clone()) { - Position currentPosition = i.getSecond(); - E currentObject = i.getFirst(); + 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)) { continue; @@ -85,7 +87,7 @@ public class StoneTray<E extends Sizeable> implements // Move object to avoid overlap switch (direction) { case TOP: - newPosition = new Position(i.getSecond().getX(), + newPosition = new Position(currentPosition.getX(), position.getY() - currentObject.getHeight()); break; case BOTTOM: @@ -102,10 +104,10 @@ public class StoneTray<E extends Sizeable> implements break; } - objects.remove(i); + objects.remove(i.getKey()); drop(currentObject, newPosition, direction); } - objects.add(new Pair<E, Position>(object, position)); + objects.put(object, position); } /** Tests whether two objects overlap **/ @@ -128,14 +130,14 @@ public class StoneTray<E extends Sizeable> implements } private Direction getMoveDirection(E object, Position position, - Pair<E, Position> blocking) { + Map.Entry<E, Position> blocking) { boolean isVertical = getMoveOrientationn(object, position, blocking); float objectMidpointX = position.getX() + object.getWidth() / 2; float objectMidpointY = position.getY() + object.getHeight() / 2; - float blockingMidpointX = blocking.getSecond().getX() - + blocking.getFirst().getWidth() / 2; - float blockingMidpointY = blocking.getSecond().getY() - + blocking.getFirst().getHeight() / 2; + float blockingMidpointX = blocking.getValue().getX() + + blocking.getKey().getWidth() / 2; + float blockingMidpointY = blocking.getValue().getY() + + blocking.getKey().getHeight() / 2; if (isVertical) { if (objectMidpointY < blockingMidpointY) { return Direction.BOTTOM; @@ -152,19 +154,19 @@ public class StoneTray<E extends Sizeable> implements } private boolean getMoveOrientationn(E object, Position position, - Pair<E, Position> blocking) { + Map.Entry<E, Position> blocking) { float objectRight = position.getX() + object.getWidth(); - float blockingRight = blocking.getSecond().getX() - + blocking.getFirst().getWidth(); + float blockingRight = blocking.getValue().getX() + + blocking.getKey().getWidth(); float overlapRight = Math.min(objectRight, blockingRight); - float overlapLeft = Math.max(position.getX(), blocking.getSecond() + float overlapLeft = Math.max(position.getX(), blocking.getValue() .getX()); float overlapX = overlapRight - overlapLeft; float objectBottom = position.getY() + object.getHeight(); - float blockingBottom = blocking.getSecond().getY() - + blocking.getFirst().getHeight(); + float blockingBottom = blocking.getValue().getY() + + blocking.getKey().getHeight(); float overlapBottom = Math.min(objectBottom, blockingBottom); - float overlapTop = Math.max(position.getY(), blocking.getSecond() + float overlapTop = Math.max(position.getY(), blocking.getValue() .getY()); float overlapY = overlapBottom - overlapTop; // vertical or horizontal Shift @@ -180,17 +182,31 @@ public class StoneTray<E extends Sizeable> implements * @return position of the object or null when the object is not on the tray */ public Position getPosition(E object) { - for (Pair<E, Position> i : objects) { - if (object.equals(i.getFirst())) { - return i.getSecond(); - } - } - return null; + return objects.get(object); } @Override public Iterator<Pair<E, Position>> iterator() { - return objects.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(); + } + + @Override + public Pair<E, Position> next() { + Map.Entry<E, Position> entry = iterator.next(); + return new Pair<E, Position>(entry.getKey(), entry.getValue()); + } + + @Override + public void remove() { + iterator.remove(); + } + + }; } } |