public static GoalMacroTree BuildMacroTree(AbstractSokobanState state) { GoalMacroWrapper.state = state; File.WriteAllText("level.tmp", state.State.ToString()); string s = GetMacros("level.tmp"); GoalMacroTree tree = JsonConvert.DeserializeObject <GoalMacroTree>(s); //GoalMacroNode node = null; //for (int j=0;j< tree.Roots.Length;j++) //{ // node = tree.Roots[j]; // while (node.Entries.Length > 0) // { // s = ""; // for (int i = 0; i < node.StonesPosition.Length; i++) // { // if (i % 16 == 0 && i != 0) // { // s += "\n"; // } // if (i == node.Entries[0].GoalPosition) // s += "x"; // else if (i == node.Entries[0].EntrancePosition) // s += "-"; // else // s += node.StonesPosition[i]; // } // Debug.WriteLine(s); // Debug.WriteLine("GoalPosition: "+node.Entries[0].GetGoalPosition()); // Debug.WriteLine("EntrancePosition: " + node.Entries[0].GetEntrancePosition()); // List<Position> boxes = node.GetBoxPositions(); // foreach (Position box in boxes) // Debug.WriteLine("Box in "+box); // node = node.Entries[0].Next; // Debug.WriteLine("\n\n"); // } //} HashSet <Position> goalsInRoom = new HashSet <Position>(); foreach (GoalMacroNode n in tree.Roots) { CompressTree(n, new Dictionary <int, GoalMacroNode>(), goalsInRoom); } tree.GoalsInRoom = goalsInRoom; return(tree); }
private void Init(SokobanGameState state, RewardType rewardType, bool useNormalizedPosition, bool useGoalMacro, bool useTunnelMacro, bool useGoalCut, ISPSimulationStrategy simulationStrategy, MersenneTwister rng = null) { this.useNormalizedPosition = useNormalizedPosition; this.state = (SokobanGameState)state; if (simulationStrategy == null) { simulationStrategy = new SokobanRandomStrategy(); } if (rng == null) { rng = new MersenneTwister(); } this.rng = rng; this.rewardType = rewardType; this.simulationStrategy = simulationStrategy; normalizedPlayerPosition = new Position(state.PlayerX, state.PlayerY); availableMoves = null; this.useGoalMacro = useGoalMacro; this.useTunnelMacro = useTunnelMacro; this.useGoalCut = useGoalCut; if (useGoalMacro) { goalMacroTree = GoalMacroWrapper.BuildMacroTree(this); if (goalMacroTree.Roots.Length > 0) { currentGoalMacroNode = GetInitialMacroNode(goalMacroTree.Roots[0], state.GetGoalMacroHash(goalMacroTree.GoalsInRoom)); if (currentGoalMacroNode == null) { this.useGoalMacro = false; } } else { this.useGoalMacro = false; } } }