summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/jrummikub/model/Hand.java4
-rw-r--r--src/jrummikub/model/StoneTray.java72
2 files changed, 41 insertions, 35 deletions
diff --git a/src/jrummikub/model/Hand.java b/src/jrummikub/model/Hand.java
index 73ffab5..644ae94 100644
--- a/src/jrummikub/model/Hand.java
+++ b/src/jrummikub/model/Hand.java
@@ -3,4 +3,8 @@ package jrummikub.model;
/** Class managing a {@link Player}'s {@link Stone}s */
public class Hand extends StoneTray<Stone> implements IHand {
+ @Override
+ protected Position fixInvalidDrop(Stone stone, Position pos) {
+ return null;
+ }
}
diff --git a/src/jrummikub/model/StoneTray.java b/src/jrummikub/model/StoneTray.java
index 94b09be..52079d0 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> {
protected HashMap<E, Pair<E, Position>> objects = new HashMap<E, Pair<E, Position>>();
@@ -22,11 +22,6 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
LEFT, RIGHT, TOP, BOTTOM;
}
- /*
- * (non-Javadoc)
- *
- * @see jrummikub.model.IStoneTray#pickUp(jrummikub.model.Position)
- */
@Override
public E pickUp(Position position) {
for (Map.Entry<E, Pair<E, Position>> i : objects.entrySet()) {
@@ -39,12 +34,10 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
if (position.getY() < currentPosition.getY()) {
continue;
}
- if (position.getX() > currentPosition.getX()
- + currentObject.getWidth()) {
+ if (position.getX() > currentPosition.getX() + currentObject.getWidth()) {
continue;
}
- if (position.getY() > currentPosition.getY()
- + currentObject.getHeight()) {
+ if (position.getY() > currentPosition.getY() + currentObject.getHeight()) {
continue;
}
// Position is inside the current object
@@ -54,11 +47,6 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
return null;
}
- /*
- * (non-Javadoc)
- *
- * @see jrummikub.model.IStoneTray#drop(E, jrummikub.model.Position)
- */
@Override
public void drop(E object, Position position) {
if (object != null) {
@@ -68,32 +56,35 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
@SuppressWarnings("unchecked")
private void drop(E object, Position position, Direction direction) {
+ objects.put(object, new Pair<E, Position>(object, position));
for (Pair<E, Position> i : ((Map<E, Pair<E, Position>>) objects.clone())
.values()) {
- Position currentPosition = i.getSecond();
+ Direction newDirection = direction;
E currentObject = i.getFirst();
- if (!objectsOverlap(object, position, currentObject,
- currentPosition)) {
+ if (currentObject == object)
+ continue;
+ Position currentPosition = getPosition(currentObject);
+ if (!objectsOverlap(object, position, currentObject, currentPosition)) {
continue;
}
// Object would be placed inside the current object
- if (direction == null) {
- direction = getMoveDirection(object, position, i);
+ if (newDirection == null) {
+ newDirection = getMoveDirection(object, position, i);
}
Position newPosition = null;
// Move object to avoid overlap
- switch (direction) {
+ switch (newDirection) {
case TOP:
- newPosition = new Position(currentPosition.getX(),
- position.getY() - currentObject.getHeight());
+ newPosition = new Position(currentPosition.getX(), position.getY()
+ - currentObject.getHeight());
break;
case BOTTOM:
- newPosition = new Position(currentPosition.getX(),
- position.getY() + object.getHeight());
+ newPosition = new Position(currentPosition.getX(), position.getY()
+ + object.getHeight());
break;
case LEFT:
- newPosition = new Position(position.getX()
- - currentObject.getWidth(), currentPosition.getY());
+ newPosition = new Position(position.getX() - currentObject.getWidth(),
+ currentPosition.getY());
break;
case RIGHT:
newPosition = new Position(position.getX() + object.getWidth(),
@@ -101,10 +92,23 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
break;
}
- objects.remove(i.getFirst());
+ objects.remove(currentObject);
drop(currentObject, newPosition, direction);
}
- objects.put(object, new Pair<E, Position>(object, position));
+ }
+
+ /**
+ * Checks whether the object may be placed on the given position, computes new
+ * position if not
+ *
+ * @param object
+ * to be dropped
+ * @param p
+ * the object is dropped at
+ * @return null if the drop is valid, new position otherwise
+ */
+ protected Position fixInvalidDrop(E object, Position pos) {
+ return null;
}
/** Tests whether two objects overlap **/
@@ -156,15 +160,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
float blockingRight = blocking.getSecond().getX()
+ blocking.getFirst().getWidth();
float overlapRight = Math.min(objectRight, blockingRight);
- float overlapLeft = Math.max(position.getX(), blocking.getSecond()
- .getX());
+ float overlapLeft = Math.max(position.getX(), blocking.getSecond().getX());
float overlapX = overlapRight - overlapLeft;
float objectBottom = position.getY() + object.getHeight();
float blockingBottom = blocking.getSecond().getY()
+ blocking.getFirst().getHeight();
float overlapBottom = Math.min(objectBottom, blockingBottom);
- float overlapTop = Math
- .max(position.getY(), blocking.getSecond().getY());
+ float overlapTop = Math.max(position.getY(), blocking.getSecond().getY());
float overlapY = overlapBottom - overlapTop;
// vertical or horizontal Shift
// TODO magic factor
@@ -207,11 +209,11 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
try {
StoneTray<E> copy = (StoneTray<E>) super.clone();
copy.objects = (HashMap<E, Pair<E, Position>>) objects.clone();
-
+
return copy;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
-
+
return null;
}
}