나는 미로 생성기를 쓰고있다. 다음과 같은 "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 보인다.
아무도 도와 줄 수 있습니까?
질문에 대한 답변이 없지만 (IMO) 유용한 팁 : Java 코드 규칙을 사용하십시오! 그렇게함으로써, Java에 익숙한 다른 사람들이 당신에게 도움의 손길을 더 쉽게 줄 것입니다. 클래스 이름은 대문자로 시작하고, 변수 이름은 소문자로 시작합니다. 자세한 정보 : http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html –
@Bart, 알겠습니다. 웬일인지 공개 위원이 대문자로 시작한다고 생각했습니다. 나는 관습에 충실 할 것이다. – Scorcher84
지금 자바 관례로 바꾸려고했습니다. – Scorcher84