From 027656acf714c8c2015cd7a8d3b84367e4b97640 Mon Sep 17 00:00:00 2001 From: Ida Massow Date: Sun, 1 May 2011 01:05:56 +0200 Subject: StoneSet isValid fertig und getestet git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@46 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/model/StoneSet.java | 120 ++++++++++++++++++++++++++++---------- 1 file changed, 88 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/jrummikub/model/StoneSet.java b/src/jrummikub/model/StoneSet.java index ba96625..f10eca0 100644 --- a/src/jrummikub/model/StoneSet.java +++ b/src/jrummikub/model/StoneSet.java @@ -2,8 +2,10 @@ package jrummikub.model; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Set; import sun.reflect.generics.reflectiveObjects.NotImplementedException; @@ -14,17 +16,71 @@ public class StoneSet implements Iterable { private List stones; public StoneSet(Stone stone) { - stones = Collections.singletonList(stone); + stones = Collections.singletonList(stone); } public StoneSet(List stones) { - this.stones = new ArrayList(stones); + this.stones = new ArrayList(stones); } /** Test for rule conflict within the StoneSet */ public boolean isValid() { - return false; + if (stones.size() < 3) { + return false; + } + int nonJoker1 = -1, nonJoker2 = -1; + for (int i = 0; i < stones.size(); i++) { + if (stones.get(i).isJoker()) { + continue; + } + nonJoker2 = nonJoker1; + nonJoker1 = i; + } + if (nonJoker2 == -1) { + return true; + } + // 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; + } + // is group + else { + if (stones.size() > 4) { + return false; + } + Set seenColors = new HashSet(); + for (Stone i : stones) { + if (i.isJoker()) { + continue; + } + if (seenColors.contains(i.getColor())) { + return false; + } else { + seenColors.add(i.getColor()); + } + } + return true; + } } /** @@ -35,12 +91,11 @@ public class StoneSet implements Iterable { * Splitting {@link Position} */ public Pair splitAt(int position) { - //Exception falls falscher index - if (position==0||position==stones.size()){ - - } - else { - + // Exception falls falscher index + if (position == 0 || position == stones.size()) { + + } else { + } return null; @@ -65,28 +120,29 @@ public class StoneSet implements Iterable { return stones.get(i); } - @Override - public Iterator iterator() { - final Iterator it = stones.iterator(); - - return new Iterator(){ - - @Override - public boolean hasNext() { - return it.hasNext(); - } - - @Override - public Stone next() { - return it.next(); - } - - @Override - public void remove() { - // removing stones is impossible - - throw new NotImplementedException(); - }}; - } + @Override + public Iterator iterator() { + final Iterator it = stones.iterator(); + + return new Iterator() { + + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public Stone next() { + return it.next(); + } + + @Override + public void remove() { + // removing stones is impossible + + throw new NotImplementedException(); + } + }; + } } -- cgit v1.2.3