diff options
Diffstat (limited to 'src/jrummikub/model/StoneSet.java')
-rw-r--r-- | src/jrummikub/model/StoneSet.java | 79 |
1 files changed, 48 insertions, 31 deletions
diff --git a/src/jrummikub/model/StoneSet.java b/src/jrummikub/model/StoneSet.java index 98d952f..465692a 100644 --- a/src/jrummikub/model/StoneSet.java +++ b/src/jrummikub/model/StoneSet.java @@ -43,45 +43,62 @@ public class StoneSet implements Iterable<Stone>, Sizeable { // is run if (stones.get(nonJoker1).getColor() == stones.get(nonJoker2) .getColor()) { - StoneColor runColor = stones.get(nonJoker1).getColor(); - int startValue = stones.get(nonJoker1).getValue() - nonJoker1; - int endValue = startValue + stones.size() - 1; - if (startValue < 1 || endValue > 13) { - return false; - } - for (int i = 0; i < stones.size(); i++) { - if (stones.get(i).isJoker()) { - continue; - } - if (stones.get(i).getColor() != runColor) { - // warum macht er das nicht? - return false; - } - if (stones.get(i).getValue() != i + startValue) { - return false; - } - } - return true; + return isValidRun(nonJoker1); } // is group else { - if (stones.size() > 4) { + return isValidGroup(); + } + } + + /** + * Test for rule conflict within the StoneSet, assuming we have a run + * + * @param referencePosition + * position of stone used as reference (any non-joker stone) + */ + private boolean isValidRun(int referencePosition) { + StoneColor runColor = stones.get(referencePosition).getColor(); + int startValue = stones.get(referencePosition).getValue() + - referencePosition; + int endValue = startValue + stones.size() - 1; + if (startValue < 1 || endValue > 13) { + return false; + } + for (int i = 0; i < stones.size(); i++) { + if (stones.get(i).isJoker()) { + continue; + } + if (stones.get(i).getColor() != runColor) { + // warum macht er das nicht? return false; } - Set<StoneColor> seenColors = new HashSet<StoneColor>(); - for (Stone i : stones) { - if (i.isJoker()) { - continue; - } - if (seenColors.contains(i.getColor())) { - return false; - } else { - seenColors.add(i.getColor()); - } + if (stones.get(i).getValue() != i + startValue) { + return false; + } + } + return true; + } + /** + * Test for rule conflict within the StoneSet, assuming we have a group + */ + private boolean isValidGroup() { + if (stones.size() > 4) { + return false; + } + Set<StoneColor> seenColors = new HashSet<StoneColor>(); + for (Stone i : stones) { + if (i.isJoker()) { + continue; + } + if (seenColors.contains(i.getColor())) { + return false; + } else { + seenColors.add(i.getColor()); } - return true; } + return true; } /** |