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:
parent
f5f53cb551
commit
8e73d1ab37
3 changed files with 52 additions and 6 deletions
|
@ -1,10 +1,33 @@
|
||||||
package jrummikub.model;
|
package jrummikub.model;
|
||||||
|
|
||||||
|
import jrummikub.util.Pair;
|
||||||
|
|
||||||
|
import static jrummikub.model.StoneTray.Direction.*;
|
||||||
|
|
||||||
/** Class managing a {@link Player}'s {@link Stone}s */
|
/** Class managing a {@link Player}'s {@link Stone}s */
|
||||||
public class Hand extends StoneTray<Stone> implements IHand {
|
public class Hand extends StoneTray<Stone> implements IHand {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Position fixInvalidDrop(Stone stone, Position pos) {
|
protected Pair<Position, Direction> fixInvalidDrop(Stone stone, Position pos,
|
||||||
return null;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,8 +18,22 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
|
|
||||||
/** Possible move directions in case of overlapping Stones/Sets */
|
/** Possible move directions in case of overlapping Stones/Sets */
|
||||||
|
|
||||||
private static enum Direction {
|
protected static enum Direction {
|
||||||
LEFT, RIGHT, TOP, BOTTOM;
|
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
|
@Override
|
||||||
|
@ -56,6 +70,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private void drop(E object, Position position, Direction direction) {
|
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));
|
objects.put(object, new Pair<E, Position>(object, position));
|
||||||
for (Pair<E, Position> i : ((Map<E, Pair<E, Position>>) objects.clone())
|
for (Pair<E, Position> i : ((Map<E, Pair<E, Position>>) objects.clone())
|
||||||
.values()) {
|
.values()) {
|
||||||
|
@ -103,11 +124,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
|
||||||
*
|
*
|
||||||
* @param object
|
* @param object
|
||||||
* to be dropped
|
* to be dropped
|
||||||
|
* @param dir
|
||||||
* @param p
|
* @param p
|
||||||
* the object is dropped at
|
* the object is dropped at
|
||||||
* @return null if the drop is valid, new position otherwise
|
* @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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -89,8 +89,8 @@ public class HandTest {
|
||||||
hand.drop(stone1, new Position(13, 0));
|
hand.drop(stone1, new Position(13, 0));
|
||||||
hand.drop(stone2, new Position(12.5f, 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(stone1));
|
||||||
assertEquals(new Position(0, 1), hand.getPosition(stone2));
|
assertEquals(new Position(12.5f, 0), hand.getPosition(stone2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Reference in a new issue