public void BuildMaze()
        {
            // pick a random square
            int c = 0;
            int r = random.Next(maze.Height);
            Square startingSquare = maze.SquareAt(r, c);

            // set current square to starting square
            currentSquare = startingSquare;

            // remove LEFT wall of current square
            currentSquare.RemoveSide(Square.Direction.LEFT);

            // set last side to left
            lastSide = Square.Direction.LEFT;

            // while there remains any blocked square in the grid
            while (maze.IsAnyBlocked()) {
                // set bool addingSquare to false
                addingSquareFlag = false;

                // create a random sequence of the 0, 1, 2, or 3 remaining walls for currentSquare
                // exclude sides that lead to unblocked squares (squares that are already in the maze)
                directions.Clear();
                if (currentSquare.HasSide(Square.Direction.LEFT)) {
                    if (currentSquare.AdjacentSquare(Square.Direction.LEFT) != null &&
                        currentSquare.AdjacentSquare(Square.Direction.LEFT).IsBlocked()) {
                        directions.Add(Square.Direction.LEFT);
                    }
                }

                if (currentSquare.HasSide(Square.Direction.RIGHT)) {
                    if (currentSquare.AdjacentSquare(Square.Direction.RIGHT) != null &&
                        currentSquare.AdjacentSquare(Square.Direction.RIGHT).IsBlocked()) {
                        directions.Add(Square.Direction.RIGHT);
                    }
                }

                if (currentSquare.HasSide(Square.Direction.TOP)) {
                    if (currentSquare.AdjacentSquare(Square.Direction.TOP) != null &&
                        currentSquare.AdjacentSquare(Square.Direction.TOP).IsBlocked()) {
                        directions.Add(Square.Direction.TOP);
                    }
                }

                if (currentSquare.HasSide(Square.Direction.BOTTOM)) {
                    if (currentSquare.AdjacentSquare(Square.Direction.BOTTOM) != null &&
                        currentSquare.AdjacentSquare(Square.Direction.BOTTOM).IsBlocked()) {
                        directions.Add(Square.Direction.BOTTOM);
                    }
                }

                // exclude lastSide
                directions.Remove(lastSide);

                // shuffle them
                Shuffle(directions);

                foreach (Square.Direction dir in directions) {
                    // let nextSide be the current element in this sequence
                    Square.Direction nextSide = dir;

                    // if square adjacent to the current square on side nextSide exists and is blocked
                    if (currentSquare.AdjacentSquare(nextSide) != null && currentSquare.AdjacentSquare(nextSide).IsBlocked()) {
                        // remove walls that separate currentSquare and adjacentSquare
                        Square adjacent = currentSquare.AdjacentSquare(nextSide);
                        currentSquare.RemoveSide(nextSide);
                        adjacent.RemoveSide(Square.OppositeSide(nextSide));

                        // add path to adjacency matrix
                        int u = ConvertToNode(currentSquare.Row, currentSquare.Column);
                        int v = ConvertToNode(adjacent.Row, adjacent.Column);
                        adjacentMatrix[u, v] = true;
                        adjacentMatrix[v, u] = true;

                        // adjacentSquare becomes currentSquare
                        currentSquare = adjacent;

                        // set boolean addingSquareFlag to true
                        addingSquareFlag = true;

                        // set lastSide to the opposite of nextSide
                        lastSide = Square.OppositeSide(nextSide);
                        break;
                    }
                }

                // this means we couldn't find any adjacent square to move to in the for loop above
                // we now have to backtrack by picking a random unblocked square elsewhere in the maze
                // and try to build a path from that point
                if (!addingSquareFlag) {
                    // pick an unblocked square (already in the maze) from the grid at
                    // random, optionally ensuring that it is adjacent to at least one
                    // blocked square (not already in the maze)
                    // this becomes the current square
                    currentSquare = maze.SelectRandomUnblockedSquare();
                    if (currentSquare == null)
                        break;

                    // randomly pick an open side (one with wall already removed) of the
                    // current square, this becomes lastSide
                    lastSide = currentSquare.SelectOpenSide();
                }

                // redraw
                UpdateDrawing();
            }

            // update state
            mazeState = MazeState.BUILT;
        }