summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/model
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/model')
-rw-r--r--src/jrummikub/model/StoneTray.java109
1 files changed, 61 insertions, 48 deletions
diff --git a/src/jrummikub/model/StoneTray.java b/src/jrummikub/model/StoneTray.java
index f2f1151..8681c75 100644
--- a/src/jrummikub/model/StoneTray.java
+++ b/src/jrummikub/model/StoneTray.java
@@ -11,7 +11,7 @@ import jrummikub.util.Pair;
* or {@link StoneSet}s.
*
* @param <E>
- * Type of positioned objects (must implement Sizeable)
+ * Type of positioned objects (must implement Sizeable)
*/
public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
private static final long serialVersionUID = -6329309928640027222L;
@@ -36,11 +36,11 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
* dropped one collides with position-wise evade in
*
* @param object
- * the object to add to Hand
+ * the object to add to Hand
* @param position
- * {@link Position} to put the object
+ * {@link Position} to put the object
* @param direction
- * the direction the other stones evade in
+ * the direction the other stones evade in
*/
private void drop(E object, Position position, Direction direction) {
Pair<Position, Direction> update = fixInvalidDrop(object, position,
@@ -57,11 +57,11 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
* Subroutine to "drop" to execute the actual drop
*
* @param object
- * the object to add to Hand
+ * the object to add to Hand
* @param position
- * {@link Position} to put the object
+ * {@link Position} to put the object
* @param direction
- * the direction the other stones evade in
+ * the direction the other stones evade in
*/
@SuppressWarnings("unchecked")
private void dropUnchecked(E object, Position position, Direction direction) {
@@ -73,7 +73,8 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
if (currentObject == object)
continue;
Position currentPosition = getPosition(currentObject);
- if (!objectsOverlap(object, position, currentObject, currentPosition)) {
+ if (!objectsOverlap(object, position, currentObject,
+ currentPosition)) {
continue;
}
// Object would be placed inside the current object
@@ -82,39 +83,47 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
}
Position newPosition = null;
// Move object to avoid overlap
- switch (newDirection) {
- case TOP:
- newPosition = new Position(currentPosition.getX(), position.getY()
- - currentObject.getHeight());
- break;
- case BOTTOM:
- newPosition = new Position(currentPosition.getX(), position.getY()
- + object.getHeight());
- break;
- case LEFT:
- newPosition = new Position(position.getX() - currentObject.getWidth(),
- currentPosition.getY());
- break;
- case RIGHT:
- newPosition = new Position(position.getX() + object.getWidth(),
- currentPosition.getY());
- break;
- }
+ newPosition = getNewPosition(object, position, newDirection,
+ currentObject, currentPosition);
objects.remove(currentObject);
drop(currentObject, newPosition, newDirection);
}
}
+ private Position getNewPosition(E object, Position position,
+ Direction newDirection, E currentObject, Position currentPosition) {
+ Position newPosition = new Position(0, 0);
+ switch (newDirection) {
+ case TOP:
+ newPosition = new Position(currentPosition.getX(), position.getY()
+ - currentObject.getHeight());
+ break;
+ case BOTTOM:
+ newPosition = new Position(currentPosition.getX(), position.getY()
+ + object.getHeight());
+ break;
+ case LEFT:
+ newPosition = new Position(position.getX()
+ - currentObject.getWidth(), currentPosition.getY());
+ break;
+ case RIGHT:
+ newPosition = new Position(position.getX() + object.getWidth(),
+ currentPosition.getY());
+ break;
+ }
+ return newPosition;
+ }
+
/**
- * Checks whether the object may be placed on the given position, computes new
- * position if not
+ * Checks whether the object may be placed on the given position, computes
+ * new position if not
*
* @param object
- * to be dropped
+ * to be dropped
* @param dir
* @param pos
- * the object is dropped at
+ * the object is dropped at
* @return null if the drop is valid, new position otherwise
*/
protected Pair<Position, Direction> fixInvalidDrop(E object, Position pos,
@@ -123,13 +132,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
}
/**
- * Static method for determining a less or equal relation considering a small
- * fuzziness
+ * Static method for determining a less or equal relation considering a
+ * small fuzziness
*
* @param d
- * the value to be less or equal
+ * the value to be less or equal
* @param e
- * than the other one
+ * than the other one
* @return if d is less or equal e
*/
private static boolean lessOrEqual(double d, double e) {
@@ -149,13 +158,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
* Tests whether two objects overlap
*
* @param object1
- * first object
+ * first object
* @param position1
- * first object's position
+ * first object's position
* @param object2
- * second object
+ * second object
* @param position2
- * second object's position
+ * second object's position
*
* @return whether they overlap
**/
@@ -165,13 +174,15 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
if (lessOrEqual(position1.getX() + object1.getWidth(), position2.getX())) {
return false;
}
- if (lessOrEqual(position1.getY() + object1.getHeight(), position2.getY())) {
+ if (lessOrEqual(position1.getY() + object1.getHeight(),
+ position2.getY())) {
return false;
}
if (lessOrEqual(position2.getX() + object2.getWidth(), position1.getX())) {
return false;
}
- if (lessOrEqual(position2.getY() + object2.getHeight(), position1.getY())) {
+ if (lessOrEqual(position2.getY() + object2.getHeight(),
+ position1.getY())) {
return false;
}
return true;
@@ -181,11 +192,11 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
* Returns the direction to move the object in
*
* @param object
- * the object
+ * the object
* @param position
- * the object's position
+ * the object's position
* @param blocking
- * the object thats blocking
+ * the object thats blocking
* @return the direction
*/
private Direction getMoveDirection(E object, Position position,
@@ -216,11 +227,11 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
* Will the object be moved horizontally or vertically
*
* @param object
- * the object
+ * the object
* @param position
- * the objects position
+ * the objects position
* @param blocking
- * the object thats blocking
+ * the object thats blocking
*
* @return boolean vertical movement
*/
@@ -230,13 +241,15 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
double blockingRight = blocking.getSecond().getX()
+ blocking.getFirst().getWidth();
double overlapRight = Math.min(objectRight, blockingRight);
- double overlapLeft = Math.max(position.getX(), blocking.getSecond().getX());
+ double overlapLeft = Math.max(position.getX(), blocking.getSecond()
+ .getX());
double overlapX = overlapRight - overlapLeft;
double objectBottom = position.getY() + object.getHeight();
double blockingBottom = blocking.getSecond().getY()
+ blocking.getFirst().getHeight();
double overlapBottom = Math.min(objectBottom, blockingBottom);
- double overlapTop = Math.max(position.getY(), blocking.getSecond().getY());
+ double overlapTop = Math.max(position.getY(), blocking.getSecond()
+ .getY());
double overlapY = overlapBottom - overlapTop;
return overlapX > overlapY;
}