Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        public static GoalMacro GenerateGoalMacro(Position playerPosition, Position goal, Position entrance, List <Position> boxesInGoal, SokobanGameState state)
        {
            SokobanGameState clone = (SokobanGameState)state.Clone();

            clone.ClearBoardForGoalMacro(boxesInGoal, goal, entrance);

            if (clone.Board[playerPosition.X, playerPosition.Y] == SokobanGameState.EMPTY || clone.Board[playerPosition.X, playerPosition.Y] == SokobanGameState.GOAL)
            {
                clone.SetPlayerPosition(playerPosition);
                AbstractSokobanState clearState = new AbstractSokobanState(clone.ToString(), clone.RewardType, false, false, false, false, clone.SimulationStrategy, null);
                SokobanPushMove      pushMove   = SolveMacro(clearState);
                if (pushMove != null)
                {
                    return(new GoalMacro(playerPosition, pushMove));
                }
            }
            return(null);
        }
Esempio n. 3
0
        static SokobanPushMove SolveMacro(AbstractSokobanState s)
        {
            s.UseGoalCut     = false;
            s.UseGoalMacro   = false;
            s.UseTunnelMacro = false;
            List <IPuzzleMove> solution = idaStar.Solve(s, 10000, 10000, 700);

            if (solution.Count == 0)
            {
                return(null);
            }
            List <SokobanGameMove> moveList = new List <SokobanGameMove>();

            foreach (SokobanPushMove push in solution)
            {
                moveList.AddRange(push.MoveList);
            }
            return(new SokobanPushMove(moveList, new Position(s.State.PlayerX, s.State.PlayerY), moveList[solution.Count - 1].BoxIndex));
        }