2011-04-30 15:45:11 +02:00
|
|
|
package jrummikub.model;
|
|
|
|
|
2011-05-03 17:13:10 +02:00
|
|
|
import java.util.HashMap;
|
2011-04-30 21:17:14 +02:00
|
|
|
import java.util.Iterator;
|
2011-05-03 17:13:10 +02:00
|
|
|
import java.util.Map;
|
2011-04-30 21:17:14 +02:00
|
|
|
|
|
|
|
import jrummikub.util.Pair;
|
|
|
|
|
2011-04-30 23:13:22 +02:00
|
|
|
/**
|
|
|
|
* A StoneTray is a collection of positioned objects (for example {@link Stone}s
|
|
|
|
* or {@link StoneSet}s.
|
2011-05-02 04:46:00 +02:00
|
|
|
*
|
|
|
|
* @param <E>
|
2011-05-04 19:09:39 +02:00
|
|
|
* Type of positioned objects (must implement Sizeable)
|
2011-04-30 23:13:22 +02:00
|
|
|
*/
|
2011-05-04 16:23:10 +02:00
|
|
|
public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
2011-05-03 17:13:10 +02:00
|
|
|
protected HashMap<E, Position> objects = new HashMap<E, Position>();
|
2011-04-30 23:13:22 +02:00
|
|
|
|
2011-05-02 00:31:30 +02:00
|
|
|
/** Possible move directions in case of overlapping Stones/Sets */
|
|
|
|
|
|
|
|
private static enum Direction {
|
|
|
|
LEFT, RIGHT, TOP, BOTTOM;
|
|
|
|
}
|
|
|
|
|
2011-05-04 16:23:10 +02:00
|
|
|
/*
|
|
|
|
* (non-Javadoc)
|
2011-04-30 15:45:11 +02:00
|
|
|
*
|
2011-05-04 16:23:10 +02:00
|
|
|
* @see jrummikub.model.IStoneTray#pickUp(jrummikub.model.Position)
|
2011-04-30 15:45:11 +02:00
|
|
|
*/
|
2011-05-04 16:23:10 +02:00
|
|
|
@Override
|
2011-04-30 15:45:11 +02:00
|
|
|
public E pickUp(Position position) {
|
2011-05-03 17:13:10 +02:00
|
|
|
for (Map.Entry<E, Position> i : objects.entrySet()) {
|
|
|
|
Position currentPosition = i.getValue();
|
|
|
|
E currentObject = i.getKey();
|
2011-04-30 23:13:22 +02:00
|
|
|
// Tests if position is left of, above ... the current object
|
|
|
|
if (position.getX() < currentPosition.getX()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (position.getY() < currentPosition.getY()) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-05-04 19:09:39 +02:00
|
|
|
if (position.getX() > currentPosition.getX()
|
|
|
|
+ currentObject.getWidth()) {
|
2011-04-30 23:13:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
2011-05-04 19:09:39 +02:00
|
|
|
if (position.getY() > currentPosition.getY()
|
|
|
|
+ currentObject.getHeight()) {
|
2011-04-30 23:13:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Position is inside the current object
|
2011-05-03 17:13:10 +02:00
|
|
|
objects.remove(i.getKey());
|
2011-04-30 23:13:22 +02:00
|
|
|
return currentObject;
|
|
|
|
}
|
|
|
|
return null;
|
2011-04-30 15:45:11 +02:00
|
|
|
}
|
|
|
|
|
2011-05-04 16:23:10 +02:00
|
|
|
/*
|
|
|
|
* (non-Javadoc)
|
2011-04-30 15:45:11 +02:00
|
|
|
*
|
2011-05-04 16:23:10 +02:00
|
|
|
* @see jrummikub.model.IStoneTray#drop(E, jrummikub.model.Position)
|
2011-04-30 15:45:11 +02:00
|
|
|
*/
|
2011-05-04 16:23:10 +02:00
|
|
|
@Override
|
2011-04-30 15:45:11 +02:00
|
|
|
public void drop(E object, Position position) {
|
2011-05-03 19:06:15 +02:00
|
|
|
if (object != null) {
|
|
|
|
drop(object, position, null);
|
|
|
|
}
|
2011-04-30 15:45:11 +02:00
|
|
|
}
|
2011-04-30 21:17:14 +02:00
|
|
|
|
2011-05-02 23:33:46 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2011-04-30 23:13:22 +02:00
|
|
|
private void drop(E object, Position position, Direction direction) {
|
2011-05-04 16:23:10 +02:00
|
|
|
for (Map.Entry<E, Position> i : ((Map<E, Position>) objects.clone())
|
|
|
|
.entrySet()) {
|
2011-05-03 17:13:10 +02:00
|
|
|
Position currentPosition = i.getValue();
|
|
|
|
E currentObject = i.getKey();
|
2011-05-04 19:09:39 +02:00
|
|
|
if (!objectsOverlap(object, position, currentObject,
|
|
|
|
currentPosition)) {
|
2011-04-30 23:13:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Object would be placed inside the current object
|
|
|
|
if (direction == null) {
|
|
|
|
direction = getMoveDirection(object, position, i);
|
|
|
|
}
|
|
|
|
Position newPosition = null;
|
2011-05-03 17:13:09 +02:00
|
|
|
// Move object to avoid overlap
|
2011-04-30 23:13:22 +02:00
|
|
|
switch (direction) {
|
|
|
|
case TOP:
|
2011-05-04 19:09:39 +02:00
|
|
|
newPosition = new Position(currentPosition.getX(),
|
|
|
|
position.getY() - currentObject.getHeight());
|
2011-04-30 23:13:22 +02:00
|
|
|
break;
|
|
|
|
case BOTTOM:
|
2011-05-04 19:09:39 +02:00
|
|
|
newPosition = new Position(currentPosition.getX(),
|
|
|
|
position.getY() + object.getHeight());
|
2011-04-30 23:13:22 +02:00
|
|
|
break;
|
|
|
|
case LEFT:
|
2011-05-04 19:09:39 +02:00
|
|
|
newPosition = new Position(position.getX()
|
|
|
|
- currentObject.getWidth(), currentPosition.getY());
|
2011-04-30 23:13:22 +02:00
|
|
|
break;
|
|
|
|
case RIGHT:
|
|
|
|
newPosition = new Position(position.getX() + object.getWidth(),
|
|
|
|
currentPosition.getY());
|
|
|
|
break;
|
|
|
|
}
|
2011-05-04 16:23:10 +02:00
|
|
|
|
2011-05-03 17:13:10 +02:00
|
|
|
objects.remove(i.getKey());
|
2011-04-30 23:13:22 +02:00
|
|
|
drop(currentObject, newPosition, direction);
|
|
|
|
}
|
2011-05-03 17:13:10 +02:00
|
|
|
objects.put(object, position);
|
2011-04-30 23:13:22 +02:00
|
|
|
}
|
2011-04-30 21:17:14 +02:00
|
|
|
|
2011-05-02 02:00:50 +02:00
|
|
|
/** Tests whether two objects overlap **/
|
|
|
|
private boolean objectsOverlap(E object1, Position position1, E object2,
|
|
|
|
Position position2) {
|
|
|
|
// Tests if position is left of, above ... the current object
|
|
|
|
if (position1.getX() + object1.getWidth() <= position2.getX()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (position1.getY() + object1.getHeight() <= position2.getY()) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-05-02 04:46:00 +02:00
|
|
|
if (position1.getX() >= position2.getX() + object2.getWidth()) {
|
2011-05-02 02:00:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-05-02 04:46:00 +02:00
|
|
|
if (position1.getY() >= position2.getY() + object2.getHeight()) {
|
2011-05-02 02:00:50 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-30 23:13:22 +02:00
|
|
|
private Direction getMoveDirection(E object, Position position,
|
2011-05-03 17:13:10 +02:00
|
|
|
Map.Entry<E, Position> blocking) {
|
2011-04-30 23:13:22 +02:00
|
|
|
boolean isVertical = getMoveOrientationn(object, position, blocking);
|
|
|
|
float objectMidpointX = position.getX() + object.getWidth() / 2;
|
|
|
|
float objectMidpointY = position.getY() + object.getHeight() / 2;
|
2011-05-03 17:13:10 +02:00
|
|
|
float blockingMidpointX = blocking.getValue().getX()
|
|
|
|
+ blocking.getKey().getWidth() / 2;
|
|
|
|
float blockingMidpointY = blocking.getValue().getY()
|
|
|
|
+ blocking.getKey().getHeight() / 2;
|
2011-04-30 23:13:22 +02:00
|
|
|
if (isVertical) {
|
|
|
|
if (objectMidpointY < blockingMidpointY) {
|
|
|
|
return Direction.BOTTOM;
|
|
|
|
} else {
|
|
|
|
return Direction.TOP;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (objectMidpointX < blockingMidpointX) {
|
|
|
|
return Direction.RIGHT;
|
|
|
|
} else {
|
|
|
|
return Direction.LEFT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-30 21:17:14 +02:00
|
|
|
|
2011-04-30 23:13:22 +02:00
|
|
|
private boolean getMoveOrientationn(E object, Position position,
|
2011-05-03 17:13:10 +02:00
|
|
|
Map.Entry<E, Position> blocking) {
|
2011-04-30 23:13:22 +02:00
|
|
|
float objectRight = position.getX() + object.getWidth();
|
2011-05-03 17:13:10 +02:00
|
|
|
float blockingRight = blocking.getValue().getX()
|
|
|
|
+ blocking.getKey().getWidth();
|
2011-04-30 23:13:22 +02:00
|
|
|
float overlapRight = Math.min(objectRight, blockingRight);
|
2011-05-04 19:09:39 +02:00
|
|
|
float overlapLeft = Math.max(position.getX(), blocking.getValue()
|
|
|
|
.getX());
|
2011-04-30 23:13:22 +02:00
|
|
|
float overlapX = overlapRight - overlapLeft;
|
|
|
|
float objectBottom = position.getY() + object.getHeight();
|
2011-05-03 17:13:10 +02:00
|
|
|
float blockingBottom = blocking.getValue().getY()
|
|
|
|
+ blocking.getKey().getHeight();
|
2011-04-30 23:13:22 +02:00
|
|
|
float overlapBottom = Math.min(objectBottom, blockingBottom);
|
2011-05-04 19:09:39 +02:00
|
|
|
float overlapTop = Math
|
|
|
|
.max(position.getY(), blocking.getValue().getY());
|
2011-04-30 23:13:22 +02:00
|
|
|
float overlapY = overlapBottom - overlapTop;
|
|
|
|
// vertical or horizontal Shift
|
|
|
|
// TODO magic factor
|
|
|
|
return overlapX > overlapY;
|
2011-04-30 21:17:14 +02:00
|
|
|
}
|
|
|
|
|
2011-05-04 16:23:10 +02:00
|
|
|
/*
|
|
|
|
* (non-Javadoc)
|
2011-05-02 04:46:00 +02:00
|
|
|
*
|
2011-05-04 16:23:10 +02:00
|
|
|
* @see jrummikub.model.IStoneTray#getPosition(E)
|
2011-05-02 04:46:00 +02:00
|
|
|
*/
|
2011-05-04 16:23:10 +02:00
|
|
|
@Override
|
2011-04-30 23:13:22 +02:00
|
|
|
public Position getPosition(E object) {
|
2011-05-03 17:13:10 +02:00
|
|
|
return objects.get(object);
|
2011-04-30 23:13:22 +02:00
|
|
|
}
|
2011-04-30 21:17:14 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<Pair<E, Position>> iterator() {
|
2011-05-04 19:09:39 +02:00
|
|
|
final Iterator<Map.Entry<E, Position>> entryIterator = objects
|
|
|
|
.entrySet().iterator();
|
2011-05-03 17:13:10 +02:00
|
|
|
return new Iterator<Pair<E, Position>>() {
|
|
|
|
Iterator<Map.Entry<E, Position>> iterator = entryIterator;
|
2011-05-04 16:23:10 +02:00
|
|
|
|
2011-05-03 17:13:10 +02:00
|
|
|
@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();
|
|
|
|
}
|
2011-05-04 16:23:10 +02:00
|
|
|
|
2011-05-03 17:13:10 +02:00
|
|
|
};
|
2011-04-30 21:17:14 +02:00
|
|
|
}
|
2011-04-30 23:13:22 +02:00
|
|
|
|
2011-05-04 16:23:10 +02:00
|
|
|
/*
|
|
|
|
* (non-Javadoc)
|
|
|
|
*
|
|
|
|
* @see jrummikub.model.IStoneTray#pickUp(E)
|
|
|
|
*/
|
|
|
|
@Override
|
2011-05-03 17:13:11 +02:00
|
|
|
public void pickUp(E object) {
|
|
|
|
objects.remove(object);
|
|
|
|
}
|
2011-05-04 16:23:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* (non-Javadoc)
|
|
|
|
*
|
|
|
|
* @see jrummikub.model.IStoneTray#clone()
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
2011-05-03 19:06:12 +02:00
|
|
|
@Override
|
2011-05-04 16:23:10 +02:00
|
|
|
public IStoneTray<E> clone() {
|
2011-05-04 19:39:17 +02:00
|
|
|
try {
|
|
|
|
StoneTray<E> copy = (StoneTray<E>) super.clone();
|
|
|
|
copy.objects = (HashMap<E, Position>) objects.clone();
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
} catch (CloneNotSupportedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2011-05-03 19:06:12 +02:00
|
|
|
}
|
2011-05-03 17:13:11 +02:00
|
|
|
|
2011-05-04 19:09:39 +02:00
|
|
|
@Override
|
|
|
|
public int getSize() {
|
|
|
|
return objects.size();
|
|
|
|
}
|
|
|
|
|
2011-04-30 15:45:11 +02:00
|
|
|
}
|