summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/model/StoneTray.java
diff options
context:
space:
mode:
authorJannis Harder <harder@informatik.uni-luebeck.de>2011-05-03 17:13:10 +0200
committerJannis Harder <harder@informatik.uni-luebeck.de>2011-05-03 17:13:10 +0200
commit42a8b91c4da3f98312ce444cca157b7d9b8e729f (patch)
tree7f6950584632c8510dc3343ac69537b5ceaf56db /src/jrummikub/model/StoneTray.java
parent1236d16fc658cf2a85de3c780e13bd35f7c65547 (diff)
downloadJRummikub-42a8b91c4da3f98312ce444cca157b7d9b8e729f.tar
JRummikub-42a8b91c4da3f98312ce444cca157b7d9b8e729f.zip
StoneTray refactored, uses Map now
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@77 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/model/StoneTray.java')
-rw-r--r--src/jrummikub/model/StoneTray.java76
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();
+ }
+
+ };
}
}