summaryrefslogtreecommitdiffstats
path: root/src/jrummikub
diff options
context:
space:
mode:
authorJannis Harder <harder@informatik.uni-luebeck.de>2011-05-03 19:06:16 +0200
committerJannis Harder <harder@informatik.uni-luebeck.de>2011-05-03 19:06:16 +0200
commit7b8732714deb27cbb9295d0022231e00a71d28bb (patch)
treee46c4d5a356cffa6a141a7ccd2027ebc74675daf /src/jrummikub
parent760f914494d2cd8552a2f4408fbceb38ac7027ba (diff)
downloadJRummikub-7b8732714deb27cbb9295d0022231e00a71d28bb.tar
JRummikub-7b8732714deb27cbb9295d0022231e00a71d28bb.zip
Implemented Table, passes tests
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@90 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub')
-rw-r--r--src/jrummikub/model/Table.java61
1 files changed, 58 insertions, 3 deletions
diff --git a/src/jrummikub/model/Table.java b/src/jrummikub/model/Table.java
index 59ef01d..e82ca03 100644
--- a/src/jrummikub/model/Table.java
+++ b/src/jrummikub/model/Table.java
@@ -1,5 +1,7 @@
package jrummikub.model;
+import jrummikub.util.Pair;
+
/** Class administering the {@link Stone}s on the game-Table */
public class Table extends StoneTray<StoneSet> {
@@ -11,13 +13,66 @@ public class Table extends StoneTray<StoneSet> {
* stone to pick up
*/
public void pickUpStone(Stone stone) {
- // TODO implement this method
+ // Find the set of the stone
+ StoneSet set = null;
+ Position setPosition = null;
+ int stonePosition = 0;
+ setLoop: for (Pair<StoneSet, Position> i : this) {
+ set = i.getFirst();
+ setPosition = i.getSecond();
+ stonePosition = 0;
+ for (Stone j : set) {
+ if (j == stone) {
+ break setLoop;
+ }
+ stonePosition++;
+ }
+ }
+ // Stone not found
+ if (set == null) {
+ return;
+ }
+
+ splitSet(set, setPosition, stonePosition);
+ }
+
+ private void splitSet(StoneSet set, Position setPosition, int stonePosition) {
+ pickUp(set);
+
+ Pair<StoneSet, StoneSet> firstSplit = set.splitAt(stonePosition);
+ Pair<StoneSet, StoneSet> secondSplit = firstSplit.getSecond().splitAt(1);
+
+ StoneSet leftSet = firstSplit.getFirst();
+ StoneSet rightSet = secondSplit.getSecond();
+
+ if (set.classify() == StoneSet.Type.RUN) {
+ Position leftPosition, rightPosition;
+ leftPosition = setPosition;
+ rightPosition = new Position(setPosition.getX() + stonePosition, setPosition.getY());
+
+ drop(leftSet, leftPosition);
+ drop(rightSet, rightPosition);
+ } else {
+ Position newPosition = new Position(setPosition.getX() + 0.5f, setPosition.getY());
+
+ if (leftSet == null) {
+ drop(rightSet, newPosition);
+ } else if (rightSet == null) {
+ drop(leftSet, newPosition);
+ } else {
+ drop(leftSet.join(rightSet), newPosition);
+ }
+ }
}
/** Tests the Table for rule conflicts by checking all the {@link StoneSet} */
public boolean isValid() {
- // TODO implement this method
- return false;
+ for (Pair<StoneSet, Position> i : this) {
+ if (!i.getFirst().isValid()) {
+ return false;
+ }
+ }
+ return true;
}
}