diff options
-rw-r--r-- | src/jrummikub/control/turn/HumanTurnControl.java | 2 | ||||
-rw-r--r-- | src/jrummikub/model/StoneSet.java | 2 | ||||
-rw-r--r-- | src/jrummikub/model/StoneTray.java | 65 | ||||
-rw-r--r-- | src/jrummikub/view/impl/TablePanel.java | 8 | ||||
-rw-r--r-- | test/jrummikub/control/turn/TurnControlTest.java | 48 | ||||
-rw-r--r-- | test/jrummikub/model/StoneSetTest.java | 8 |
6 files changed, 71 insertions, 62 deletions
diff --git a/src/jrummikub/control/turn/HumanTurnControl.java b/src/jrummikub/control/turn/HumanTurnControl.java index 55b17f2..85f242f 100644 --- a/src/jrummikub/control/turn/HumanTurnControl.java +++ b/src/jrummikub/control/turn/HumanTurnControl.java @@ -439,7 +439,7 @@ public class HumanTurnControl extends AbstractTurnControl { table.drop( new StoneSet(selectedStones), new Position( - pos.getX() + (set.size() - selectedStones.size()) * 0.5f, pos + pos.getX() + (set.getSize() - selectedStones.size()) * 0.5f, pos .getY())); } diff --git a/src/jrummikub/model/StoneSet.java b/src/jrummikub/model/StoneSet.java index 39fb158..0d53b52 100644 --- a/src/jrummikub/model/StoneSet.java +++ b/src/jrummikub/model/StoneSet.java @@ -208,7 +208,7 @@ public class StoneSet implements Iterable<Stone>, Sizeable { * * @return number of stones */ - public int size() { + public int getSize() { return stones.size(); } diff --git a/src/jrummikub/model/StoneTray.java b/src/jrummikub/model/StoneTray.java index 60b8cc3..c4230f2 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>>(); @@ -50,7 +50,8 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> { if (currentObject == object) continue; Position currentPosition = getPosition(currentObject); - if (!objectsOverlap(object, position, currentObject, currentPosition)) { + if (!objectsOverlap(object, position, currentObject, + currentPosition)) { continue; } // Object would be placed inside the current object @@ -60,23 +61,22 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> { Position newPosition = null; // Move object to avoid overlap switch (newDirection) { - case TOP: - newPosition = new Position(currentPosition.getX(), position.getY() - - currentObject.getHeight()); - break; - case BOTTOM: - newPosition = new Position(currentPosition.getX(), position.getY() - + object.getHeight()); - break; - case LEFT: - newPosition = new Position( - position.getX() - currentObject.getWidth(), - currentPosition.getY()); - break; - case RIGHT: - newPosition = new Position(position.getX() + object.getWidth(), - currentPosition.getY()); - break; + case TOP: + newPosition = new Position(currentPosition.getX(), + position.getY() - currentObject.getHeight()); + break; + case BOTTOM: + newPosition = new Position(currentPosition.getX(), + position.getY() + object.getHeight()); + break; + case LEFT: + newPosition = new Position(position.getX() + - currentObject.getWidth(), currentPosition.getY()); + break; + case RIGHT: + newPosition = new Position(position.getX() + object.getWidth(), + currentPosition.getY()); + break; } objects.remove(currentObject); @@ -85,14 +85,14 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> { } /** - * Checks whether the object may be placed on the given position, computes new - * position if not + * Checks whether the object may be placed on the given position, computes + * new position if not * * @param object - * to be dropped + * to be dropped * @param dir * @param pos - * the object is dropped at + * the object is dropped at * @return null if the drop is valid, new position otherwise */ protected Pair<Position, Direction> fixInvalidDrop(E object, Position pos, @@ -100,7 +100,7 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> { return null; } - private boolean lessOrEqual(float x, float y) { + private static boolean lessOrEqual(float x, float y) { if (-0.000001f < y && y < 0.000001f) { return (x < y + 0.000001f); } @@ -120,13 +120,15 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> { if (lessOrEqual(position1.getX() + object1.getWidth(), position2.getX())) { return false; } - if (lessOrEqual(position1.getY() + object1.getHeight(), position2.getY())) { + if (lessOrEqual(position1.getY() + object1.getHeight(), + position2.getY())) { return false; } if (lessOrEqual(position2.getX() + object2.getWidth(), position1.getX())) { return false; } - if (lessOrEqual(position2.getY() + object2.getHeight(), position1.getY())) { + if (lessOrEqual(position2.getY() + object2.getHeight(), + position1.getY())) { return false; } return true; @@ -156,19 +158,26 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> { } } + /** + * Will the object be moved horizontally or vertically + * + * @return boolean vertical movement + */ private boolean getMoveOrientation(E object, Position position, Pair<E, Position> blocking) { float objectRight = position.getX() + object.getWidth(); 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 diff --git a/src/jrummikub/view/impl/TablePanel.java b/src/jrummikub/view/impl/TablePanel.java index 31f6296..7c53d28 100644 --- a/src/jrummikub/view/impl/TablePanel.java +++ b/src/jrummikub/view/impl/TablePanel.java @@ -147,8 +147,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel { if (p.getY() < miny) miny = p.getY(); - if (p.getX() + stoneSet.size() > maxx) - maxx = p.getX() + stoneSet.size(); + if (p.getX() + stoneSet.getSize() > maxx) + maxx = p.getX() + stoneSet.getSize(); if (p.getY() + 1 > maxy) maxy = p.getY() + 1; @@ -204,7 +204,7 @@ class TablePanel extends AbstractStonePanel implements ITablePanel { } // right connector - rect = new Rectangle2D.Float(x + stoneSet.size(), y, CONNECTOR_WIDTH, 1); + rect = new Rectangle2D.Float(x + stoneSet.getSize(), y, CONNECTOR_WIDTH, 1); if (rect.contains(pos.getX(), pos.getY())) { rightConnectorClickEvent.emit(stoneSet); return true; @@ -235,7 +235,7 @@ class TablePanel extends AbstractStonePanel implements ITablePanel { } // right connector - rect = new Rectangle2D.Float(x + stoneSet.size(), y, CONNECTOR_WIDTH, 1); + rect = new Rectangle2D.Float(x + stoneSet.getSize(), y, CONNECTOR_WIDTH, 1); if (rect.contains(pos.getX(), pos.getY())) { rightHoveredConnector = stoneSet; break; diff --git a/test/jrummikub/control/turn/TurnControlTest.java b/test/jrummikub/control/turn/TurnControlTest.java index 4784f98..ed0b758 100644 --- a/test/jrummikub/control/turn/TurnControlTest.java +++ b/test/jrummikub/control/turn/TurnControlTest.java @@ -663,7 +663,7 @@ public class TurnControlTest { // tablecheck assertEquals(2, table.getSize()); StoneSet newSet1, newSet2; - if (table.getSetArray()[0].size() == 3) { + if (table.getSetArray()[0].getSize() == 3) { newSet2 = table.getSetArray()[0]; newSet1 = table.getSetArray()[1]; } else { @@ -672,7 +672,7 @@ public class TurnControlTest { } assertSame(oldSet2, newSet2); // setcheck - assertEquals(9, newSet1.size()); + assertEquals(9, newSet1.getSize()); assertSame(newSet1.get(0), blueThree); assertSame(newSet1.get(1), redOne); assertSame(newSet1.get(2), redThree); @@ -691,7 +691,7 @@ public class TurnControlTest { assertSame(mockHand.stones.get(0).getFirst(), blueFour); // tablecheck assertEquals(2, table.getSize()); - if (table.getSetArray()[0].size() == 5) { + if (table.getSetArray()[0].getSize() == 5) { newSet2 = table.getSetArray()[0]; newSet1 = table.getSetArray()[1]; } else { @@ -699,7 +699,7 @@ public class TurnControlTest { newSet2 = table.getSetArray()[1]; } // setcheck1 - assertEquals(7, newSet1.size()); + assertEquals(7, newSet1.getSize()); assertSame(newSet1.get(0), blueThree); assertSame(newSet1.get(1), blueOne); assertSame(newSet1.get(2), blackOne); @@ -708,7 +708,7 @@ public class TurnControlTest { assertSame(newSet1.get(5), blackTwo); assertSame(newSet1.get(6), blackThree); // setcheck2 - assertEquals(5, newSet2.size()); + assertEquals(5, newSet2.getSize()); assertSame(newSet2.get(0), redOne); assertSame(newSet2.get(1), redThree); assertSame(newSet2.get(2), blueTwo); @@ -723,7 +723,7 @@ public class TurnControlTest { assertEquals(0, mockHand.getSize()); // tablecheck assertEquals(2, table.getSize()); - if (table.getSetArray()[0].size() == 6) { + if (table.getSetArray()[0].getSize() == 6) { newSet2 = table.getSetArray()[0]; newSet1 = table.getSetArray()[1]; } else { @@ -731,9 +731,9 @@ public class TurnControlTest { newSet2 = table.getSetArray()[1]; } // setcheck1 - assertEquals(7, newSet1.size()); + assertEquals(7, newSet1.getSize()); // setcheck2 - assertEquals(6, newSet2.size()); + assertEquals(6, newSet2.getSize()); assertSame(newSet2.get(0), blueFour); assertSame(newSet2.get(1), redOne); assertSame(newSet2.get(2), redThree); @@ -781,7 +781,7 @@ public class TurnControlTest { // tablecheck assertEquals(2, table.getSize()); StoneSet newSet1, newSet2; - if (table.getSetArray()[0].size() == 3) { + if (table.getSetArray()[0].getSize() == 3) { newSet2 = table.getSetArray()[0]; newSet1 = table.getSetArray()[1]; } else { @@ -790,7 +790,7 @@ public class TurnControlTest { } assertSame(oldSet2, newSet2); // setcheck - assertEquals(9, newSet1.size()); + assertEquals(9, newSet1.getSize()); assertSame(newSet1.get(0), blueOne); assertSame(newSet1.get(1), blackOne); assertSame(newSet1.get(2), redTwo); @@ -809,7 +809,7 @@ public class TurnControlTest { assertSame(mockHand.stones.get(0).getFirst(), blueFour); // tablecheck assertEquals(2, table.getSize()); - if (table.getSetArray()[0].size() == 5) { + if (table.getSetArray()[0].getSize() == 5) { newSet2 = table.getSetArray()[0]; newSet1 = table.getSetArray()[1]; } else { @@ -817,7 +817,7 @@ public class TurnControlTest { newSet2 = table.getSetArray()[1]; } // setcheck1 - assertEquals(7, newSet1.size()); + assertEquals(7, newSet1.getSize()); assertSame(newSet1.get(0), blueOne); assertSame(newSet1.get(1), blackOne); assertSame(newSet1.get(2), redTwo); @@ -826,7 +826,7 @@ public class TurnControlTest { assertSame(newSet1.get(5), blackThree); assertSame(newSet1.get(6), blueThree); // setcheck2 - assertEquals(5, newSet2.size()); + assertEquals(5, newSet2.getSize()); assertSame(newSet2.get(0), blueTwo); assertSame(newSet2.get(1), blackFour); assertSame(newSet2.get(2), blackFive); @@ -841,7 +841,7 @@ public class TurnControlTest { assertEquals(0, mockHand.getSize()); // tablecheck assertEquals(2, table.getSize()); - if (table.getSetArray()[0].size() == 6) { + if (table.getSetArray()[0].getSize() == 6) { newSet2 = table.getSetArray()[0]; newSet1 = table.getSetArray()[1]; } else { @@ -849,9 +849,9 @@ public class TurnControlTest { newSet2 = table.getSetArray()[1]; } // setcheck1 - assertEquals(7, newSet1.size()); + assertEquals(7, newSet1.getSize()); // setcheck2 - assertEquals(6, newSet2.size()); + assertEquals(6, newSet2.getSize()); assertSame(newSet2.get(0), blueTwo); assertSame(newSet2.get(1), blackFour); assertSame(newSet2.get(2), blackFive); @@ -900,18 +900,18 @@ public class TurnControlTest { // tablecheck StoneSet newSet1, newSet2, newSet3; assertEquals(3, table.getSize()); - if (table.getSetArray()[0].size() == 2) { + if (table.getSetArray()[0].getSize() == 2) { newSet2 = table.getSetArray()[0]; - if (table.getSetArray()[1].size() == 4) { + if (table.getSetArray()[1].getSize() == 4) { newSet3 = table.getSetArray()[1]; newSet1 = table.getSetArray()[2]; } else { newSet3 = table.getSetArray()[2]; newSet1 = table.getSetArray()[1]; } - } else if (table.getSetArray()[0].size() == 4) { + } else if (table.getSetArray()[0].getSize() == 4) { newSet3 = table.getSetArray()[0]; - if (table.getSetArray()[1].size() == 2) { + if (table.getSetArray()[1].getSize() == 2) { newSet2 = table.getSetArray()[1]; newSet1 = table.getSetArray()[2]; } else { @@ -920,7 +920,7 @@ public class TurnControlTest { } } else { newSet1 = table.getSetArray()[0]; - if (table.getSetArray()[1].size() == 2) { + if (table.getSetArray()[1].getSize() == 2) { newSet2 = table.getSetArray()[1]; newSet3 = table.getSetArray()[2]; } else { @@ -930,7 +930,7 @@ public class TurnControlTest { } // setcheck1 - assertEquals(6, newSet1.size()); + assertEquals(6, newSet1.getSize()); assertSame(newSet1.get(0), blueOne); assertSame(newSet1.get(1), blackOne); assertSame(newSet1.get(2), redTwo); @@ -938,11 +938,11 @@ public class TurnControlTest { assertSame(newSet1.get(4), blackTwo); assertSame(newSet1.get(5), blackThree); // setcheck2 - assertEquals(2, newSet2.size()); + assertEquals(2, newSet2.getSize()); assertSame(newSet2.get(0), blackFour); assertSame(newSet2.get(1), blackFive); // setcheck1 - assertEquals(4, newSet3.size()); + assertEquals(4, newSet3.getSize()); assertSame(newSet3.get(0), blueThree); assertSame(newSet3.get(1), redOne); assertSame(newSet3.get(2), redThree); diff --git a/test/jrummikub/model/StoneSetTest.java b/test/jrummikub/model/StoneSetTest.java index d944369..fda4c6c 100644 --- a/test/jrummikub/model/StoneSetTest.java +++ b/test/jrummikub/model/StoneSetTest.java @@ -272,8 +272,8 @@ public class StoneSetTest { StoneSet testSet = createTestSet(); Pair<StoneSet, StoneSet> newSets = testSet.splitAt(1); // Sets have right size - assertEquals(1, newSets.getFirst().size()); - assertEquals(2, newSets.getSecond().size()); + assertEquals(1, newSets.getFirst().getSize()); + assertEquals(2, newSets.getSecond().getSize()); // Set have right Stones assertSame(testSet.get(0), newSets.getFirst().get(0)); assertSame(testSet.get(1), newSets.getSecond().get(0)); @@ -297,7 +297,7 @@ public class StoneSetTest { StoneSet secondSet = new StoneSet(new Stone(2, BLUE)); StoneSet joinedSet = testSet.join(secondSet); // Sets have right size - assertEquals(4, joinedSet.size()); + assertEquals(4, joinedSet.getSize()); // Set have right Stones assertSame(testSet.get(0), joinedSet.get(0)); assertSame(testSet.get(1), joinedSet.get(1)); @@ -317,7 +317,7 @@ public class StoneSetTest { i++; } - assertEquals(i, testSet.size()); + assertEquals(i, testSet.getSize()); } /** |