summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/jrummikub/model/Table.java36
-rw-r--r--test/jrummikub/model/TableTest.java11
2 files changed, 44 insertions, 3 deletions
diff --git a/src/jrummikub/model/Table.java b/src/jrummikub/model/Table.java
index 232986f..71ce27c 100644
--- a/src/jrummikub/model/Table.java
+++ b/src/jrummikub/model/Table.java
@@ -6,6 +6,17 @@ import jrummikub.util.Pair;
public class Table extends StoneTray<StoneSet> implements ITable {
+ private static class StoneInfo {
+ StoneSet set;
+ Position setPosition;
+ int stonePosition;
+ public StoneInfo(StoneSet set, Position setPosition, int stonePosition) {
+ this.set = set;
+ this.setPosition = setPosition;
+ this.stonePosition = stonePosition;
+ }
+ }
+
/**
* Removes {@link Stone} from the Table
*
@@ -14,7 +25,14 @@ public class Table extends StoneTray<StoneSet> implements ITable {
*/
@Override
public void pickUpStone(Stone stone) {
+ StoneInfo info = findStoneInfo(stone);
+
+ splitSet(info.set, info.setPosition, info.stonePosition);
+ }
+
+ private StoneInfo findStoneInfo(Stone stone) {
// Find the set of the stone
+ StoneInfo info;
StoneSet set = null;
Position setPosition = null;
int stonePosition = 0;
@@ -31,11 +49,23 @@ public class Table extends StoneTray<StoneSet> implements ITable {
}
// Stone not found
if (set == null) {
- return;
+ info = null;
+ } else {
+ info = new StoneInfo(set, setPosition, stonePosition);
}
-
- splitSet(set, setPosition, stonePosition);
+ return info;
+ }
+
+ @Override
+ public StoneSet findStoneSet(Stone stone) {
+ StoneInfo info = findStoneInfo(stone);
+ if (info == null) {
+ return null;
+ }
+ return info.set;
}
+
+
private void splitSet(StoneSet set, Position setPosition, int stonePosition) {
pickUp(set);
diff --git a/test/jrummikub/model/TableTest.java b/test/jrummikub/model/TableTest.java
index 17ed8c8..25cc3c3 100644
--- a/test/jrummikub/model/TableTest.java
+++ b/test/jrummikub/model/TableTest.java
@@ -69,4 +69,15 @@ public class TableTest {
}
assertEquals(2, counter);
}
+
+ @Test
+ public void testFindSet() {
+ Stone targetStone = new Stone(BLACK);
+ StoneSet testSet = new StoneSet(Arrays.asList(new Stone(1, RED), targetStone,
+ new Stone(3, RED)));
+ testTable.drop(testSet, new Position(0,0));
+
+ StoneSet foundSet = testTable.findStoneSet(targetStone);
+ assertSame(testSet, foundSet);
+ }
}