Ejemplo n.º 1
0
        public static bool Solve(string level, ref string solution)
        {
            Level.levelFromString(level);

            SolFunc.initArrays();
            Console.WriteLine("Starting Solve");
            Global.solvable        = true;
            Global.levelSol.length = 0;
            LevelInfo.preprocessLevel();
            DeadlockTable.calculateStaticDeadlocks();
            //Level.printLevel(Global.level);

            SolvingRoutine.trySolveLevel();
            if (Global.solvable)
            {
                if (!SolFunc.checkSolution(Global.levelSol, Global.level))
                {
                    Global.solvable = false;
                    return(false);
                }
                for (int i = 0; i < Global.levelSol.length; i++)
                {
                    solution += Global.levelSol.move[i];
                }
                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
        public static void runRoutine()
        {
            newMove = Allocator.mallocMove();
            for ( ; Global.currentDistance < Global.MAXDISTANCE; Global.currentDistance++)
            {
                while (!Queue.isQueueEmpty(Global.moveQueue[Global.currentDistance]))
                {
                    if (Hashtable.count > Global.HASHMAX)
                    {
                        Global.solvable = false;
                        //Console.WriteLine("Hash Too Full");
                        return;
                    }

                    Queue node = Queue.removeQueueNode(Global.moveQueue[Global.currentDistance]);
                    mov = node.e;

                    Position.setPosition(ref mov.pos);
                    posnum++;

                    /*if (posnum % 50000 == 0)
                     * {
                     *  Level.printLevel(Global.level);
                     * }*/

                    CRS.calculatePosition();

                    //echoAreas();
                    //Level.printLevel(Global.level);
                    //Console.WriteLine("Current Heuristic = " + Global.currentDistance);

                    //Console.WriteLine("mov.heuristic = " + mov.heuristic + " HIBYTES = " + Global.HIBYTES);
                    //Console.WriteLine("Heuristic %: " + mov.heuristic % Global.HIBYTES + " Heuristic /: " + mov.heuristic / Global.HIBYTES);
                    if (mov.heuristic % Global.HIBYTES == mov.heuristic / Global.HIBYTES)
                    {
                        //Level.printLevel(Global.level);
                        //Console.WriteLine("Level Solved with " + mov.heuristic / Global.HIBYTES + " pushes!");

                        SolFunc.createSolution(Global.levelSol, mov);
                        SolFunc.writeSolution(Global.levelSol);

                        return;
                    }

                    for (int b = 0; b < Global.boxCount; b++)
                    {
                        int x    = Global.boxx[b];
                        int y    = Global.boxy[b];
                        int from = Global.levelInfo.fieldNum[y, x];

                        if (Global.levelInfo.tunnel[from] == 0)
                        {
                            for (int i = 0; i < 4; i++)
                            {
                                int xsok = x + Global.movesX[i]; int ysok = y + Global.movesY[i];
                                int xto = x + Global.movesX[i + 2]; int yto = y + Global.movesY[i + 2];
                                if (Global.reachable[ysok, xsok] == 1 && Level.isPushable(Global.level.grid[yto][xto]))
                                {
                                    int to = Global.levelInfo.fieldNum[yto, xto];
                                    addMove(x, y, from, xto, yto, to, 1);
                                }
                            }
                        }
                        else
                        {
                            //Console.WriteLine("Using Tunnel Macro...");
                            for (int i = 0; i < 2; i++)
                            {
                                int type = Global.levelInfo.tunnel[from] = 1;
                                int xsok = x + Global.movesX[i * 2 + type]; int ysok = y + Global.movesY[i * 2 + type];
                                int to = Global.levelInfo.tunnelGoals[from, i];

                                if (to != -1)
                                {
                                    int xto = Level.xPos(Global.levelInfo.fieldPos[to]); int yto = Level.yPos(Global.levelInfo.fieldPos[to]);
                                    if (Global.reachable[ysok, xsok] == 1 && Level.isPushable(Global.level.grid[yto][xto]))
                                    {
                                        int pd = Global.levelInfo.tunnelDists[from, i];
                                        //Console.WriteLine("Tunnel: (" + xsok + ", " + ysok + ") =" + pd + "=> (" + xto + ", " + yto + ")");
                                        addMove(x, y, from, xto, yto, to, pd);
                                    }
                                }
                            }
                        }
                    }
                    //Console.WriteLine("--------------------------------------------------------------\n");
                }
            }
        }