예제 #1
0
        public Solution <T> Search(ISearchable <T> searchable)
        {
            this.closedList = new Solution <T>(new List <Node <T> >());
            this.openList   = new Solution <T>(new List <Node <T> >());
            this.maze       = searchable.GetMaze();
            // Searcher's abstract method overriding
            addToOpenList(maze.GetStartPoint());
            // HashSet<State<T>> closed = new HashSet<State<T>>();

            while (this.openList.GetLength() != 0)
            {
                // inherited from Searcher, removes the best state
                Node <T> bestState = popOpenList();
                this.closedList.AddSolution(bestState);
                if (bestState.Equals(maze.GetEndPoint()))
                {
                    return(backTrace(searchable)); // private method, back traces through the parents
                }
                // calling the delegated method, returns a list of states with n as a parent
                List <Node <T> > succerssors = searchable.getAllPossibleStates(bestState);
                foreach (Node <T> s in succerssors)
                {
                    // If it is not in CLOSED and it is not in OPEN:
                    //evaluate it, add it to OPEN, and record its parent.
                    if (!this.closedList.Containe(s) && !this.openList.Containe(s))
                    {
                        s.SetCost(s.GetCost() + bestState.GetCost());
                        addToOpenList(s);
                        s.SetParent(bestState);
                    }

                    else
                    {
                        //Otherwise, if this new path is better than previous one, change its recorded parent.
                        if ((s.GetCost() + bestState.GetCost()) < this.closedList.GetCost(s))
                        {
                            s.SetParent(bestState);
                            //i.  If it is not in OPEN add it to OPEN.
                            if (!this.openList.Containe(s))
                            {
                                addToOpenList(s);
                            }
                            // ii.Otherwise, adjust its priority in OPEN using this new evaluation.
                            else
                            {
                                this.openList.FindNode(s).SetCost(s.GetCost() + bestState.GetCost());
                            }
                        }
                    }
                }
            }
            return(backTrace(searchable));
        }
예제 #2
0
파일: Game.cs 프로젝트: Silby17/Ex1_Maze
        /// <summary>
        /// Creates a second maze for the Second player
        /// that has joined the game </summary>
        public void CreateSecondMaze()
        {
            Node <int>[,] grid = mazePlayer1.GetGrid();
            Node <int> start = mazePlayer1.GetStartPoint();
            Node <int> end   = mazePlayer1.GetEndPoint();

            _2DMaze <int> twoDMaze = new _2DMaze <int>();

            twoDMaze.height = mazePlayer1.GetHeight();
            twoDMaze.width  = mazePlayer1.GetWidth();
            twoDMaze.CopyGrid(grid);

            //Switches around the starting and ending cell in the Second Maze
            twoDMaze.SetStartingCell(end.GetRow(), end.GetCol());
            twoDMaze.SetEndingCell(start.GetRow(), start.GetCol());
            this.mazePlayer2 = new GeneralMaze <int>(twoDMaze);
            this.mazePlayer2.MakeMazeString();
            this.mazePlayer2.UpdateMembers();
            this.mazePlayer2.Name = this.mazePlayer1.Name + "_2";
        }