Esempio n. 1
0
    public Stack <Vector3> GetVectorPath(Vector3 i_strt, Vector3 i_end)
    {
        if (IsGrathConacted() == false)
        {
            Debug.LogError("grath is not conacted");
        }
        Stack <Vector3> moveQ  = null;
        RoomNode        startR = convertV3toRoomNode(i_strt);
        RoomNode        endR   = convertV3toRoomNode(i_end);


        if (startR != null && endR != null && isRoomReachbol(endR) == true)
        {
            if (startR != endR)
            {
                BfsNode bfsNode = findSortPath(startR, endR);
                moveQ = getV3PathFromBfsNode(i_end, bfsNode);
            }
            else
            {
                moveQ = new Stack <Vector3>();
                moveQ.Push(i_end);
            }
        }
        else
        {
            moveQ = new Stack <Vector3>();
        }

        return(moveQ);
    }
Esempio n. 2
0
    private List <BfsNode> getGoodNaeibersForBfs(BfsNode i_node, List <Vector2> i_arcivs)
    {
        List <BfsNode> nodss = new List <BfsNode>();
        int            x     = (int)i_node.mySpot.x;
        int            y     = (int)i_node.mySpot.y;


        if (x + 1 < c_x && i_arcivs.Contains(m_rooms[y, x + 1].myPosition) != true)
        {
            i_arcivs.Add(m_rooms[y, x + 1].myPosition);
            nodss.Add(new BfsNode(m_rooms[y, x + 1].myPosition, i_node, i_node.depth + 1));
        }

        if (x != 0 && i_arcivs.Contains(m_rooms[y, x - 1].myPosition) != true)
        {
            i_arcivs.Add(m_rooms[y, x - 1].myPosition);
            nodss.Add(new BfsNode(m_rooms[y, x - 1].myPosition, i_node, i_node.depth + 1));
        }

        if (y + 1 < c_y && i_arcivs.Contains(m_rooms[y + 1, x].myPosition) != true)
        {
            i_arcivs.Add(m_rooms[y + 1, x].myPosition);
            nodss.Add(new BfsNode(m_rooms[y + 1, x].myPosition, i_node, i_node.depth + 1));
        }

        if (y != 0 && i_arcivs.Contains(m_rooms[y - 1, x].myPosition) != true)
        {
            i_arcivs.Add(m_rooms[y - 1, x].myPosition);
            nodss.Add(new BfsNode(m_rooms[y - 1, x].myPosition, i_node, i_node.depth + 1));
        }


        return(nodss);
    }
Esempio n. 3
0
    private void ConactMe(Vector2 S, Vector2 i_badRoom)
    {
        Queue <BfsNode> queue  = new Queue <BfsNode>();
        List <Vector2>  arcivs = new List <Vector2>();

        arcivs.Add(S); arcivs.Add(i_badRoom);
        queue.Enqueue(new BfsNode(S, null, 0));
        BfsNode solver = null;

        while (solver == null)
        {
            BfsNode node = queue.Dequeue();

            List <BfsNode> naeibers = getGoodNaeibersForBfs(node, arcivs);
            foreach (BfsNode naeiber in naeibers)
            {
                if (m_rooms[(int)naeiber.mySpot.y, (int)naeiber.mySpot.x].m_niebringRooms != null && m_rooms[(int)naeiber.mySpot.y, (int)naeiber.mySpot.x].m_activRoom == true && m_rooms[(int)naeiber.mySpot.y, (int)naeiber.mySpot.x].m_eColor == eColor.Black)
                {
                    solver = naeiber;
                    break;
                }
                else
                {
                    queue.Enqueue(naeiber);
                }
            }
        }
        ReconctingGrath(solver);
    }
Esempio n. 4
0
 private static void AddNodeToQueue(
     BfsNode[,] grid,
     Queue <BfsNode> queue,
     BfsNode node,
     int nodeRow,
     int nodeCol)
 {
     queue.Enqueue(new BfsNode(nodeRow, nodeCol, node, node.Distance + 1));
     grid[nodeRow, nodeCol].IsVisited = true;
 }
Esempio n. 5
0
 private IEnumerable <BfsNode> NextStates(BfsNode node)
 {
     return(node.Parser.AcceptableStructuralChars().Select(c =>
     {
         var newJson = new StructuralChar[node.Json.Length + 1];
         node.Json.CopyTo(newJson, 0);
         newJson[node.Json.Length] = c;
         return new BfsNode
         {
             Json = newJson,
             Parser = node.Parser.Read(c)
         };
     }));
 }
Esempio n. 6
0
    private Stack <Vector3> getV3PathFromBfsNode(Vector3 i_Target, BfsNode i_bfsNode)
    {
        Stack <Vector3> stack = new Stack <Vector3>();

        if (i_bfsNode != null)
        {
            BfsNode bfsNode = i_bfsNode;

            stack.Push(i_Target);

            while (bfsNode != null)
            {
                stack.Push(m_rooms[(int)bfsNode.mySpot.y, (int)bfsNode.mySpot.x].refRoom.transform.position);
                bfsNode = bfsNode.perent;
            }
        }

        return(stack);
    }
Esempio n. 7
0
    private void ReconctingGrath(BfsNode i_goodStart)
    {
        BfsNode  son = i_goodStart;
        BfsNode  dad = son.perent;
        RoomNode n1 = m_rooms[(int)son.mySpot.y, (int)son.mySpot.x], n2 = m_rooms[(int)dad.mySpot.y, (int)dad.mySpot.x];

        while (n2.m_activRoom == false)
        {
            n2.ActivatRoom(new List <RoomNode> {
                n1
            });
            son = dad;
            dad = dad.perent;
        }
        n2.m_niebringRooms.Add(n1);
        n1.m_niebringRooms.Add(n2);

        n2.refRoom.FindTheDoorBetwin2RoomsAndActivate(n1.refRoom);
    }
Esempio n. 8
0
        /// <summary>
        ///     Traverses the graph using the breadth-first-search starting from <paramref name="startVertex" />.
        /// </summary>
        /// <param name="startVertex">The start vertex.</param>
        /// <param name="handleVertexCompleted">The function called when a vertex is completed.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="startVertex" /> or
        ///     <paramref name="handleVertexCompleted" /> is <c>null</c>.
        /// </exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="startVertex" /> is not part of this graph.</exception>
        public virtual void TraverseBfs([NotNull] TVertex startVertex,
                                        [NotNull] Predicate <IBfsNode> handleVertexCompleted)
        {
            Validate.ArgumentNotNull(nameof(startVertex), startVertex);
            Validate.ArgumentNotNull(nameof(handleVertexCompleted), handleVertexCompleted);

            var inspectQueue  = new Queue <BfsNode>();
            var discoveredSet = new HashSet <TVertex>();
            var first         = new BfsNode(startVertex);

            inspectQueue.Enqueue(first);
            discoveredSet.Add(first.Vertex);

            while (inspectQueue.Count > 0)
            {
                var vertexNode = inspectQueue.Dequeue();
                Assert.NotNull(vertexNode);
                Assert.Condition(discoveredSet.Contains(vertexNode.Vertex));

                foreach (var edge in GetEdges(vertexNode.Vertex))
                {
                    if (discoveredSet.Contains(edge.ToVertex))
                    {
                        continue;
                    }

                    var connectedNode = new BfsNode(edge)
                    {
                        Parent = vertexNode
                    };

                    discoveredSet.Add(edge.ToVertex);
                    inspectQueue.Enqueue(connectedNode);
                }

                if (!handleVertexCompleted(vertexNode))
                {
                    break;
                }
            }
        }
Esempio n. 9
0
    private BfsNode findSortPath(RoomNode i_start, RoomNode i_end)
    {
        Queue <BfsNode> queue  = new Queue <BfsNode>();
        List <Vector2>  arcivs = new List <Vector2>();

        queue.Enqueue(new BfsNode(i_start.myPosition, null, 0));
        BfsNode solver = null;

        while (solver == null && queue.Count != 0)
        {
            BfsNode node = queue.Dequeue();
            if (m_rooms[(int)node.mySpot.y, (int)node.mySpot.x].m_niebringRooms == null)
            {
                continue;
            }
            foreach (RoomNode room in m_rooms[(int)node.mySpot.y, (int)node.mySpot.x].m_niebringRooms)
            {
                if (isRoomVisabol(room) != true)
                {
                    continue;
                }
                BfsNode bfsNode = new BfsNode(room.myPosition, node, node.depth + 1);
                if (room == i_end)
                {
                    solver = bfsNode;
                    break;
                }
                if (arcivs.Contains(bfsNode.mySpot) == false)
                {
                    arcivs.Add(bfsNode.mySpot);
                    queue.Enqueue(bfsNode);
                }
            }
        }
        return(solver);
    }
Esempio n. 10
0
 public BfsNode(Vector2 me, BfsNode per, int dep)
 {
     mySpot = me; perent = per; depth = dep;
 }
Esempio n. 11
0
 public BfsNode(Vector2 me,BfsNode per ,int dep )
 {
     mySpot = me; perent = per; depth = dep;
 }
Esempio n. 12
0
    private void ReconctingGrath(BfsNode i_goodStart)
    {
        BfsNode son = i_goodStart ;
        BfsNode dad = son.perent;
        RoomNode n1 = m_rooms[(int)son.mySpot.y, (int)son.mySpot.x], n2 = m_rooms[(int)dad.mySpot.y, (int)dad.mySpot.x];
        while(n2.m_activRoom == false)
        {
        n2.ActivatRoom(new List<RoomNode> { n1 });
        son = dad;
        dad = dad.perent;
        }
        n2.m_niebringRooms.Add(n1);
        n1.m_niebringRooms.Add(n2);

        n2.refRoom.FindTheDoorBetwin2RoomsAndActivate(n1.refRoom);
    }
Esempio n. 13
0
    private Stack<Vector3> getV3PathFromBfsNode(Vector3 i_Target, BfsNode i_bfsNode)
    {
        Stack<Vector3> stack = new Stack<Vector3>();
        if (i_bfsNode != null)
        {
            BfsNode bfsNode = i_bfsNode;

            stack.Push(i_Target);

            while (bfsNode != null)
            {
                stack.Push(m_rooms[(int)bfsNode.mySpot.y, (int)bfsNode.mySpot.x].refRoom.transform.position);
                bfsNode = bfsNode.perent;
            }
        }

        return stack;
    }
Esempio n. 14
0
    private List<BfsNode> getGoodNaeibersForBfs(BfsNode i_node,List<Vector2> i_arcivs)
    {
        List<BfsNode> nodss = new List<BfsNode>();
        int x = (int)i_node.mySpot.x;
        int y = (int)i_node.mySpot.y;

        if( x + 1 < c_x && i_arcivs.Contains(m_rooms[y,x + 1].myPosition ) != true )
        {
        i_arcivs.Add(m_rooms[y, x + 1].myPosition);
        nodss.Add(new BfsNode(m_rooms[y, x + 1].myPosition, i_node, i_node.depth + 1));
        }

        if (x  != 0 && i_arcivs.Contains(m_rooms[y, x - 1].myPosition) != true)
        {
        i_arcivs.Add(m_rooms[y, x - 1].myPosition);
        nodss.Add(new BfsNode(m_rooms[y, x - 1].myPosition, i_node, i_node.depth + 1));
        }

        if (y + 1 < c_y && i_arcivs.Contains(m_rooms[y + 1, x ].myPosition) != true)
        {
        i_arcivs.Add(m_rooms[y + 1,x].myPosition);
        nodss.Add(new BfsNode(m_rooms[y + 1, x].myPosition, i_node, i_node.depth + 1));
        }

        if (y != 0 && i_arcivs.Contains(m_rooms[y - 1, x].myPosition) != true)
        {
        i_arcivs.Add(m_rooms[y - 1 , x].myPosition);
        nodss.Add(new BfsNode(m_rooms[y - 1, x].myPosition, i_node, i_node.depth + 1));
        }

        return nodss;
    }
Esempio n. 15
0
    private BfsNode findSortPath(RoomNode i_start , RoomNode i_end)
    {
        Queue<BfsNode> queue = new Queue<BfsNode>();
        List<Vector2> arcivs = new List<Vector2>();
        queue.Enqueue(new BfsNode(i_start.myPosition, null, 0));
        BfsNode solver = null;

        while (solver == null && queue.Count != 0)
        {
            BfsNode node = queue.Dequeue();
            if (m_rooms[(int)node.mySpot.y, (int)node.mySpot.x].m_niebringRooms == null) continue;
            foreach (RoomNode room in m_rooms[(int)node.mySpot.y, (int)node.mySpot.x].m_niebringRooms)
            {
                if (isRoomVisabol(room) != true) continue;
                BfsNode bfsNode = new BfsNode(room.myPosition, node, node.depth + 1);
                if (room == i_end)
                {
                    solver = bfsNode;
                    break;
                }
                if (arcivs.Contains(bfsNode.mySpot) == false)
                {
                    arcivs.Add(bfsNode.mySpot);
                    queue.Enqueue(bfsNode);
                }
            }
        }
        return solver;
    }