예제 #1
0
        public string Solve()
        {
            bool         solutionFound  = false;
            string       solutionString = "";
            List <State> visitedStates  = new List <State>();

            StatesProcessedAmount = 0;
            State currentState;

            _priorityQueue.Enqueue(InitialState);

            while (_priorityQueue.Count() > 0)
            {
                currentState = _priorityQueue.Dequeue();
                if (visitedStates.Any())
                {
                    while (visitedStates.Any(p => p.Equals(currentState)))
                    {
                        currentState = _priorityQueue.Dequeue();
                    }
                }

                if (currentState.Depth > MaxDepth)
                {
                    MaxDepth = currentState.Depth;
                }

                //Console.WriteLine(currentState);
                //Console.WriteLine("\n");

                if ((this as ISolver).IsPuzzleSolution(currentState, Solved))
                {
                    solutionString = currentState.MoveSet;
                    solutionFound  = true;
                    break;
                }

                currentState.GenerateNextStates("lurd");
                foreach (State nextState in currentState.NextStates)
                {
                    StatesProcessedAmount++;
                    int heuresticValue = _metric.GetDistanceFromSolution(nextState) + nextState.Depth;
                    _priorityQueue.Enqueue(nextState, heuresticValue);
                }
                visitedStates.Add(currentState);
            }

            StatesVisitedAmount = visitedStates.Count();
            return(solutionFound ? solutionString : "No solution found!");
        }