StoneTray refactored, uses Map now
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@77 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
1236d16fc6
commit
42a8b91c4d
1 changed files with 46 additions and 30 deletions
|
@ -1,8 +1,10 @@
|
||||||
package jrummikub.model;
|
package jrummikub.model;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import jrummikub.util.Pair;
|
import jrummikub.util.Pair;
|
||||||
|
|
||||||
|
@ -15,7 +17,7 @@ import jrummikub.util.Pair;
|
||||||
*/
|
*/
|
||||||
public class StoneTray<E extends Sizeable> implements
|
public class StoneTray<E extends Sizeable> implements
|
||||||
Iterable<Pair<E, Position>> {
|
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 */
|
/** 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
|
* @return the picked up stone
|
||||||
*/
|
*/
|
||||||
public E pickUp(Position position) {
|
public E pickUp(Position position) {
|
||||||
for (Pair<E, Position> i : objects) {
|
for (Map.Entry<E, Position> i : objects.entrySet()) {
|
||||||
Position currentPosition = i.getSecond();
|
Position currentPosition = i.getValue();
|
||||||
E currentObject = i.getFirst();
|
E currentObject = i.getKey();
|
||||||
// Tests if position is left of, above ... the current object
|
// Tests if position is left of, above ... the current object
|
||||||
if (position.getX() < currentPosition.getX()) {
|
if (position.getX() < currentPosition.getX()) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -50,7 +52,7 @@ public class StoneTray<E extends Sizeable> implements
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Position is inside the current object
|
// Position is inside the current object
|
||||||
objects.remove(i);
|
objects.remove(i.getKey());
|
||||||
return currentObject;
|
return currentObject;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -70,9 +72,9 @@ public class StoneTray<E extends Sizeable> implements
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private void drop(E object, Position position, Direction direction) {
|
private void drop(E object, Position position, Direction direction) {
|
||||||
for (Pair<E, Position> i : (List<Pair<E, Position>>)objects.clone()) {
|
for (Map.Entry<E, Position> i : ((Map<E,Position>)objects.clone()).entrySet()) {
|
||||||
Position currentPosition = i.getSecond();
|
Position currentPosition = i.getValue();
|
||||||
E currentObject = i.getFirst();
|
E currentObject = i.getKey();
|
||||||
if (!objectsOverlap(object, position, currentObject,
|
if (!objectsOverlap(object, position, currentObject,
|
||||||
currentPosition)) {
|
currentPosition)) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -85,7 +87,7 @@ public class StoneTray<E extends Sizeable> implements
|
||||||
// Move object to avoid overlap
|
// Move object to avoid overlap
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case TOP:
|
case TOP:
|
||||||
newPosition = new Position(i.getSecond().getX(),
|
newPosition = new Position(currentPosition.getX(),
|
||||||
position.getY() - currentObject.getHeight());
|
position.getY() - currentObject.getHeight());
|
||||||
break;
|
break;
|
||||||
case BOTTOM:
|
case BOTTOM:
|
||||||
|
@ -102,10 +104,10 @@ public class StoneTray<E extends Sizeable> implements
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
objects.remove(i);
|
objects.remove(i.getKey());
|
||||||
drop(currentObject, newPosition, direction);
|
drop(currentObject, newPosition, direction);
|
||||||
}
|
}
|
||||||
objects.add(new Pair<E, Position>(object, position));
|
objects.put(object, position);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Tests whether two objects overlap **/
|
/** Tests whether two objects overlap **/
|
||||||
|
@ -128,14 +130,14 @@ public class StoneTray<E extends Sizeable> implements
|
||||||
}
|
}
|
||||||
|
|
||||||
private Direction getMoveDirection(E object, Position position,
|
private Direction getMoveDirection(E object, Position position,
|
||||||
Pair<E, Position> blocking) {
|
Map.Entry<E, Position> blocking) {
|
||||||
boolean isVertical = getMoveOrientationn(object, position, blocking);
|
boolean isVertical = getMoveOrientationn(object, position, blocking);
|
||||||
float objectMidpointX = position.getX() + object.getWidth() / 2;
|
float objectMidpointX = position.getX() + object.getWidth() / 2;
|
||||||
float objectMidpointY = position.getY() + object.getHeight() / 2;
|
float objectMidpointY = position.getY() + object.getHeight() / 2;
|
||||||
float blockingMidpointX = blocking.getSecond().getX()
|
float blockingMidpointX = blocking.getValue().getX()
|
||||||
+ blocking.getFirst().getWidth() / 2;
|
+ blocking.getKey().getWidth() / 2;
|
||||||
float blockingMidpointY = blocking.getSecond().getY()
|
float blockingMidpointY = blocking.getValue().getY()
|
||||||
+ blocking.getFirst().getHeight() / 2;
|
+ blocking.getKey().getHeight() / 2;
|
||||||
if (isVertical) {
|
if (isVertical) {
|
||||||
if (objectMidpointY < blockingMidpointY) {
|
if (objectMidpointY < blockingMidpointY) {
|
||||||
return Direction.BOTTOM;
|
return Direction.BOTTOM;
|
||||||
|
@ -152,19 +154,19 @@ public class StoneTray<E extends Sizeable> implements
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean getMoveOrientationn(E object, Position position,
|
private boolean getMoveOrientationn(E object, Position position,
|
||||||
Pair<E, Position> blocking) {
|
Map.Entry<E, Position> blocking) {
|
||||||
float objectRight = position.getX() + object.getWidth();
|
float objectRight = position.getX() + object.getWidth();
|
||||||
float blockingRight = blocking.getSecond().getX()
|
float blockingRight = blocking.getValue().getX()
|
||||||
+ blocking.getFirst().getWidth();
|
+ blocking.getKey().getWidth();
|
||||||
float overlapRight = Math.min(objectRight, blockingRight);
|
float overlapRight = Math.min(objectRight, blockingRight);
|
||||||
float overlapLeft = Math.max(position.getX(), blocking.getSecond()
|
float overlapLeft = Math.max(position.getX(), blocking.getValue()
|
||||||
.getX());
|
.getX());
|
||||||
float overlapX = overlapRight - overlapLeft;
|
float overlapX = overlapRight - overlapLeft;
|
||||||
float objectBottom = position.getY() + object.getHeight();
|
float objectBottom = position.getY() + object.getHeight();
|
||||||
float blockingBottom = blocking.getSecond().getY()
|
float blockingBottom = blocking.getValue().getY()
|
||||||
+ blocking.getFirst().getHeight();
|
+ blocking.getKey().getHeight();
|
||||||
float overlapBottom = Math.min(objectBottom, blockingBottom);
|
float overlapBottom = Math.min(objectBottom, blockingBottom);
|
||||||
float overlapTop = Math.max(position.getY(), blocking.getSecond()
|
float overlapTop = Math.max(position.getY(), blocking.getValue()
|
||||||
.getY());
|
.getY());
|
||||||
float overlapY = overlapBottom - overlapTop;
|
float overlapY = overlapBottom - overlapTop;
|
||||||
// vertical or horizontal Shift
|
// 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
|
* @return position of the object or null when the object is not on the tray
|
||||||
*/
|
*/
|
||||||
public Position getPosition(E object) {
|
public Position getPosition(E object) {
|
||||||
for (Pair<E, Position> i : objects) {
|
return objects.get(object);
|
||||||
if (object.equals(i.getFirst())) {
|
|
||||||
return i.getSecond();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<Pair<E, Position>> iterator() {
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue