2009-10-15 7 views
0

나는 미로 생성기를 쓰고있다. 다음과 같은 "Cell"클래스가 있습니다 :Java로 작성한이 방법을 어떻게 정리할 수 있습니까?

public class Cell { 
    public boolean northWall; 
    public boolean southWall; 
    public boolean eastWall; 
    public boolean westWall; 

    public Cell north; 
    public Cell south; 
    public Cell east; 
    public Cell west; 

    public boolean visited; 

    public Cell() { 
     northWall = true; 
     southWall = true; 
     eastWall = true; 
     westWall = true; 
     visited = false; 
    } 

    public boolean hasUnvisitedNeighbors() { 
     return ((north != null && !north.Visited) 
       || (south != null && !south.Visited) 
       || (east != null && !east.Visited) || (west != null && !west.Visited)); 
    } 

    public Cell removeRandomWall() { 
     List<Cell> unvisitedNeighbors = new ArrayList<Cell>(); 
     if (north != null && !north.Visited) 
      unvisitedNeighbors.add(north); 
     if (south != null && !south.Visited) 
      unvisitedNeighbors.add(south); 
     if (west != null && !west.Visited) 
      unvisitedNeighbors.add(west); 
     if (east != null && !east.Visited) 
      unvisitedNeighbors.add(east); 



     if (unvisitedNeighbors.size() == 0) { 
      return null; 
     } else { 
      Random randGen = new Random(); 
      Cell neighbor = unvisitedNeighbors.get(randGen 
        .nextInt((unvisitedNeighbors.size()))); 

      if (neighbor == north) { 
       northWall = false; 
       north.southWall = false; 
       return north; 
      } else if (neighbor == south) { 
       southWall = false; 
       south.northWall = false; 
       return south; 
      } else if (neighbor == west) { 
       westWall = false; 
       west.eastWall = false; 
       return west; 
      } else if (neighbor == east) { 
       eastWall = false; 
       east.westWall = false; 
       return east; 
      } 

      return null; 
     } 

    } 
} 

내 프로그램의 미로는 단순히 셀의 2 차원 배열입니다. 배열을 만든 후에는 수동으로 이동하여 인접한 셀 (북쪽, 남쪽, 동쪽, 서쪽)에 대한 모든 참조를 설정합니다.

내가 정리하려고하는 것은 입니다. removeRandomWall()입니다. 방문 플래그가 false로 설정된 인접 셀을 무작위로 선택하고이 셀과 셀을 연결하는 인접 셀 모두에서 벽을 제거한다고 가정합니다.

방문한 적이없는 모든 인접 셀을 무작위로 선택한 다음이 셀의 벽과 인접한 셀을 거짓으로 설정하여 이제는 그 사이에 경로가 있어야합니다. 나는 그것을 위에 시도했다 그러나 그것은 매우 cludgey 보인다.

아무도 도와 줄 수 있습니까?

+1

질문에 대한 답변이 없지만 (IMO) 유용한 팁 : Java 코드 규칙을 사용하십시오! 그렇게함으로써, Java에 익숙한 다른 사람들이 당신에게 도움의 손길을 더 쉽게 줄 것입니다. 클래스 이름은 대문자로 시작하고, 변수 이름은 소문자로 시작합니다. 자세한 정보 : http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html –

+0

@Bart, 알겠습니다. 웬일인지 공개 위원이 대문자로 시작한다고 생각했습니다. 나는 관습에 충실 할 것이다. – Scorcher84

+0

지금 자바 관례로 바꾸려고했습니다. – Scorcher84

답변

4

대신 4 명 별도의 회원들 필요없이 :

public Cell [] cells = new Cell[4]; 

그리고 4 상수 :

public final int NORTH = 0; 
public final int EAST = 1; 
public final int SOUTH = 2; 
public final int WEST = 3; 

가 무작위로 벽을 제거하는 등의 일을하게

public Cell North; 
public Cell South; 
public Cell East; 
public Cell West; 

을 그냥 1 개 배열을 훨씬 더 쉽습니다.

3

방향에 대한 열거를 한 다음 방향을 지정하여 벽과 이웃에 액세스합니다.

멤버 변수를 비공개로 만들고 소문자로 작성해야합니다. 당신이 할 수있는 경우

+0

9,999에있는 동안 빠르게 스크린 샷을 찍어보세요! –

1

첫 번째 시도는, 내가 회원이 변수 민간 최종 만들 것입니다 :는 "널 셀"개체를 사용하여, 널 (null)에 대한 검사를 제거하는

public class Cell { 
    public boolean NorthWall; 
    public boolean SouthWall; 
    public boolean EastWall; 
    public boolean WestWall; 

    public Cell North; 
    public Cell South; 
    public Cell East; 
    public Cell West; 

    public boolean Visited; 

    public Cell() { 
     NorthWall = true; 
     SouthWall = true; 
     EastWall = true; 
     WestWall = true; 
     Visited = false; 
    } 

    public boolean hasUnvisitedNeighbors() { 
     return unvisited(North) || unvisited(South) || unvisited(East) || unvisited(West); 
    } 

    private List<Cell> getUnvisitedNeighbors() { 
     List<Cell> result = new ArrayList<Cell>(); 
     if (unvisited(North)) 
      result.add(North); 
     if (unvisited(South)) 
      result.add(South); 
     if (unvisited(West)) 
      result.add(West); 
     if (unvisited(East)) 
      result.add(East); 
     return result; 
    } 

    private boolean unvisited(Cell cell) { 
     return cell != null && !cell.Visited; 
    } 

    private Cell getRandomUnvisitedNeighbor() { 
     Random randGen = new Random(); 
     List<Cell> unvisitedNeighbors = getUnvisitedNeighbors(); 
     return unvisitedNeighbors.get(randGen.nextInt((unvisitedNeighbors.size()))); 
    } 

    public Cell removeRandomWall() { 
     if (!hasUnvisitedNeighbors()) { 
      return null; 
     } 
     Cell neighbor = getRandomUnvisitedNeighbor(); 
     if (neighbor == North) { 
      NorthWall = false; 
      North.SouthWall = false; 
     } else if (neighbor == South) { 
      SouthWall = false; 
      South.NorthWall = false; 
     } else if (neighbor == West) { 
      WestWall = false; 
      West.EastWall = false; 
     } else if (neighbor == East) { 
      EastWall = false; 
      East.WestWall = false; 
     } 

     return neighbor; 

    } 
} 
0

시도 ...

public static final Cell NULL_CELL = new Cell(); 

그런 셀이 존재하지 않음을 나타 내기 위해 null을 사용하는 곳에서는 이제 NULL_CELL을 사용할 수 있습니다. 당신이

if (north != null && !north.Visited) 
       unvisitedNeighbors.add(north); 

일반적으로 자바

if (!north.isVisited()) { 
    unvisitedNeighbors.add(north); 
} 

으로, 멤버 변수 개인, 그리고 당신이 그들에 액세스하기 위해 "게터"를 사용하여 대체 할 수있는 지금

...

private boolean northWall; 
private boolean southWall; 
private boolean eastWall; 
private boolean westWall; 

private Cell north; 
private Cell south; 
private Cell east; 
private Cell west; 

private boolean visited; 

public boolean hasNorthWall() { 
    return northWall; 
} 

public Cell getNorthCell() { 
    return north; 
} 

// etc. 
관련 문제