yiwei
yiwei

Reputation: 4190

how to randomly open up lines for maze in java

I am making a maze and I would like to use the recursive method as defined here. However, I need some help as to how to randomly open up the lines once I randomly draw them. Right now I'm creating the lines (walls of the maze) simply by drawing them with their beginning and end x- and y-coordinates. I just can't seem to find a simple way to "erase" (or "open up") parts of the lines.

EDIT: Okay I need to be slightly more specific. How could I randomly select places on each line to "open up?"

EDIT 2: Here is some code of what I'm trying to do:

public static void draw() {
    // picks a random spot in the rectangle
    Random r = new Random();
    int x0 = r.nextInt(w)
    int y0 = r.nextInt(h)

    // draws the 4 lines that are perpendicular to each other and meet
    // at the selected point
    StdDraw.line(x0, 0, x0, y0);
    StdDraw.line(0, y0, x0, y0);
    StdDraw.line(x0, h, x0, y0);
    StdDraw.line(w, y0, x0, y0);
}

public static void main(String[] args) {   
    // set up the walls of the maze
    // given w = width and h = height
    StdDraw.setXscale(0, w);
    StdDraw.setYscale(0, h);

    StdDraw.line(0, 0, 0, h);
    StdDraw.line(0, h, w, h);
    StdDraw.line(w, h, w, 0);
    StdDraw.line(w, 0, 0, 0);

    draw();
}

Now I just need to figure out how to randomly select 3 of these lines, and for each line randomly erase a portion.

Upvotes: 0

Views: 415

Answers (1)

Nick Rippe
Nick Rippe

Reputation: 6475

Assuming you're using swing and the paintComponent method, you would set the Graphic's color to the background color and draw over the line again. Here's an example:

public class DrawTest extends JPanel{
    public static void main(String[] args)
    {

        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new DrawTest());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public Dimension getPreferredSize(){
        return new Dimension(400,300);
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.black);
        g.drawLine(10, 10, 100, 10);
        g.setColor(getBackground());
        g.drawLine(50, 10, 60, 10);
    }
}

EDIT

I suppose you're not creating the Maze in the paintComponent method (or you'd end up with a new maze every time you repainted). So, I'd recommend creating a sub-class similar to below and storing instances of it in an ArrayList field of your main class. Then you can iterate through your ArrayList when you're doing your panting.

public static class MazeWall{
    public static final int OpeningWidth = 10;

    Point start;
    Point end;
    Point opening;

    public MazeWall(Point start, Point end, boolean hasOpening){
        this.start = start;
        this.end = end;

        if(hasOpening){
            int range;
            if(start.x == end.x){
                range = end.x - start.x - OpeningWidth;
                int location = (int)(Math.random() * range + start.x);
                opening = new Point(location, start.y);
            } else{
                range = end.y - start.y - OpeningWidth;
                int location = (int)(Math.random() * range + start.y);
                opening = new Point(location, start.x);
            }
        }   
    }
}

Upvotes: 1

Related Questions