Exemplo n.º 1
0
        //
        // The Grid constructor will populate the 10 by 10 array of GRectangles, which we call
        // gridRec, and then will go through, and in a second pass, for each rectangle, point
        // to it's neighbors via a List of GRectangles.
        //
        public Grid()
        {
            gridRec = new GRectangle[10, 10];   // Must create an instance of gridRec
            brushgrey = new SolidBrush(Color.DimGray);
            brushwhite = new SolidBrush(Color.White);
            blackPen = new Pen(Color.Black, 1);
            //
            // Here we initialize the grid, which consists of an array of GRectangles - just
            // C# Rectangles with some extra properties and methods.
            // Initially, wall is set to true (all squares are walls), then the Search backtracking
            // methed will produce the 'free' squares, by setting wall to false.
            //
            x = 0; y = 0; width = 45; height = 35;
            for (int i = gridRec.GetLowerBound(0); i <= gridRec.GetUpperBound(0); i++)
            {
                x = 0;
                for (int j = gridRec.GetLowerBound(1); j <= gridRec.GetUpperBound(1); j++)
                {
                    gridRec[i, j] = new GRectangle(x, y, width, height, true, i, j);
                    x += 45;
                }
                y += 35;
            }  // end of for loop

            //
            // Take second pass through the array of GRectangles, to setup the neighbors
            // A neighbor is merely an adjacent square.
            x = 0; y = 0; width = 45; height = 35;
            for (int i = gridRec.GetLowerBound(0); i <= gridRec.GetUpperBound(0); i++)
            {
                x = 0;
                for (int j = gridRec.GetLowerBound(1); j <= gridRec.GetUpperBound(1); j++)
                {
                    // Up, Down, Left, Right in order for List
                    if (i > 0) { gridRec[i,j].setUp(gridRec[i-1, j]); }

                    if (i < 9) { gridRec[i,j].setDown(gridRec[i+1,j]); }
                    if (j > 0) { gridRec[i,j].setLeft(gridRec[i, j - 1]); }
                    if (j < 9) { gridRec[i,j].setRight(gridRec[i, j + 1]); }

                    x += 45;
                }
                y += 35;
            } //end of second for loop
            //
            //
            // Begin the recursive search for the maze, starting at upper left corner square, which is
            // position 0,0
            rand = new Random();  // Seed a random number generator.
            int Counter = 0;
            Search(gridRec[0, 0], ref Counter);  // Still in the constructor, we fire off the recursion with this first
                                                 // call to Search (begin at upper left corner of the grid).
        }
Exemplo n.º 2
0
        //
        // The Grid constructor will populate the 10 by 10 array of GRectangles, which we call
        // gridRec, and then will go through, and in a second pass, for each rectangle, point
        // to it's neighbors via a List of GRectangles.
        //
        public Grid()
        {
            gridRec = new GRectangle[25, 25];
            S = new Pair<GRectangle, int>[dimx*dimy];
            brushgrey = new SolidBrush(Color.DimGray);
            brushwhite = new SolidBrush(Color.White);
            blackPen = new Pen(Color.Black, 1);
            greyPen = new Pen(Color.DimGray);
            //
            // Here we initialize the grid, which consists of an array of GRectangles - just
            // C# Rectangles with some extra properties and methods.
            // Initially, wall is set to true (all squares are walls), then the Search backtracking
            // methed will produce the 'free' squares, by setting wall to false.
            //
            x = 0; y = 0; width = 500/dimx; height = 500/dimy;
            for (int i = gridRec.GetLowerBound(0); i <= gridRec.GetUpperBound(0); i++)
            {
                x = 0;
                for (int j = gridRec.GetLowerBound(1); j <= gridRec.GetUpperBound(1); j++)
                {
                    gridRec[i, j] = new GRectangle(x, y, width, height, true, i, j);

                    x += width;
                }
                y += height;
            }  // end of for loop

            //
            // Take second pass through the array of GRectangles, to setup the neighbors
            //
            x = 0; y = 0;
            for (int i = gridRec.GetLowerBound(0); i <= gridRec.GetUpperBound(0); i++)
            {
                x = 0;
                for (int j = gridRec.GetLowerBound(1); j <= gridRec.GetUpperBound(1); j++)
                {
                    // Up, Down, Left, Right in order for List
                    if (i > 0) { gridRec[i, j].setUp(gridRec[i - 1, j]); }

                    if (i < dimx-1) { gridRec[i, j].setDown(gridRec[i + 1, j]); }
                    if (j > 0) { gridRec[i, j].setLeft(gridRec[i, j - 1]); }
                    if (j < dimy-1) { gridRec[i, j].setRight(gridRec[i, j + 1]); }
                    S[dimx * i + j] = new Pair<GRectangle, int>();
                    S[dimx * i + j].First = gridRec[i, j];
                    S[dimx * i + j].Second = -1;
                    x += width;
                }
                y += height;
            } //end of second for loop
            //
        }
Exemplo n.º 3
0
 public void setUp(GRectangle u)
 {
     Up = true;
     Neighbors.Add(u);
 }
Exemplo n.º 4
0
 public void setRight(GRectangle u)
 {
     Right = true;
     Neighbors.Add(u);
 }
Exemplo n.º 5
0
 public void setLeft(GRectangle u)
 {
     Left = true;
     Neighbors.Add(u);
 }
Exemplo n.º 6
0
 public void setDown(GRectangle u)
 {
     Down = true;
      Neighbors.Add(u);
 }
Exemplo n.º 7
0
        //
        //
        // The method Search will do depth-first search to create the maze.
        //
        public void Search(GRectangle r, ref int callcounter)
        {
            //
            //
            // If the curreent square is not available, we return.  But if it's
            // available, we make it free (change wall to false), and then search
            // through it's neighbors, calling Search again for each of them.
            if (r.isAvailable() == false) // Is it available?
                return;
            r.makeFree();  // Make it free (no longer a wall).
            //
            // The following line is temporary: just for debugging.  callcounter will be printed out
            // in the drawGrid method: it tracks the order in which the squares were 'dug out'.
            callcounter++; r.setCounter(callcounter);  // We house the callcounter in each square, so we can access it later.
            //
            // We randomly choose which neighbor of the current square to look at, to get
            // a more robust maze. testrec is just a copy of the neighbors.  Each time we select a neighbor
            // we remove it from testrec, so we can then get a random integer within the new, smaller range on
            // the next iteration
            List<GRectangle> testrec = r.getNeighbors();  // Get a copy of the list of neighbors for r.
            do
            {
                int testidx = rand.Next(testrec.Count);
                if (testrec[testidx].isWall() == true)  // Only walls need to considered to 'burrow through'.
                {
                    Search(testrec[testidx], ref callcounter);  // Here we call Search for each of the remainding neighbors.
                }
                testrec.RemoveAt(testidx); // As this one was considered, remove it (so now we have a smaller list).
            } while (testrec.Count > 0); // end of do while

            return;
        }
Exemplo n.º 8
0
 //
 // The next four methods merely add neighbors to the Neighbors list, as needed.
 public void setUp(GRectangle u)
 {
     Neighbors.Add(u);
 }