diff options
-rw-r--r-- | src/jrummikub/model/Hand.java | 27 | ||||
-rw-r--r-- | src/jrummikub/model/StoneTray.java | 27 | ||||
-rw-r--r-- | test/jrummikub/model/HandTest.java | 4 |
3 files changed, 52 insertions, 6 deletions
diff --git a/src/jrummikub/model/Hand.java b/src/jrummikub/model/Hand.java index 644ae94..8864a17 100644 --- a/src/jrummikub/model/Hand.java +++ b/src/jrummikub/model/Hand.java @@ -1,10 +1,33 @@ package jrummikub.model; +import jrummikub.util.Pair; + +import static jrummikub.model.StoneTray.Direction.*; + /** 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; + protected Pair<Position, Direction> fixInvalidDrop(Stone stone, Position pos, + Direction dir) { + float x = pos.getX(); + float y = pos.getY(); + + if (x >= 0 && x <= 13) { + return null; + } + if (x < 0) { + if (y == 0) { + return new Pair<Position, Direction>(new Position(0, 0), RIGHT); + } else { + return new Pair<Position, Direction>(new Position(13, 0), LEFT); + } + } else { + if (y == 0) { + return new Pair<Position, Direction>(new Position(0, 1), RIGHT); + } else { + return new Pair<Position, Direction>(new Position(13, 1), LEFT); + } + } } } diff --git a/src/jrummikub/model/StoneTray.java b/src/jrummikub/model/StoneTray.java index 52079d0..8b83371 100644 --- a/src/jrummikub/model/StoneTray.java +++ b/src/jrummikub/model/StoneTray.java @@ -18,8 +18,22 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> { /** Possible move directions in case of overlapping Stones/Sets */ - private static enum Direction { + protected static enum Direction { LEFT, RIGHT, TOP, BOTTOM; + + public Direction reverse() { + switch (this) { + case LEFT: + return RIGHT; + case RIGHT: + return LEFT; + case TOP: + return BOTTOM; + case BOTTOM: + return TOP; + } + return null; + } } @Override @@ -56,6 +70,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> { @SuppressWarnings("unchecked") private void drop(E object, Position position, Direction direction) { + Pair<Position, Direction> update = fixInvalidDrop(object, position, + direction); + if (update != null) { + position = update.getFirst(); + direction = update.getSecond(); + } + objects.put(object, new Pair<E, Position>(object, position)); for (Pair<E, Position> i : ((Map<E, Pair<E, Position>>) objects.clone()) .values()) { @@ -103,11 +124,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> { * * @param object * to be dropped + * @param dir * @param p * the object is dropped at * @return null if the drop is valid, new position otherwise */ - protected Position fixInvalidDrop(E object, Position pos) { + protected Pair<Position, Direction> fixInvalidDrop(E object, Position pos, + Direction dir) { return null; } diff --git a/test/jrummikub/model/HandTest.java b/test/jrummikub/model/HandTest.java index b855edd..d913caf 100644 --- a/test/jrummikub/model/HandTest.java +++ b/test/jrummikub/model/HandTest.java @@ -89,8 +89,8 @@ public class HandTest { hand.drop(stone1, new Position(13, 0));
hand.drop(stone2, new Position(12.5f, 0));
- assertEquals(new Position(12.5f, 0), hand.getPosition(stone1));
- assertEquals(new Position(0, 1), hand.getPosition(stone2));
+ assertEquals(new Position(0, 1), hand.getPosition(stone1));
+ assertEquals(new Position(12.5f, 0), hand.getPosition(stone2));
}
@Test
|