Tests und imlementierung für Wert von Sets bestimmen, fertig und getestet

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@242 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Ida Massow 2011-05-16 20:54:37 +02:00
parent 9ad7f0822e
commit f7aace7234
5 changed files with 147 additions and 58 deletions

View file

@ -52,7 +52,7 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
* @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; return classify().getFirst() != INVALID;
} }
/** /**
@ -61,10 +61,11 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
* *
* @return GROUP or RUN for valid sets, INVALID otherwise * @return GROUP or RUN for valid sets, INVALID otherwise
*/ */
public Type classify() { // TODO: Vernünftige Werte Returnen
public Pair<Type, Integer> classify() {
// TODO: extend this for score calculation (release 2...) // TODO: extend this for score calculation (release 2...)
if (stones.size() < 3) { if (stones.size() < 3) {
return INVALID; return new Pair<Type, Integer>(INVALID, 0);
} }
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++) {
@ -75,18 +76,36 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
nonJoker1 = i; nonJoker1 = i;
} }
if (nonJoker2 == -1) { if (nonJoker2 == -1) {
return GROUP; // Z first
if (nonJoker1 == 0 && stones.get(nonJoker1).getValue() < 12) {
int origValue = stones.get(nonJoker1).getValue();
int value = 0;
for (int i = 0; i < stones.size(); i++) {
value += origValue + i;
} }
return new Pair<Type, Integer>(RUN, value);
} else {
int value = stones.get(nonJoker1).getValue() * stones.size();
return new Pair<Type, Integer>(GROUP, value);
}
}
// 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) ? RUN : INVALID; if (isValidRun(nonJoker1) > 0) {
return new Pair<Type, Integer>(RUN, isValidRun(nonJoker1));
}
return new Pair<Type, Integer>(INVALID, 0);
} }
// is group // is group
else { else {
return isValidGroup(stones.get(nonJoker1).getValue()) ? GROUP if (isValidGroup(stones.get(nonJoker1).getValue()) > 0) {
: INVALID; return new Pair<Type, Integer>(GROUP, isValidGroup(stones.get(
nonJoker1).getValue()));
}
return new Pair<Type, Integer>(INVALID, 0);
} }
} }
@ -96,13 +115,13 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
* @param referencePosition * @param referencePosition
* position of stone used as reference (any non-joker stone) * position of stone used as reference (any non-joker stone)
*/ */
private boolean isValidRun(int referencePosition) { private int isValidRun(int referencePosition) {
StoneColor runColor = stones.get(referencePosition).getColor(); StoneColor runColor = stones.get(referencePosition).getColor();
int startValue = stones.get(referencePosition).getValue() int startValue = stones.get(referencePosition).getValue()
- referencePosition; - referencePosition;
int endValue = startValue + stones.size() - 1; int endValue = startValue + stones.size() - 1;
if (startValue < 1 || endValue > 13) { if (startValue < 1 || endValue > 13) {
return false; return 0;
} }
for (int i = 0; i < stones.size(); i++) { for (int i = 0; i < stones.size(); i++) {
if (stones.get(i).isJoker()) { if (stones.get(i).isJoker()) {
@ -110,21 +129,25 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
} }
if (stones.get(i).getColor() != runColor) { if (stones.get(i).getColor() != runColor) {
// warum macht er das nicht? // warum macht er das nicht?
return false; return 0;
} }
if (stones.get(i).getValue() != i + startValue) { if (stones.get(i).getValue() != i + startValue) {
return false; return 0;
} }
} }
return true; int value = 0;
for (int i = 0; i < stones.size(); i++) {
value += startValue + i;
}
return value;
} }
/** /**
* Test for rule conflict within the StoneSet, assuming we have a group * Test for rule conflict within the StoneSet, assuming we have a group
*/ */
private boolean isValidGroup(int value) { private int isValidGroup(int value) {
if (stones.size() > 4) { if (stones.size() > 4) {
return false; return 0;
} }
Set<StoneColor> seenColors = new HashSet<StoneColor>(); Set<StoneColor> seenColors = new HashSet<StoneColor>();
for (Stone i : stones) { for (Stone i : stones) {
@ -132,15 +155,15 @@ public class StoneSet implements Iterable<Stone>, Sizeable {
continue; continue;
} }
if (i.getValue() != value) { if (i.getValue() != value) {
return false; return 0;
} }
if (seenColors.contains(i.getColor())) { if (seenColors.contains(i.getColor())) {
return false; return 0;
} else { } else {
seenColors.add(i.getColor()); seenColors.add(i.getColor());
} }
} }
return true; return value * stones.size();
} }
/** /**

View file

@ -79,7 +79,7 @@ public class Table extends StoneTray<StoneSet> implements ITable {
StoneSet leftSet = firstSplit.getFirst(); StoneSet leftSet = firstSplit.getFirst();
StoneSet rightSet = secondSplit.getSecond(); StoneSet rightSet = secondSplit.getSecond();
if (set.classify() == StoneSet.Type.RUN) { if (set.classify().getFirst() == StoneSet.Type.RUN) {
Position leftPosition, rightPosition; Position leftPosition, rightPosition;
leftPosition = setPosition; leftPosition = setPosition;
rightPosition = new Position(setPosition.getX() + stonePosition + 1, rightPosition = new Position(setPosition.getX() + stonePosition + 1,

View file

@ -92,6 +92,17 @@ public class RoundControlTest {
view.displayStartTurnPanel = false; view.displayStartTurnPanel = false;
} }
/** */
@Test
public void playerCameOutCorrectly(){
MockTable oldTable = new MockTable();
MockTable newTable = new MockTable();
Stone blueTen = new Stone(10, BLUE);
Stone redTen = new Stone(10, RED);
Stone blackTen = new Stone(10, BLACK);
}
/** */ /** */
@Test @Test
public void testDealStone() { public void testDealStone() {

View file

@ -31,4 +31,5 @@ public class GameStateTest {
testGame.nextPlayer(); testGame.nextPlayer();
assertSame(Color.red, testGame.getActivePlayer().getColor()); assertSame(Color.red, testGame.getActivePlayer().getColor());
} }
} }

View file

@ -16,92 +16,146 @@ import static org.junit.Assert.*;
*/ */
public class StoneSetTest { public class StoneSetTest {
private void assertSet(StoneSet.Type expectedType, List<Stone> stones) { private void assertSet(StoneSet.Type expectedType, Integer expectedValue,
List<Stone> stones) {
StoneSet set = new StoneSet(stones); StoneSet set = new StoneSet(stones);
assertSame(expectedType, set.classify()); assertSame(expectedType, set.classify().getFirst());
assertEquals(expectedValue, set.classify().getSecond());
} }
// valid // valid+point count
/** */ /** */
@Test @Test
public void doubleJoker() { public void doubleJoker() {
assertSet(GROUP, // 3 Stones
Arrays.asList(new Stone(RED), new Stone(BLACK), new Stone(1, BLACK))); // JJZ
assertSet(GROUP, 3, Arrays.asList(new Stone(RED), new Stone(BLACK),
new Stone(1, BLACK)));
// JZJ
assertSet(GROUP, 3, Arrays.asList(new Stone(RED), new Stone(1, BLACK),
new Stone(BLACK)));
// ZJJ
assertSet(RUN, 6, Arrays.asList(new Stone(1, RED), new Stone(RED),
new Stone(BLACK)));
// 4 Stones
// JJZZ
assertSet(GROUP, 4, Arrays.asList(new Stone(RED), new Stone(BLACK),
new Stone(1, BLACK), new Stone(1, RED)));
assertSet(RUN, 10, Arrays.asList(new Stone(RED), new Stone(BLACK),
new Stone(3, RED), new Stone(4, RED)));
// ZZJJ
assertSet(RUN, 10, Arrays.asList(new Stone(1, RED), new Stone(2, RED),
new Stone(BLACK), new Stone(RED)));
assertSet(GROUP, 4, Arrays.asList(new Stone(1, RED),
new Stone(1, BLACK), new Stone(BLACK), new Stone(RED)));
// ZJZJ
assertSet(GROUP, 4, Arrays.asList(new Stone(1, RED), new Stone(BLACK),
new Stone(1, BLACK), new Stone(RED)));
assertSet(RUN, 10, Arrays.asList(new Stone(1, RED), new Stone(RED),
new Stone(3, RED), new Stone(BLACK)));
// JZJZ
assertSet(GROUP, 4, Arrays.asList(new Stone(RED), new Stone(1, BLACK),
new Stone(BLACK), new Stone(1, RED)));
assertSet(RUN, 10, Arrays.asList(new Stone(RED), new Stone(2, RED),
new Stone(BLACK), new Stone(4, RED)));
// JZZJ
assertSet(GROUP, 4, Arrays.asList(new Stone(RED), new Stone(1, BLACK),
new Stone(1, RED), new Stone(BLACK)));
assertSet(RUN, 10, Arrays.asList(new Stone(RED), new Stone(2, RED),
new Stone(3, RED), new Stone(BLACK)));
// ZJJZ
assertSet(GROUP, 4, Arrays.asList(new Stone(1, RED), new Stone(BLACK),
new Stone(RED), new Stone(1, BLACK)));
assertSet(RUN, 10, Arrays.asList(new Stone(1, RED), new Stone(RED),
new Stone(BLACK), new Stone(4, RED)));
} }
/** */ /** */
@Test @Test
public void groups() { public void groups() {
assertSet(GROUP, Arrays.asList(new Stone(1, RED), new Stone(1, BLACK), assertSet(GROUP, 3, Arrays.asList(new Stone(1, RED),
new Stone(1, BLUE))); new Stone(1, BLACK), new Stone(1, BLUE)));
assertSet(GROUP, Arrays.asList(new Stone(1, RED), new Stone(1, BLACK), assertSet(GROUP, 4, Arrays.asList(new Stone(1, RED),
new Stone(1, BLUE), new Stone(1, ORANGE))); new Stone(1, BLACK), new Stone(1, BLUE), new Stone(1, ORANGE)));
} }
/** */ /** */
@Test @Test
public void runs() { public void runs() {
assertSet(RUN, assertSet(RUN, 6, Arrays.asList(new Stone(1, RED), new Stone(2, RED),
Arrays.asList(new Stone(1, RED), new Stone(2, RED), new Stone(3, RED))); new Stone(3, RED)));
assertSet(RUN, Arrays.asList(new Stone(4, BLUE), new Stone(5, BLUE), assertSet(RUN, 22, Arrays.asList(new Stone(4, BLUE), new Stone(5, BLUE),
new Stone(6, BLUE))); new Stone(6, BLUE), new Stone(7, BLUE)));
} }
/** */ /** */
@Test @Test
public void singleJoker() { public void singleJoker() {
assertSet(GROUP, // ZJZ
Arrays.asList(new Stone(1, RED), new Stone(1, BLACK), new Stone(RED))); assertSet(GROUP, 3, Arrays.asList(new Stone(1, RED), new Stone(BLACK),
assertSet(RUN, new Stone(1, BLACK)));
Arrays.asList(new Stone(2, RED), new Stone(3, RED), new Stone(BLACK))); assertSet(RUN, 6, Arrays.asList(new Stone(1, RED), new Stone(RED),
new Stone(3, RED)));
// JZZ
assertSet(GROUP, 3, Arrays.asList(new Stone(RED), new Stone(1, RED),
new Stone(1, BLACK)));
assertSet(RUN, 6, Arrays.asList(new Stone(RED), new Stone(2, RED),
new Stone(3, RED)));
// ZZJ
assertSet(GROUP, 3, Arrays.asList(new Stone(1, RED),
new Stone(1, BLACK), new Stone(BLACK)));
assertSet(RUN, 6, Arrays.asList(new Stone(1, RED), new Stone(2, RED),
new Stone(RED)));
} }
// invalid // invalid
/** */ /** */
@Test @Test
public void outOfBounds() { public void outOfBounds() {
assertSet(INVALID, Arrays.asList(new Stone(RED), new Stone(1, RED), assertSet(INVALID, 0, Arrays.asList(new Stone(RED), new Stone(1, RED),
new Stone(2, RED))); new Stone(2, RED)));
assertSet(INVALID, Arrays.asList(new Stone(12, RED), assertSet(INVALID, 0, Arrays.asList(new Stone(12, RED), new Stone(13,
new Stone(13, RED), new Stone(RED))); RED), new Stone(RED)));
assertSet(INVALID, Arrays.asList(new Stone(RED), new Stone(BLACK), assertSet(INVALID, 0, 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(INVALID, assertSet(INVALID, 0, Arrays.asList(new Stone(1, RED),
Arrays.asList(new Stone(1, RED), new Stone(1, RED), new Stone(1, BLUE))); new Stone(1, RED), new Stone(1, BLUE)));
assertSet(INVALID, Arrays.asList(new Stone(1, RED), new Stone(1, BLUE), assertSet(INVALID, 0, Arrays.asList(new Stone(1, RED), new Stone(1,
new Stone(1, BLACK), new Stone(1, ORANGE), new Stone(RED))); BLUE), new Stone(1, BLACK), new Stone(1, ORANGE),
new Stone(RED)));
} }
/** */ /** */
@Test @Test
public void incorrectOrder() { public void incorrectOrder() {
assertSet(INVALID, assertSet(INVALID, 0, Arrays.asList(new Stone(4, RED),
Arrays.asList(new Stone(4, RED), new Stone(6, RED), new Stone(5, RED))); new Stone(6, RED), new Stone(5, RED)));
assertSet(INVALID, assertSet(INVALID, 0, Arrays.asList(new Stone(4, RED),
Arrays.asList(new Stone(4, RED), new Stone(6, RED), new Stone(RED))); new Stone(6, RED), new Stone(RED)));
assertSet(INVALID, assertSet(INVALID, 0, Arrays.asList(new Stone(4, RED), new Stone(RED),
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(INVALID, assertSet(INVALID, 0, Arrays.asList(new Stone(4, RED),
Arrays.asList(new Stone(4, RED), new Stone(5, RED), new Stone(7, RED))); new Stone(5, RED), new Stone(7, RED)));
assertSet(INVALID, assertSet(INVALID, 0, Arrays.asList(new Stone(4, RED), new Stone(5,
Arrays.asList(new Stone(4, RED), new Stone(5, BLUE), new Stone(6, RED))); BLUE), new Stone(6, RED)));
assertSet(INVALID, Arrays.asList(new Stone(4, RED), new Stone(5, RED))); assertSet(INVALID, 0,
assertSet(INVALID, Arrays.asList(new Stone(4, RED), new Stone(5, RED)));
Arrays.asList(new Stone(4, BLUE), new Stone(5, RED), new Stone(6, RED))); assertSet(INVALID, 0, Arrays.asList(new Stone(4, BLUE), new Stone(5,
RED), new Stone(6, RED)));
// Regression test: // Regression test:
assertSet(INVALID, Arrays.asList(new Stone(12, ORANGE), new Stone(12, assertSet(INVALID, 0, Arrays.asList(new Stone(12, ORANGE), new Stone(
BLACK), new Stone(7, BLUE))); 12, BLACK), new Stone(7, BLUE)));
} }
// invalid Split // invalid Split