为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我的 2D Array Maze 不打印生成的内容?

为什么我的 2D Array Maze 不打印生成的内容?

慕虎7371278 2021-09-15 16:53:52
我的目标是生成一个由 Cell 对象的 2D 数组组成的迷宫。下面是单元格和迷宫的代码。使用调试器我可以看到布尔值正在改变值,并且生成按预期进行,但是当它打印出来时,没有路径。所有的墙都还在原地,我似乎无法弄清楚断开的位置,如果有的话?这是 Cell 类:public class Cell {    //coordinates    private int x;    private int y;    //cell status    private boolean visited;    //cell walls status    private boolean northWall;    private boolean southWall;    private boolean eastWall;    private boolean westWall;    public Cell(int x, int y) {        this.x = x;        this.y = y;        visited =  false;        northWall= true;        southWall= true;        eastWall = true;        westWall = true;    }    public int getX() {        return x;    }    public int getY() {        return y;    }    public boolean isVisited() {        return visited;    }    public void setVisited(boolean visited) {        this.visited = visited;    }    public boolean isNorthWall() {        return northWall;    }    public void setNorthWall(boolean northWall) {        this.northWall = northWall;    }    public boolean isSouthWall() {        return southWall;    }    public void setSouthWall(boolean southWall) {        this.southWall = southWall;    }    public boolean isEastWall() {        return eastWall;    }    public void setEastWall(boolean eastWall) {        this.eastWall = eastWall;    }    public boolean isWestWall() {        return westWall;    }    public void setWestWall(boolean westWall) {        this.westWall = westWall;    }
查看完整描述

2 回答

?
汪汪一只猫

TA贡献1898条经验 获得超8个赞

你不是在打印你的迷宫。


在该printMaze()方法中,您正确地循环了迷宫,但不是打印出迷宫单元格,而是在每次迭代时创建一个新的(空白)单元格。


public void printMaze() {

    for (int i = 0; i < height; i++) {

        /* ... */

        for (int j = 0; j < width; j++) {

            Cell current = new Cell(i, j); // <- HERE

            /* ... */

        }

        /* ... */

    }

    /* ... */

}

该行应该是:


Cell current = maze[i][j];


查看完整回答
反对 回复 2021-09-15
?
慕运维8079593

TA贡献1876条经验 获得超5个赞

而不是:Cell current = new Cell(i, j);

尝试:细胞电流 = 迷宫 [i,j];


查看完整回答
反对 回复 2021-09-15
  • 2 回答
  • 0 关注
  • 147 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号