Basic FitnessEvaluator tries 10x to generate a level containing between 8 and 16 level components.
This commit is contained in:
14
src/dk/itu/mario/level/FitnessEvaluator.java
Normal file
14
src/dk/itu/mario/level/FitnessEvaluator.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package dk.itu.mario.level;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import dk.itu.mario.level.grammar.LevelParseTree;
|
||||
|
||||
public class FitnessEvaluator {
|
||||
public static boolean isFit(LevelParseTree parseTree, PlayerProfile playerProfile, LevelArchetype levelArchetype) {
|
||||
System.out.println("Evaluating LevelParseTree for fitness");
|
||||
List<LevelComponent> levelTemplate = parseTree.getLevelTemplate();
|
||||
//a good level has 8-16 components, plus some additional complexity depending on the player's skill level
|
||||
return levelTemplate.size() > 7 && levelTemplate.size() < 17;
|
||||
}
|
||||
}
|
||||
@@ -82,6 +82,9 @@ public class PCGLevel extends Level {
|
||||
String grammarFileName = "grammars/overland.grm";
|
||||
grammar = LevelGrammarFactory.createGrammar(new File(grammarFileName));
|
||||
System.out.println("Read grammar from file: " + grammarFileName);
|
||||
System.out.println("==== LEVEL GRAMMAR ====");
|
||||
System.out.println(grammar);
|
||||
System.out.println("=======================");
|
||||
} catch (Exception ex) {
|
||||
System.out.println("Failed to parse grammar file due to exception: " + ex.getMessage());
|
||||
System.out.println("Defaulting to basic overland grammar.");
|
||||
@@ -185,6 +188,19 @@ public class PCGLevel extends Level {
|
||||
else {
|
||||
System.out.println("Generating level for component list: ");
|
||||
LevelParseTree parseTree = grammar.generateRandomTree(seed, width);
|
||||
|
||||
int MAX_REGENS = 10;
|
||||
int nRegens = 0;
|
||||
while (!FitnessEvaluator.isFit(parseTree,profile,archetype) && nRegens < MAX_REGENS) {
|
||||
System.out.println("Generated level is NOT fit. Regenerating...");
|
||||
parseTree = grammar.generateRandomTree(seed, width);
|
||||
nRegens++;
|
||||
}
|
||||
|
||||
if (nRegens == MAX_REGENS) {
|
||||
System.out.println("Failed to generate a fit level after " + nRegens + " attempts. Proceeding with unfit level.");
|
||||
}
|
||||
|
||||
List<LevelComponent> levelTemplate = parseTree.getLevelTemplate();
|
||||
|
||||
for (LevelComponent lcomp : levelTemplate) {
|
||||
|
||||
@@ -24,7 +24,7 @@ public class LevelGrammarFactory {
|
||||
if (line.length() == 0 || line.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
System.out.println("Read: " + line);
|
||||
//System.out.println("Read: " + line);
|
||||
//split on whitespace
|
||||
String[] fields;
|
||||
if (line.startsWith("VAR")) {
|
||||
@@ -37,7 +37,7 @@ public class LevelGrammarFactory {
|
||||
} else if (line.startsWith("RULE")) {
|
||||
fields = line.split("->");
|
||||
String ruleName = fields[0].split("\\s")[1];
|
||||
System.out.println("Rule name: " + ruleName);
|
||||
//System.out.println("Rule name: " + ruleName);
|
||||
Variable lhs = levelGrammar.getVariable(ruleName);
|
||||
if (lhs == null) {
|
||||
throw new RuntimeException("LHS variable not found: " + ruleName);
|
||||
@@ -74,7 +74,7 @@ public class LevelGrammarFactory {
|
||||
if (lBraceIndex != -1) {
|
||||
isOrClause = true;
|
||||
rBraceIndex = clause.indexOf("}");
|
||||
System.out.println("Read OR-clause probabilities from: " + lBraceIndex + " to " + rBraceIndex);
|
||||
//System.out.println("Read OR-clause probabilities from: " + lBraceIndex + " to " + rBraceIndex);
|
||||
String[] doubleFields = clause.substring(lBraceIndex+1,rBraceIndex).split(",");
|
||||
chances = new double[doubleFields.length];
|
||||
for (int i = 0; i < doubleFields.length; i++) {
|
||||
|
||||
Reference in New Issue
Block a user