59 lines
1.2 KiB
Java
59 lines
1.2 KiB
Java
package dk.itu.mario.level.grammar;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class OrClause implements Clause {
|
|
private double[] chances;
|
|
private List<Clause> subClauses = new ArrayList<Clause>();
|
|
|
|
public OrClause(double[] chances, Clause... subClauses) {
|
|
this.chances = chances;
|
|
double chanceTotal = 0.0;
|
|
for (double chance : chances) {
|
|
chanceTotal += chance;
|
|
}
|
|
if (Math.abs(chanceTotal - 1.0) > 0.01) {
|
|
throw new IllegalArgumentException(
|
|
"Total or-clause chances must sum to 1.0.");
|
|
}
|
|
for (Clause clause : subClauses) {
|
|
this.subClauses.add(clause);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean isChoice() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean isTerminal() {
|
|
return false;
|
|
}
|
|
@Override
|
|
public Clause makeRandomChoice() {
|
|
double rand = Math.random();
|
|
for (int i = 0; i < chances.length; i++) {
|
|
if (rand < chances[i]) {
|
|
return subClauses.get(i);
|
|
}
|
|
rand -= chances[i];
|
|
}
|
|
return subClauses.get(chances.length - 1);
|
|
}
|
|
|
|
@Override
|
|
public int getNumSubClauses() {
|
|
return subClauses.size();
|
|
}
|
|
|
|
public Clause getSubClause(int index) {
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
|
|
@Override
|
|
public boolean isVariable() {
|
|
return false;
|
|
}
|
|
} |