This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
JRummikub/src/jrummikub/model/StoneSet.java

149 lines
3 KiB
Java
Raw Normal View History

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;
import jrummikub.util.Pair;
/** Class managing {@link Stone}s joined together to form sets */
public class StoneSet implements Iterable<Stone> {
private List<Stone> stones;
public StoneSet(Stone stone) {
stones = Collections.singletonList(stone);
}
public StoneSet(List<Stone> stones) {
this.stones = new ArrayList<Stone>(stones);
}
/** Test for rule conflict within the StoneSet */
public boolean isValid() {
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;
}
}
/**
* Splits the StoneSet at a specified {@link Position} and returns two new
* Stone Sets
*
* @param position
* Splitting {@link Position}
*/
public Pair<StoneSet, StoneSet> splitAt(int position) {
// Exception falls falscher index
if (position == 0 || position == stones.size()) {
} else {
}
return null;
}
/**
* Joins StoneSet to another StoneSet and returns the resulting new StoneSet
*
* @param other
* StoneSet to be joined to active StoneSet
*/
public StoneSet join(StoneSet other) {
return null;
}
public int size() {
return stones.size();
}
public Stone get(int i) {
return stones.get(i);
}
@Override
public Iterator<Stone> iterator() {
final Iterator<Stone> it = stones.iterator();
return new Iterator<Stone>() {
@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();
}
};
}
}