package jrummikub.model; import jrummikub.util.Pair; /** Class administering the {@link Stone}s on the game-Table */ public class Table extends StoneTray { /** * Removes {@link Stone} from the Table * * @param stone * stone to pick up */ public void pickUpStone(Stone stone) { // Find the set of the stone StoneSet set = null; Position setPosition = null; int stonePosition = 0; setLoop: for (Pair 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 firstSplit = set.splitAt(stonePosition); Pair 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() { for (Pair i : this) { if (!i.getFirst().isValid()) { return false; } } return true; } }