Repackaged grammar and (profile/archetype) matchers.
This commit is contained in:
10
src/dk/itu/mario/level/grammar/GrammarTuner.java
Normal file
10
src/dk/itu/mario/level/grammar/GrammarTuner.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package dk.itu.mario.level.grammar;
|
||||
|
||||
import dk.itu.mario.level.matcher.LevelArchetype;
|
||||
import dk.itu.mario.level.matcher.PlayerProfile;
|
||||
|
||||
public class GrammarTuner {
|
||||
public static LevelGrammar tune(LevelGrammar grammar, PlayerProfile profile, LevelArchetype archetype) {
|
||||
return grammar;
|
||||
}
|
||||
}
|
||||
59
src/dk/itu/mario/level/grammar/LevelGrammar.java
Normal file
59
src/dk/itu/mario/level/grammar/LevelGrammar.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package dk.itu.mario.level.grammar;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
|
||||
public class LevelGrammar {
|
||||
private Variable start;
|
||||
private Map<Variable, ProductionRule> ruleMap = new HashMap<Variable, ProductionRule>();
|
||||
private Set<Variable> variables = new TreeSet<Variable>();
|
||||
|
||||
public void addVariable(Variable var) {
|
||||
if (variables.contains(var)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Grammar already contains variable: " + var);
|
||||
}
|
||||
variables.add(var);
|
||||
}
|
||||
|
||||
public void addProductionRule(ProductionRule rule) {
|
||||
if (ruleMap.containsKey(rule.getLHS())) {
|
||||
throw new IllegalArgumentException(
|
||||
"Grammar already contains rule with LHS: " + rule.getLHS());
|
||||
}
|
||||
ruleMap.put(rule.getLHS(), rule);
|
||||
}
|
||||
|
||||
public String generateRandom(long randomSeed) {
|
||||
System.out.println("Generating random level parameters using seed: "
|
||||
+ randomSeed);
|
||||
List<Variable> startRuleRHS = getRule(getStart()).getRHS();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Variable var : startRuleRHS) {
|
||||
sb.append(var.toString());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String generateRandom() {
|
||||
System.out.println("Creating new random seed for LevelGrammar");
|
||||
return generateRandom(new Random().nextLong());
|
||||
}
|
||||
|
||||
public ProductionRule getRule(Variable var) {
|
||||
return ruleMap.get(var);
|
||||
}
|
||||
|
||||
public Variable getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public void setStart(Variable start) {
|
||||
this.start = start;
|
||||
}
|
||||
}
|
||||
24
src/dk/itu/mario/level/grammar/LevelGrammarFactory.java
Normal file
24
src/dk/itu/mario/level/grammar/LevelGrammarFactory.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package dk.itu.mario.level.grammar;
|
||||
|
||||
import dk.itu.mario.level.matcher.LevelArchetype;
|
||||
import dk.itu.mario.level.matcher.PlayerProfile;
|
||||
|
||||
|
||||
public class LevelGrammarFactory {
|
||||
public static LevelGrammar createGrammar(PlayerProfile playerProfile, LevelArchetype archetype) {
|
||||
LevelGrammar grammar = new LevelGrammar();
|
||||
|
||||
Variable v_S = new Variable("S");
|
||||
Variable v_level_start = new Variable("level_start");
|
||||
Variable v_level_end = new Variable("level_end");
|
||||
|
||||
grammar.addVariable(v_S);
|
||||
grammar.addVariable(v_level_start);
|
||||
grammar.addVariable(v_level_end);
|
||||
|
||||
grammar.addProductionRule(new ProductionRule(v_S,v_level_start,v_level_end));
|
||||
grammar.setStart(v_S);
|
||||
|
||||
return grammar;
|
||||
}
|
||||
}
|
||||
26
src/dk/itu/mario/level/grammar/ProductionRule.java
Normal file
26
src/dk/itu/mario/level/grammar/ProductionRule.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package dk.itu.mario.level.grammar;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class ProductionRule {
|
||||
private Variable lhs;
|
||||
private List<Variable> rhs;
|
||||
|
||||
public ProductionRule(Variable lhs, Variable... rhs) {
|
||||
this.lhs = lhs;
|
||||
this.rhs = new ArrayList<Variable>();
|
||||
for (Variable var : rhs) {
|
||||
this.rhs.add(var);
|
||||
}
|
||||
}
|
||||
|
||||
public Variable getLHS() {
|
||||
return lhs;
|
||||
}
|
||||
|
||||
public List<Variable> getRHS() {
|
||||
return rhs;
|
||||
}
|
||||
}
|
||||
27
src/dk/itu/mario/level/grammar/Variable.java
Normal file
27
src/dk/itu/mario/level/grammar/Variable.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package dk.itu.mario.level.grammar;
|
||||
|
||||
public class Variable implements Comparable<Variable> {
|
||||
private boolean terminal;
|
||||
private String value;
|
||||
|
||||
public Variable(String value) {
|
||||
this.value = value;
|
||||
this.terminal = (value.toLowerCase().equals(value));
|
||||
}
|
||||
|
||||
public boolean isTerminal() {
|
||||
return terminal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Variable o) {
|
||||
return this.value.compareTo(o.value);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user