Beispiel #1
0
 public bool checkDFSNodeInStack(DFSNode node)
 {
     foreach (DFSNode item in this.DFSNodes)
     {
         if (item.Equals(node))
         {
             Console.WriteLine("Node is in stack already u f*****g numb");
             return(true);
         }
     }
     return(false);
 }
Beispiel #2
0
 public override bool Equals(Object other)
 {
     if (other == null || !this.GetType().Equals(other.GetType()))
     {
         return(false);
     }
     else
     {
         DFSNode n = (DFSNode)other;
         return((n.X == this.X) && (n.Y == this.Y));
     }
 }
Beispiel #3
0
        public void getDFSChildren(DFSNode node)
        {
            int x = node.X;
            int y = node.Y;

            if (!checkRightSide())
            {
                DFSNode rightChild = new DFSNode(x + 1, y);
                rightChild.ParentNode = node;
                if (!checkDFSNodeInStack(rightChild))
                {
                    node.RightChild = rightChild;
                }
            }

            if (!checkTopSide())
            {
                DFSNode topChild = new DFSNode(x, y + 1);
                topChild.ParentNode = node;
                if (!checkDFSNodeInStack(topChild))
                {
                    node.TopChild = topChild;
                }
            }

            if (!checkLeftSide())
            {
                DFSNode leftChild = new DFSNode(x - 1, y);
                leftChild.ParentNode = node;
                if (!checkDFSNodeInStack(leftChild))
                {
                    node.LeftChild = leftChild;
                }
            }

            if (!checkBottomSide())
            {
                DFSNode bottomChild = new DFSNode(x, y - 1);
                bottomChild.ParentNode = node;
                if (!checkDFSNodeInStack(bottomChild))
                {
                    node.BottomChild = bottomChild;
                }
            }
        }
Beispiel #4
0
        public void exploreWithDFS()
        {
            this.stack = new Stack();
            DFSNode currentNode = StartDFSNode;

            this.DFSNodes.Add(currentNode);
            try
            {
                while (!this._shouldStop)
                {
                    while (stack.Count > 0 || currentNode != null)
                    {
                        if (CurrentCoverage >= UserSetting.CoverageLimit && UserSetting.CoverageLimit != 100)
                        {
                            Console.WriteLine("Coverage limit reached!");
                            OnSendingMessage("Coverage limit reached!!!");
                            this._shouldStop = true;
                            break;
                        }
                        if (currentNode != null)
                        {
                            Console.WriteLine("Visiting node X = {0}, Y = {1}", currentNode.X, currentNode.Y);
                            currentNode.isVisited = true;
                            this.X = currentNode.X;
                            this.Y = currentNode.Y;
                            computeCoverage();
                            if (currentNode.Equals(this.GoalDFSNode))
                            {
                                Console.WriteLine("Reach goal!");
                                OnSendingMessage("Reached goal zone!!!");
                                //OnSendingMessage("X = " + GoalDFSNode.X);
                                //OnSendingMessage("Y = " + GoalDFSNode.Y);
                            }
                            getDFSChildren(currentNode);
                            if (currentNode.BottomChild != null)
                            {
                                stack.Push(currentNode.BottomChild);
                                this.DFSNodes.Add(currentNode.BottomChild);
                            }
                            if (currentNode.LeftChild != null)
                            {
                                stack.Push(currentNode.LeftChild);
                                this.DFSNodes.Add(currentNode.LeftChild);
                            }
                            if (currentNode.TopChild != null)
                            {
                                stack.Push(currentNode.TopChild);
                                this.DFSNodes.Add(currentNode.TopChild);
                            }
                            if (currentNode.RightChild != null)
                            {
                                stack.Push(currentNode.RightChild);
                                this.DFSNodes.Add(currentNode.RightChild);
                            }
                            while (currentNode.allChildrenVisited())
                            {
                                Console.WriteLine("No more children, backtracking!!!");
                                //no more children, backtracking
                                currentNode = currentNode.ParentNode;
                                Thread.Sleep(1000 / UserSetting.Speed);
                                this.X = currentNode.X;
                                this.Y = currentNode.Y;
                                if (currentNode == StartDFSNode)
                                {
                                    break;
                                }
                            }
                            //printStack();
                            if (stack.Count > 0)
                            {
                                currentNode = (DFSNode)stack.Pop();
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            if (stack.Count > 0)
                            {
                                currentNode = (DFSNode)stack.Pop();
                            }
                            else
                            {
                                break;
                            }
                        }
                        Thread.Sleep(1000 / UserSetting.Speed);
                    }
                    this.isExplored = true;
                    Console.WriteLine("Exploration finished!!!");
                    OnSendingMessage("Exploration finished!!!");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }