确保所有边界在单元格网格中均有效

时间:2018-10-25 04:29:54

标签: java

根据我的理解,此方法是boolean方法。这意味着return语句应为truefalse

因此,我最初已将数组中给定单元格的所有边界都设置为true(这是在另一个类中完成的)。

例如hasNorth(), hasSouth()等都设置为true。

此方法将检查每个单元是否具有有效的墙。例如,如果该单元格在北部具有有效墙,则将检查相邻单元格的相应墙以确保在南部具有有效墙。如果是这样,那么墙就留下了。但是,如果两个方向中的任何一个都没有该方向的墙,则对 BOTH 单元格和相应的墙返回false。

我在这里尝试了代码,但是我感觉我不知道如何说墙壁是否无效以及该怎么做:

public boolean bordersAreValid() {
     boolean north = false;
     boolean south = false;
     boolean east = false;
     boolean west = false;

        for (int i = 0; i < this.cells.length - 1; i++) {
            for(int j = 0; j <this.cells[0].length -1; j++) {
                if(this.cells[i][j].hasNorth() == this.cells[i][j-1].hasSouth()) {
                    north = true;
                }
                if(this.cells[i][j].hasSouth() == this.cells[i][j+1].hasNorth()) {
                    south = true;
                }
                if(this.cells[i][j].hasWest() == this.cells[i+1][j].hasEast()) {
                     west = true;
                }
                   if(this.cells[i][j].hasNorth() == this.cells[i-1][j].hasSouth()) {
                     east = true;
                }

     }
    }
   }

0 个答案:

没有答案