我想你可以有这样的东西:
public enum Color {
RED(true),
YELLOW(true),
BLUE(true),
ORANGE(false),
PURPLE(false),
GREEN(false),
WHITE(false);
static {
ORANGE.components.add(RED);
ORANGE.components.add(YELLOW);
PURPLE.components.add(RED);
PURPLE.components.add(BLUE);
GREEN.components.add(YELLOW);
GREEN.components.add(BLUE);
WHITE.components.add(RED);
WHITE.components.add(YELLOW);
WHITE.components.add(BLUE);
}
private boolean primary;
private List<Color> components;
Color(boolean primary) {
this.primary = primary;
this.components = new ArrayList<Color>();
}
public boolean isPrimary() {
return primary;
}
public Set<Color> components() {
return Collections.unmodifiableSet(this.components);
}
}
然后你可以有这样的东西:
public class Jewel {
private Color color;
...
}
public class Box {
private Color color;
private Jewel jewel;
...
}
所有的分数计算都可以在某种评分服务中完成:
public class ScoringService {
public int calculate(Box box) {
int score = 0;
Jewel jewel = box.getJewel();
if(box.getColor() == jewel.getColor() || box.getColor() == Color.WHITE) {
score++;
}
if(!box.getColor().isPrimary() && jewel.getColor() == Color.WHITE) {
score++;
}
if(box.getColor().isPrimary() && !jewel.getColor().isPrimary()) {
Set<Color> intersection = new HashSet<Color>(box.getColor().components());
intersection.retainAll(jewel.getColor().components());
score += intersection.size();
}
return score;
}
}