Classification of runs and sets

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@83 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Jannis Harder 2011-05-03 19:06:12 +02:00
parent 2eaf873ec6
commit f8f75fde6d
2 changed files with 42 additions and 25 deletions

View file

@ -10,6 +10,7 @@ import java.util.Set;
import sun.reflect.generics.reflectiveObjects.NotImplementedException; import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import jrummikub.util.Pair; import jrummikub.util.Pair;
import static jrummikub.model.StoneSet.Type.*;
/** Class managing {@link Stone}s joined together to form sets */ /** Class managing {@link Stone}s joined together to form sets */
public class StoneSet implements Iterable<Stone>, Sizeable { public class StoneSet implements Iterable<Stone>, Sizeable {
@ -25,14 +26,29 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
this.stones = new ArrayList<Stone>(stones); this.stones = new ArrayList<Stone>(stones);
} }
public enum Type {
GROUP, RUN, INVALID
}
/** /**
* Test for rule conflict within the StoneSet * Test for rule conflict within the StoneSet
* *
* @return true when the set is valid according to the rules * @return true when the set is valid according to the rules
*/ */
public boolean isValid() { public boolean isValid() {
return classify() != INVALID;
}
/**
* Test for rule conflict within the StoneSet and determine whether the set
* is a group or a run
*
* @return GROUP or RUN for valid sets, INVALID otherwise
*/
public Type classify() {
// TODO: extend this for score calculation (release 2...)
if (stones.size() < 3) { if (stones.size() < 3) {
return false; return INVALID;
} }
int nonJoker1 = -1, nonJoker2 = -1; int nonJoker1 = -1, nonJoker2 = -1;
for (int i = 0; i < stones.size(); i++) { for (int i = 0; i < stones.size(); i++) {
@ -43,17 +59,17 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
nonJoker1 = i; nonJoker1 = i;
} }
if (nonJoker2 == -1) { if (nonJoker2 == -1) {
return true; return GROUP;
} }
// is run // is run
if (stones.get(nonJoker1).getColor() == stones.get(nonJoker2) if (stones.get(nonJoker1).getColor() == stones.get(nonJoker2)
.getColor()) { .getColor()) {
return isValidRun(nonJoker1); return isValidRun(nonJoker1) ? RUN : INVALID;
} }
// is group // is group
else { else {
return isValidGroup(); return isValidGroup() ? GROUP : INVALID;
} }
} }

View file

@ -6,6 +6,7 @@ import java.util.List;
import jrummikub.util.Pair; import jrummikub.util.Pair;
import static jrummikub.model.StoneColor.*; import static jrummikub.model.StoneColor.*;
import static jrummikub.model.StoneSet.Type.*;
import org.junit.*; import org.junit.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -14,38 +15,38 @@ public class StoneSetTest {
// Is Valid-Test // Is Valid-Test
// valid // valid
public void assertSet(boolean valid, List<Stone> stones) { public void assertSet(StoneSet.Type expectedType, List<Stone> stones) {
StoneSet set = new StoneSet(stones); StoneSet set = new StoneSet(stones);
assertTrue(valid == set.isValid()); assertSame(expectedType, set.classify());
} }
@Test @Test
public void doubleJoker() { public void doubleJoker() {
assertSet(true, Arrays.asList(new Stone(RED), new Stone(BLACK), assertSet(GROUP, Arrays.asList(new Stone(RED), new Stone(BLACK),
new Stone(1, BLACK))); new Stone(1, BLACK)));
} }
@Test @Test
public void groups() { public void groups() {
assertSet(true, Arrays.asList(new Stone(1, RED), new Stone(1, BLACK), assertSet(GROUP, Arrays.asList(new Stone(1, RED), new Stone(1, BLACK),
new Stone(1, BLUE))); new Stone(1, BLUE)));
assertSet(true, Arrays.asList(new Stone(1, RED), new Stone(1, BLACK), assertSet(GROUP, Arrays.asList(new Stone(1, RED), new Stone(1, BLACK),
new Stone(1, BLUE), new Stone(1, ORANGE))); new Stone(1, BLUE), new Stone(1, ORANGE)));
} }
@Test @Test
public void runs() { public void runs() {
assertSet(true, Arrays.asList(new Stone(1, RED), new Stone(2, RED), assertSet(RUN, Arrays.asList(new Stone(1, RED), new Stone(2, RED),
new Stone(3, RED))); new Stone(3, RED)));
assertSet(true, Arrays.asList(new Stone(4, BLUE), new Stone(5, BLUE), assertSet(RUN, Arrays.asList(new Stone(4, BLUE), new Stone(5, BLUE),
new Stone(6, BLUE))); new Stone(6, BLUE)));
} }
@Test @Test
public void singleJoker() { public void singleJoker() {
assertSet(true, Arrays.asList(new Stone(1, RED), new Stone(1, BLACK), assertSet(GROUP, Arrays.asList(new Stone(1, RED), new Stone(1, BLACK),
new Stone(RED))); new Stone(RED)));
assertSet(true, Arrays.asList(new Stone(2, RED), new Stone(3, RED), assertSet(RUN, Arrays.asList(new Stone(2, RED), new Stone(3, RED),
new Stone(BLACK))); new Stone(BLACK)));
} }
@ -53,41 +54,41 @@ public class StoneSetTest {
@Test @Test
public void outOfBounds() { public void outOfBounds() {
assertSet(false, Arrays.asList(new Stone(RED), new Stone(1, RED), assertSet(INVALID, Arrays.asList(new Stone(RED), new Stone(1, RED),
new Stone(2, RED))); new Stone(2, RED)));
assertSet(false, Arrays.asList(new Stone(12, RED), new Stone(13, RED), assertSet(INVALID, Arrays.asList(new Stone(12, RED), new Stone(13, RED),
new Stone(RED))); new Stone(RED)));
assertSet(false, Arrays.asList(new Stone(RED), new Stone(BLACK), assertSet(INVALID, Arrays.asList(new Stone(RED), new Stone(BLACK),
new Stone(1, RED), new Stone(2, RED))); new Stone(1, RED), new Stone(2, RED)));
} }
@Test @Test
public void sameColor() { public void sameColor() {
assertSet(false, Arrays.asList(new Stone(1, RED), new Stone(1, RED), assertSet(INVALID, Arrays.asList(new Stone(1, RED), new Stone(1, RED),
new Stone(1, BLUE))); new Stone(1, BLUE)));
assertSet(false, Arrays.asList(new Stone(1, RED), new Stone(1, BLUE), assertSet(INVALID, Arrays.asList(new Stone(1, RED), new Stone(1, BLUE),
new Stone(1, BLACK), new Stone(1, ORANGE), new Stone(RED))); new Stone(1, BLACK), new Stone(1, ORANGE), new Stone(RED)));
} }
@Test @Test
public void incorrectOrder() { public void incorrectOrder() {
assertSet(false, Arrays.asList(new Stone(4, RED), new Stone(6, RED), assertSet(INVALID, Arrays.asList(new Stone(4, RED), new Stone(6, RED),
new Stone(5, RED))); new Stone(5, RED)));
assertSet(false, Arrays.asList(new Stone(4, RED), new Stone(6, RED), assertSet(INVALID, Arrays.asList(new Stone(4, RED), new Stone(6, RED),
new Stone(RED))); new Stone(RED)));
assertSet(false, Arrays.asList(new Stone(4, RED), new Stone(RED), assertSet(INVALID, Arrays.asList(new Stone(4, RED), new Stone(RED),
new Stone(5, RED))); new Stone(5, RED)));
} }
@Test @Test
public void otherInvalid() { public void otherInvalid() {
assertSet(false, Arrays.asList(new Stone(4, RED), new Stone(5, RED), assertSet(INVALID, Arrays.asList(new Stone(4, RED), new Stone(5, RED),
new Stone(7, RED))); new Stone(7, RED)));
assertSet(false, Arrays.asList(new Stone(4, RED), new Stone(5, BLUE), assertSet(INVALID, Arrays.asList(new Stone(4, RED), new Stone(5, BLUE),
new Stone(6, RED))); new Stone(6, RED)));
assertSet(false, Arrays.asList(new Stone(4, RED), new Stone(5, RED))); assertSet(INVALID, Arrays.asList(new Stone(4, RED), new Stone(5, RED)));
assertSet(false, Arrays.asList(new Stone(4, BLUE), new Stone(5, RED), assertSet(INVALID, Arrays.asList(new Stone(4, BLUE), new Stone(5, RED),
new Stone(6, RED))); new Stone(6, RED)));
} }