Ejemplo n.º 1
0
    public MazeGraph GenerateGraphPathFromMaze(Int32Point point)
    {
        MazeGraph pathGraph = new MazeGraph();

        Stack <Int32Point> cells          = new Stack <Int32Point> ();
        List <Int32Point>  processedCells = new List <Int32Point> ();

        cells.Push(point);

        while (cells.Count > 0)
        {
            Int32Point cell = cells.Pop();

            if (processedCells.Contains(cell))
            {
                continue;
            }

            processedCells.Add(cell);
            List <Int32Point> neighbours = GetCellNeighbours(cell);

            foreach (Int32Point neighbour in neighbours)
            {
                if (!HasWallBetween(cell, neighbour))
                {
                    pathGraph.AddEdge(cell, neighbour);
                    cells.Push(neighbour);
                }
            }
        }

        //Debug.Log (pathGraph);
        return(pathGraph);
    }
Ejemplo n.º 2
0
        private void init()
        {
            //this.UpdateStatus(Status.Starting());
            int total = Width * Height;

            graph = new  MazeGraph(false, total, 4);

            matrix = new MazeNode[Width, Height];

            //create the nodes
            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    matrix[x, y] = new MazeNode(x + (y * Width), NodeType.Floor);
                    graph.AddVertex(matrix[x, y]);
                }
                //this.UpdateStatus(Status.UpdateLoopMessage("Creating nodes", x, Width));
            }

            //populate the edges
            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    /*
                     * if (x == 0)
                     * {
                     *  matrix[x, y].LeftEdge = new MazeEdge(matrix[x, y], null);
                     * }
                     * if (y == 0)
                     * {
                     *  matrix[x, y].TopEdge = new MazeEdge(matrix[x, y], null);
                     * }
                     * if (x == (Width - 1))
                     * {
                     *  matrix[x, y].RightEdge = new MazeEdge(matrix[x, y], null);
                     * }
                     * if (y == (Height - 1))
                     * {
                     *  matrix[x, y].BottomEdge = new MazeEdge(matrix[x, y], null);
                     * }*/


                    if (x > 0)
                    {
                        //matrix[x, y].LeftEdge = new MazeEdge(matrix[x, y], matrix[x - 1, y]);
                        //graph.AddEdge(matrix[x, y].LeftEdge);

                        matrix[x, y].LeftEdge = matrix[x - 1, y].RightEdge;
                    }
                    if (y > 0)
                    {
                        //matrix[x, y].TopEdge = new MazeEdge(matrix[x, y], matrix[x, y - 1]);
                        //graph.AddEdge(matrix[x, y].TopEdge);

                        matrix[x, y].TopEdge = matrix[x, y - 1].BottomEdge;
                    }
                    if (x < (Width - 1))
                    {
                        matrix[x, y].RightEdge = new MazeEdge(matrix[x, y], matrix[x + 1, y]);
                        graph.AddEdge(matrix[x, y].RightEdge);
                    }
                    if (y < (Height - 1))
                    {
                        matrix[x, y].BottomEdge = new MazeEdge(matrix[x, y], matrix[x, y + 1]);
                        graph.AddEdge(matrix[x, y].BottomEdge);
                    }
                }
                //this.UpdateStatus(Status.UpdateLoopMessage("Populating edges", x, Width));
            }

            //this.UpdateStatus(Status.Done());
        }