StoneSet isValid fertig und getestet
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@46 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
d028cb8a0c
commit
027656acf7
2 changed files with 95 additions and 35 deletions
|
@ -2,8 +2,10 @@ package jrummikub.model;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
|
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
|
||||||
|
|
||||||
|
@ -14,17 +16,71 @@ public class StoneSet implements Iterable<Stone> {
|
||||||
private List<Stone> stones;
|
private List<Stone> stones;
|
||||||
|
|
||||||
public StoneSet(Stone stone) {
|
public StoneSet(Stone stone) {
|
||||||
stones = Collections.singletonList(stone);
|
stones = Collections.singletonList(stone);
|
||||||
}
|
}
|
||||||
|
|
||||||
public StoneSet(List<Stone> stones) {
|
public StoneSet(List<Stone> stones) {
|
||||||
this.stones = new ArrayList<Stone>(stones);
|
this.stones = new ArrayList<Stone>(stones);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Test for rule conflict within the StoneSet */
|
/** Test for rule conflict within the StoneSet */
|
||||||
public boolean isValid() {
|
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<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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,11 +91,10 @@ public class StoneSet implements Iterable<Stone> {
|
||||||
* Splitting {@link Position}
|
* Splitting {@link Position}
|
||||||
*/
|
*/
|
||||||
public Pair<StoneSet, StoneSet> splitAt(int position) {
|
public Pair<StoneSet, StoneSet> splitAt(int position) {
|
||||||
//Exception falls falscher index
|
// Exception falls falscher index
|
||||||
if (position==0||position==stones.size()){
|
if (position == 0 || position == stones.size()) {
|
||||||
|
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -65,28 +120,29 @@ public class StoneSet implements Iterable<Stone> {
|
||||||
return stones.get(i);
|
return stones.get(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<Stone> iterator() {
|
public Iterator<Stone> iterator() {
|
||||||
final Iterator<Stone> it = stones.iterator();
|
final Iterator<Stone> it = stones.iterator();
|
||||||
|
|
||||||
return new Iterator<Stone>(){
|
return new Iterator<Stone>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
return it.hasNext();
|
return it.hasNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Stone next() {
|
public Stone next() {
|
||||||
return it.next();
|
return it.next();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove() {
|
public void remove() {
|
||||||
// removing stones is impossible
|
// removing stones is impossible
|
||||||
|
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}};
|
}
|
||||||
}
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,6 +60,7 @@ public class StoneSetTest {
|
||||||
assertSet(false, Arrays.asList(new Stone(RED), new Stone(BLACK),
|
assertSet(false, 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(false, Arrays.asList(new Stone(1, RED), new Stone(1, RED),
|
||||||
|
@ -73,7 +74,7 @@ public class StoneSetTest {
|
||||||
assertSet(false, Arrays.asList(new Stone(4, RED), new Stone(6, RED),
|
assertSet(false, 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(false, 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(false, Arrays.asList(new Stone(4, RED), new Stone(RED),
|
||||||
new Stone(5, RED)));
|
new Stone(5, RED)));
|
||||||
}
|
}
|
||||||
|
@ -82,12 +83,15 @@ public class StoneSetTest {
|
||||||
public void otherInvalid() {
|
public void otherInvalid() {
|
||||||
|
|
||||||
assertSet(false, Arrays.asList(new Stone(4, RED), new Stone(5, RED),
|
assertSet(false, 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(false, 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(false, Arrays.asList(new Stone(4, RED), new Stone(5, RED)));
|
||||||
|
assertSet(false, Arrays.asList(new Stone(4, BLUE), new Stone(5, RED),
|
||||||
|
new Stone(6, RED)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// invalid Split
|
// invalid Split
|
||||||
@Test(expected = AssertionError.class)
|
@Test(expected = AssertionError.class)
|
||||||
public void testSplitInvalidLow() {
|
public void testSplitInvalidLow() {
|
||||||
|
|
Reference in a new issue