private bool GenerateLevel() { List <List <char> > level = new List <List <char> >(); if (blackBoard.ConfigUI.Config.UsingSimplifiedNGram) { ICompiledGram compiledGram = simpleGrammar.Compile(); int levelIndex = levelColumns.RandomIndex(); List <string> simpleInput = simplifiedLevelColumns[levelIndex].GetRange (0, blackBoard.ConfigUI.Config.N + 7); blackBoard.LevelColumns = levelColumns[levelIndex].GetRange( 0, blackBoard.ConfigUI.Config.N + 7); blackBoard.SimpleLevelColumns = NGramGenerator.Generate( compiledGram, simpleInput, blackBoard.ConfigUI.Config.LevelSize); compiledGram = grammar.Compile(); blackBoard.LevelColumns = NGramGenerator.GenerateRestricted( compiledGram, blackBoard.LevelColumns, blackBoard.SimpleLevelColumns, (inColumn) => { return(LevelParser.ClassifyColumn( inColumn, blackBoard.ConfigUI.Config.Game)); }); } else { ICompiledGram compiledGram = grammar.Compile(); blackBoard.LevelColumns = NGramGenerator.Generate( compiledGram, levelColumns.RandomValue().GetRange(0, blackBoard.ConfigUI.Config.N + 7), blackBoard.ConfigUI.Config.LevelSize); } bool generationWorked = blackBoard.LevelColumns != null; if (generationWorked) { foreach (string column in blackBoard.LevelColumns) { level.Add(new List <char>(column)); } // add ending column to the level char flagChar = Tile.playerOneFinish.ToChar(); List <char> endingColumn = new List <char>(); for (int i = 0; i < level[0].Count; ++i) { endingColumn.Add(flagChar); } level.Add(endingColumn); blackBoard.LevelInfo = LevelLoader.Build(level, blackBoard.Tilemap, blackBoard.CameraFollow); } return(generationWorked); }
public void Execute() { string keyDirectory = Path.Combine(basePath, $"{extension}_{gram.GetN()}"); if (Directory.Exists(keyDirectory) == false) { Directory.CreateDirectory(keyDirectory); } StreamWriter writer = File.CreateText($"{keyDirectory}.txt"); writer.WriteLine("Sequence_Probability,Perplexity,Linearity_JSON_Positions,Leniency"); ICompiledGram compiled = gram.Compile(); ICompiledGram simpleCompiled = simplifiedGram?.Compile(); for (int i = 0; i < numSimulations; ++i) { UtilityRandom.SetSeed(new DateTime().Millisecond); Tuple <List <string>, List <string> > tuple; if (gram as NGram == null) { tuple = GetColumnsBestGuess(compiled, simpleCompiled); } else { tuple = GetColumnsSemiGuaranteed(compiled, simpleCompiled); } List <string> columns = tuple.Item1; List <string> simplified = tuple.Item2; string[] columnsArray = columns.ToArray(); List <int> positions = LevelAnalyzer.Positions(columnsArray); JsonArray jsonPositions = new JsonArray(); foreach (int pos in positions) { jsonPositions.Add(pos); } double sequenceProbability = compiled.SequenceProbability(columnsArray); writer.Write($"{sequenceProbability},"); if (sequenceProbability == 0) { writer.Write($"0,"); } else { writer.Write($"{1d/sequenceProbability},"); } writer.Write($"{jsonPositions},"); writer.Write($"{LevelAnalyzer.Leniency(simplified.ToArray())}\n"); StreamWriter levelWriter = File.CreateText(Path.Combine(keyDirectory, $"{i}.txt")); levelWriter.Write(string.Join("\n", columnsArray)); levelWriter.Flush(); levelWriter.Close(); if (i % 200 == 0) { writer.Flush(); } } writer.Flush(); writer.Close(); }