Ejemplo n.º 1
0
        private void solvePuzzelHere(int[] inputs)
        {
            Node start = new Node();
            Node goal  = new Node();


            start.depth = 0;
            goal.depth  = 0;

            start.parent = null;

            //start.tiles = new int[9] { 8, 1, 3, 7, 2, 4, 0, 6, 5 };
            start.tiles = inputs;
            goal.tiles  = new int[9] {
                1, 2, 3, 8, 0, 4, 7, 6, 5
            };

            start.f = start.depth + Huristic.ManhattanDistance(start.tiles, goal.tiles);
            //start.f = start.depth + Huristic.DistanceN(start.tiles, goal.tiles);
            AStarSolver star = new AStarSolver(start, goal);

            output    = star.solvePuzzel();
            count     = output.Count - 1;
            this.play = true;
        }
Ejemplo n.º 2
0
        public List <Node> solvePuzzel()
        {
            openlist.Add(this.start);

            int d = 0;

            tree.Add(new KeyValuePair <int, Node>(d, this.start));

            Node solved = null;

            while (openlist.Count > 0)
            {
                Node current = getMinimumF(openlist);
                if (areArraysEqual(current.tiles, goal.tiles))
                {
                    //Console.WriteLine(current.tiles[0] + " " + current.tiles[1] + " " + current.tiles[2] + " " + current.tiles[3] + " " + current.tiles[4] + " " +
                    //current.tiles[5] + " " + current.tiles[6] + " " + current.tiles[7] + " " + current.tiles[8]);
                    //Console.WriteLine("done");
                    solved = current;
                    break;
                }
                d++;
                List <Node> successors = getAllSuccesors(current);

                foreach (Node item in successors)
                {
                    //tree.Add(new KeyValuePair<int, Node>(d, item));
                    int depth_temp = current.depth + 1;

                    double f_temp = depth_temp + Huristic.ManhattanDistance(item.tiles, this.goal.tiles);
                    //double f_temp = depth_temp + Huristic.DistanceN(item.tiles, this.goal.tiles);
                    if (openlist.Contains(item) && f_temp < item.f)
                    {
                        Console.WriteLine("con1");
                        continue;
                    }

                    if (closelist.Contains(item) && f_temp < item.f)
                    {
                        Console.WriteLine("con2");
                        continue;
                    }

                    openlist.Remove(current);
                    closelist.Remove(current);
                    item.parent = current;
                    item.f      = f_temp;
                    if (!nodeExists(openlist, item))
                    {
                        openlist.Add(item);
                    }
                }
                closelist.Add(current);
                //Console.WriteLine(current.tiles[0]+" "+current.tiles[1]+" "+current.tiles[2]+" "+current.tiles[3]+" "+current.tiles[4]+" "+
                //    current.tiles[5]+" "+current.tiles[6]+" "+current.tiles[7]+" "+current.tiles[8]);
            }

            while (solved != null)
            {
                output.Add(solved);
                solved = solved.parent;
            }

            //Thread.Sleep(10000);
            return(output);
        }