猿问

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

我的目标是生成一个由 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;

    }


慕虎7371278
浏览 148回答 2
2回答

汪汪一只猫

你不是在打印你的迷宫。在该printMaze()方法中,您正确地循环了迷宫,但不是打印出迷宫单元格,而是在每次迭代时创建一个新的(空白)单元格。public void printMaze() {&nbsp; &nbsp; for (int i = 0; i < height; i++) {&nbsp; &nbsp; &nbsp; &nbsp; /* ... */&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < width; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cell current = new Cell(i, j); // <- HERE&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* ... */&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; /* ... */&nbsp; &nbsp; }&nbsp; &nbsp; /* ... */}该行应该是:Cell current = maze[i][j];

慕运维8079593

而不是:Cell current = new Cell(i, j);尝试:细胞电流 = 迷宫 [i,j];
随时随地看视频慕课网APP

相关分类

Java
我要回答