summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/model/StoneTray.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/model/StoneTray.java')
-rw-r--r--src/jrummikub/model/StoneTray.java114
1 files changed, 79 insertions, 35 deletions
diff --git a/src/jrummikub/model/StoneTray.java b/src/jrummikub/model/StoneTray.java
index a5f023d..f2f1151 100644
--- a/src/jrummikub/model/StoneTray.java
+++ b/src/jrummikub/model/StoneTray.java
@@ -31,6 +31,17 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
}
}
+ /**
+ * Subroutine to "drop" to consider and determine the direction the objects
+ * dropped one collides with position-wise evade in
+ *
+ * @param object
+ * the object to add to Hand
+ * @param position
+ * {@link Position} to put the object
+ * @param direction
+ * the direction the other stones evade in
+ */
private void drop(E object, Position position, Direction direction) {
Pair<Position, Direction> update = fixInvalidDrop(object, position,
direction);
@@ -42,6 +53,16 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
dropUnchecked(object, position, direction);
}
+ /**
+ * Subroutine to "drop" to execute the actual drop
+ *
+ * @param object
+ * the object to add to Hand
+ * @param position
+ * {@link Position} to put the object
+ * @param direction
+ * the direction the other stones evade in
+ */
@SuppressWarnings("unchecked")
private void dropUnchecked(E object, Position position, Direction direction) {
objects.put(object, new Pair<E, Position>(object, position));
@@ -62,23 +83,22 @@ 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;
+ 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;
}
objects.remove(currentObject);
@@ -102,6 +122,16 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
return null;
}
+ /**
+ * Static method for determining a less or equal relation considering a small
+ * fuzziness
+ *
+ * @param d
+ * the value to be less or equal
+ * @param e
+ * than the other one
+ * @return if d is less or equal e
+ */
private static boolean lessOrEqual(double d, double e) {
if (-0.000001 < e && e < 0.000001) {
return (d < e + 0.000001);
@@ -115,7 +145,20 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
return d < e;
}
- /** Tests whether two objects overlap **/
+ /**
+ * Tests whether two objects overlap
+ *
+ * @param object1
+ * first object
+ * @param position1
+ * first object's position
+ * @param object2
+ * second object
+ * @param position2
+ * second object's position
+ *
+ * @return whether they overlap
+ **/
private boolean objectsOverlap(E object1, Position position1, E object2,
Position position2) {
// Tests if position is left of, above ... the current object
@@ -134,6 +177,17 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
return true;
}
+ /**
+ * Returns the direction to move the object in
+ *
+ * @param object
+ * the object
+ * @param position
+ * the object's position
+ * @param blocking
+ * the object thats blocking
+ * @return the direction
+ */
private Direction getMoveDirection(E object, Position position,
Pair<E, Position> blocking) {
boolean isVertical = getMoveOrientation(object, position, blocking);
@@ -161,6 +215,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
/**
* Will the object be moved horizontally or vertically
*
+ * @param object
+ * the object
+ * @param position
+ * the objects position
+ * @param blocking
+ * the object thats blocking
+ *
* @return boolean vertical movement
*/
private boolean getMoveOrientation(E object, Position position,
@@ -177,16 +238,9 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
double overlapBottom = Math.min(objectBottom, blockingBottom);
double overlapTop = Math.max(position.getY(), blocking.getSecond().getY());
double overlapY = overlapBottom - overlapTop;
- // vertical or horizontal Shift
- // TODO magic factor
return overlapX > overlapY;
}
- /*
- * (non-Javadoc)
- *
- * @see jrummikub.model.IStoneTray#getPosition(E)
- */
@Override
public Position getPosition(E object) {
Pair<E, Position> entry = objects.get(object);
@@ -206,21 +260,11 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
return objects.values().iterator();
}
- /*
- * (non-Javadoc)
- *
- * @see jrummikub.model.IStoneTray#pickUp(E)
- */
@Override
public boolean pickUp(E object) {
return null != objects.remove(object);
}
- /*
- * (non-Javadoc)
- *
- * @see jrummikub.model.IStoneTray#clone()
- */
@SuppressWarnings("unchecked")
@Override
public IStoneTray<E> clone() {