From 42a8b91c4da3f98312ce444cca157b7d9b8e729f Mon Sep 17 00:00:00 2001 From: Jannis Harder Date: Tue, 3 May 2011 17:13:10 +0200 Subject: StoneTray refactored, uses Map now git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@77 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/model/StoneTray.java | 76 +++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 30 deletions(-) (limited to 'src/jrummikub/model') 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 implements Iterable> { - protected ArrayList> objects = new ArrayList>(); + protected HashMap objects = new HashMap(); /** Possible move directions in case of overlapping Stones/Sets */ @@ -31,9 +33,9 @@ public class StoneTray implements * @return the picked up stone */ public E pickUp(Position position) { - for (Pair i : objects) { - Position currentPosition = i.getSecond(); - E currentObject = i.getFirst(); + for (Map.Entry 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 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 implements @SuppressWarnings("unchecked") private void drop(E object, Position position, Direction direction) { - for (Pair i : (List>)objects.clone()) { - Position currentPosition = i.getSecond(); - E currentObject = i.getFirst(); + for (Map.Entry i : ((Map)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 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 implements break; } - objects.remove(i); + objects.remove(i.getKey()); drop(currentObject, newPosition, direction); } - objects.add(new Pair(object, position)); + objects.put(object, position); } /** Tests whether two objects overlap **/ @@ -128,14 +130,14 @@ public class StoneTray implements } private Direction getMoveDirection(E object, Position position, - Pair blocking) { + Map.Entry 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 implements } private boolean getMoveOrientationn(E object, Position position, - Pair blocking) { + Map.Entry 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 implements * @return position of the object or null when the object is not on the tray */ public Position getPosition(E object) { - for (Pair i : objects) { - if (object.equals(i.getFirst())) { - return i.getSecond(); - } - } - return null; + return objects.get(object); } @Override public Iterator> iterator() { - return objects.iterator(); + final Iterator> entryIterator = objects.entrySet().iterator(); + return new Iterator>() { + Iterator> iterator = entryIterator; + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public Pair next() { + Map.Entry entry = iterator.next(); + return new Pair(entry.getKey(), entry.getValue()); + } + + @Override + public void remove() { + iterator.remove(); + } + + }; } } -- cgit v1.2.3