나는 미로를 객체 지향 방식으로 풀려고한다.스택을 사용하여 미로 해결하기 객체 지향 (역 추적) 문제
나의 미로는 필드로 구성되며, 각 필드에는 요소 속성이 있습니다. 요소는 벽, 선수, 헬퍼 또는 종점입니다. 빈 특성이있는 필드는 경로입니다.
각 필드는 HashMap에서 자신의 이웃을 추적합니다.
플레이어가 도우미를 치면 도우미가 미로를 해결하고 올바른 경로를 표시해야합니다.
헬퍼의 현재 위치를 가져 와서 이웃 필드를 스택에 저장하여 엔드 포인트를 찾으려고합니다.
이것은 헬퍼가가는 한 코드입니다. 루프가 끝나지 않고 그 이유를 모르겠습니다.
public void findRoute() {
Collection<Field> c = currentField.getHashMap().values();
Stack<Field> fieldNeighbours = new Stack<Field>();
for (Field field : c) {
fieldNeighbours.push(field);
}
while (!endpointReached) {
Field p = fieldNeighbours.pop();
if (p.getElement().getNaam().equals("endPoint")) {
System.out.println("endPoint Reached! ");
endpointReached = true;
return;
}
if (!p.getElement().getNaam().equals("Wall")) {
if (!p.getHashMap().get("north").getElement().getNaam().equals("Wall")) {
fieldNeighbours.push(p.getHashMap().get("north"));
}
if (!p.getHashMap().get("south").getElement().getNaam().equals("Wall")) {
fieldNeighbours.push(p.getHashMap().get("south"));
}
if (!p.getHashMap().get("east").getElement().getNaam().equals("Wall")) {
fieldNeighbours.push(p.getHashMap().get("east"));
}
if (!p.getHashMap().get("west").getElement().getNaam().equals("wall")) {
fieldNeighbours.push(p.getHashMap().get("west"));
}
}
}
이전에 장소를 방문했는지 여부를 표시 할 방법이 필요합니다. 정말 도움이 될 수있는 나무 구조와 같은 미로를 생각하십시오 :) – Rogue