From b86571cf832ff13010798f3607eea6ad0ef039d0 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 9 May 2011 00:33:34 +0200 Subject: Allow laying out stone sets git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@180 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/model/StoneTray.java | 73 ++++++++++++++------------------------ 1 file changed, 26 insertions(+), 47 deletions(-) (limited to 'src/jrummikub/model/StoneTray.java') diff --git a/src/jrummikub/model/StoneTray.java b/src/jrummikub/model/StoneTray.java index 14e9026..94b09be 100644 --- a/src/jrummikub/model/StoneTray.java +++ b/src/jrummikub/model/StoneTray.java @@ -14,7 +14,7 @@ import jrummikub.util.Pair; * Type of positioned objects (must implement Sizeable) */ public class StoneTray implements IStoneTray { - protected HashMap objects = new HashMap(); + protected HashMap> objects = new HashMap>(); /** Possible move directions in case of overlapping Stones/Sets */ @@ -29,8 +29,8 @@ public class StoneTray implements IStoneTray { */ @Override public E pickUp(Position position) { - for (Map.Entry i : objects.entrySet()) { - Position currentPosition = i.getValue(); + for (Map.Entry> i : objects.entrySet()) { + Position currentPosition = i.getValue().getSecond(); E currentObject = i.getKey(); // Tests if position is left of, above ... the current object if (position.getX() < currentPosition.getX()) { @@ -68,10 +68,10 @@ public class StoneTray implements IStoneTray { @SuppressWarnings("unchecked") private void drop(E object, Position position, Direction direction) { - for (Map.Entry i : ((Map) objects.clone()) - .entrySet()) { - Position currentPosition = i.getValue(); - E currentObject = i.getKey(); + for (Pair i : ((Map>) objects.clone()) + .values()) { + Position currentPosition = i.getSecond(); + E currentObject = i.getFirst(); if (!objectsOverlap(object, position, currentObject, currentPosition)) { continue; @@ -101,10 +101,10 @@ public class StoneTray implements IStoneTray { break; } - objects.remove(i.getKey()); + objects.remove(i.getFirst()); drop(currentObject, newPosition, direction); } - objects.put(object, position); + objects.put(object, new Pair(object, position)); } /** Tests whether two objects overlap **/ @@ -127,14 +127,14 @@ public class StoneTray implements IStoneTray { } private Direction getMoveDirection(E object, Position position, - Map.Entry blocking) { - boolean isVertical = getMoveOrientationn(object, position, blocking); + Pair blocking) { + boolean isVertical = getMoveOrientation(object, position, blocking); float objectMidpointX = position.getX() + object.getWidth() / 2; float objectMidpointY = position.getY() + object.getHeight() / 2; - float blockingMidpointX = blocking.getValue().getX() - + blocking.getKey().getWidth() / 2; - float blockingMidpointY = blocking.getValue().getY() - + blocking.getKey().getHeight() / 2; + float blockingMidpointX = blocking.getSecond().getX() + + blocking.getFirst().getWidth() / 2; + float blockingMidpointY = blocking.getSecond().getY() + + blocking.getFirst().getHeight() / 2; if (isVertical) { if (objectMidpointY < blockingMidpointY) { return Direction.BOTTOM; @@ -150,21 +150,21 @@ public class StoneTray implements IStoneTray { } } - private boolean getMoveOrientationn(E object, Position position, - Map.Entry blocking) { + private boolean getMoveOrientation(E object, Position position, + Pair blocking) { float objectRight = position.getX() + object.getWidth(); - float blockingRight = blocking.getValue().getX() - + blocking.getKey().getWidth(); + float blockingRight = blocking.getSecond().getX() + + blocking.getFirst().getWidth(); float overlapRight = Math.min(objectRight, blockingRight); - float overlapLeft = Math.max(position.getX(), blocking.getValue() + float overlapLeft = Math.max(position.getX(), blocking.getSecond() .getX()); float overlapX = overlapRight - overlapLeft; float objectBottom = position.getY() + object.getHeight(); - float blockingBottom = blocking.getValue().getY() - + blocking.getKey().getHeight(); + float blockingBottom = blocking.getSecond().getY() + + blocking.getFirst().getHeight(); float overlapBottom = Math.min(objectBottom, blockingBottom); float overlapTop = Math - .max(position.getY(), blocking.getValue().getY()); + .max(position.getY(), blocking.getSecond().getY()); float overlapY = overlapBottom - overlapTop; // vertical or horizontal Shift // TODO magic factor @@ -178,33 +178,12 @@ public class StoneTray implements IStoneTray { */ @Override public Position getPosition(E object) { - return objects.get(object); + return objects.get(object).getSecond(); } @Override public Iterator> 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(); - } - - }; + return objects.values().iterator(); } /* @@ -227,7 +206,7 @@ public class StoneTray implements IStoneTray { public IStoneTray clone() { try { StoneTray copy = (StoneTray) super.clone(); - copy.objects = (HashMap) objects.clone(); + copy.objects = (HashMap>) objects.clone(); return copy; } catch (CloneNotSupportedException e) { -- cgit v1.2.3