Esempio n. 1
0
 public virtual bool CanMove(int x, int y, int dx, int dy, LevelState state)
 {
     return(state.IsInBounds(x + dx, y + dy));
 }
Esempio n. 2
0
 public int Score(LevelState state)
 {
     Solver.Solution solution = Solver.FindPath(state);
     return(solution.Solved ? solution.Explored : 0);
 }
Esempio n. 3
0
        public static Solution FindPath(LevelState start, int maxExplored = 4000)
        {
            Solution             solution = new Solution();
            List <Node>          OpenList = new List <Node>();
            HashSet <LevelState> Seen     = new HashSet <LevelState>();
            Node current = new Node(null, start);

            // add start LevelState to Open List
            OpenList.Add(current);
            Seen.Add(current.State);

            bool done = false;

            while (OpenList.Count != 0)
            {
                current = OpenList[0];
                OpenList.Remove(current);
                IEnumerable <LevelState> adjacencies = current.State.GetPossibleNextStates();

                //Console.WriteLine(current.Cost);
                //Console.WriteLine(current.DistanceToTarget);
                //Console.WriteLine(current.State);
                //Console.WriteLine("-------------");

                foreach (LevelState state in adjacencies)
                {
                    solution.Explored++;
                    if (!Seen.Contains(state))
                    {
                        Node n = new Node(current, state);
                        Seen.Add(state);
                        OpenList.Insert(0, n);
                        if (n.EstimatedDistance == 0)
                        {
                            done = true;
                            break;
                        }
                    }
                }
                if (done || solution.Explored >= maxExplored)
                {
                    break;
                }
                OpenList.Sort();
            }

            solution.Seen = Seen.Count;

            // construct path, if end was not closed return null
            if (!done)
            {
                return(solution);
            }

            solution.Path = new List <LevelState>();

            // if all good, return path
            Node temp = OpenList[0];

            while (temp != null)
            {
                solution.MaxUnderestimation = Math.Max(solution.MaxUnderestimation,
                                                       temp.EstimatedDistance - solution.Path.Count);
                solution.Path.Insert(0, temp.State);
                temp = temp.Parent;
            }


            return(solution);
        }