我正在做一个迷宫,我想使用这里定义的递归方法。但是,我需要一些关于如何在随机绘制线条后随机打开线条的帮助。现在我正在创建线条(迷宫的墙壁),只需用它们的开始和结束 x 和 y 坐标绘制它们。我似乎无法找到一种简单的方法来“擦除”(或“打开”)部分线条。
编辑:好的,我需要稍微具体一点。我怎么能随机选择每条线上的地方“打开”?
编辑2:这是我正在尝试做的一些代码:
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();
}
现在我只需要弄清楚如何随机选择其中 3 行,并为每行随机擦除一部分。