Implemented positioning of stones on hand

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@202 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Bennet Gerlach 2011-05-09 23:29:01 +02:00
parent f5f53cb551
commit 8e73d1ab37
3 changed files with 52 additions and 6 deletions

View file

@ -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);
}
}
}
}

View file

@ -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;
}

View file

@ -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