From 8e73d1ab3734b4e5b88edcf024ffc37bd03225e6 Mon Sep 17 00:00:00 2001 From: Bennet Gerlach Date: Mon, 9 May 2011 23:29:01 +0200 Subject: Implemented positioning of stones on hand git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@202 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/model/Hand.java | 27 +++++++++++++++++++++++++-- src/jrummikub/model/StoneTray.java | 27 +++++++++++++++++++++++++-- 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 implements IHand { @Override - protected Position fixInvalidDrop(Stone stone, Position pos) { - return null; + protected Pair 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(new Position(0, 0), RIGHT); + } else { + return new Pair(new Position(13, 0), LEFT); + } + } else { + if (y == 0) { + return new Pair(new Position(0, 1), RIGHT); + } else { + return new Pair(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 implements IStoneTray { /** 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 implements IStoneTray { @SuppressWarnings("unchecked") private void drop(E object, Position position, Direction direction) { + Pair update = fixInvalidDrop(object, position, + direction); + if (update != null) { + position = update.getFirst(); + direction = update.getSecond(); + } + objects.put(object, new Pair(object, position)); for (Pair i : ((Map>) objects.clone()) .values()) { @@ -103,11 +124,13 @@ public class StoneTray implements IStoneTray { * * @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 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 -- cgit v1.2.3