경로 찾기에 문제가 있습니다 (내 첫 번째 경로이므로 예상 한 것입니다.) 항상 최단 경로를 차지하지는 않습니다. 예를 들어 하나의 정사각형을 아래로 이동하려면 경로가 왼쪽에 1 개, 아래쪽으로 1 개, 오른쪽으로 1 개 있습니다.내 경로 찾기에서 최단 경로를 찾는 데 문제가 있습니다
public void getSquares(){
actPath = new String[Map.x][Map.y];
isDone = new boolean[Map.x][Map.y];
squareListener = new SquareListener[Map.x][Map.y];
getSquares2(x,y,0,new String());
}
public void getSquares2(int x, int y, int movesused, String path){
boolean test1 = false;
boolean test2 = false;
test1 = (x < 0 || y < 0 || x > Map.x || y > Map.y);
if(!test1){
test2 = Map.landTile[y][x].masterID != 11;
}
if(movesused <= 6 && (test1 || test2)){
addMoveSquare2(x,y, path);
getSquares2(x+1,y,movesused+1,path+"r");
getSquares2(x,y+1,movesused+1,path+"d");
getSquares2(x,y-1,movesused+1,path+"u");
getSquares2(x-1,y,movesused+1,path+"l");
}
}
public void addMoveSquare2(int x, int y, String path){
if(x >= 0 && y>=0 && x < Map.x && y < Map.y && (actPath[x][y] == null || actPath[x][y].length() > path.length())){
if(squareListener[x][y] == null){
actPath[x][y] = new String();
actPath[x][y] = path;
JLabel square = new JLabel();
square.setBounds(x*16,y*16,16,16);
square.setIcon(moveSquare);
squareListener[x][y] = new SquareListener(x,y,path);
square.addMouseListener(squareListener[x][y]);
Map.cases.add(square);
}
else{
squareListener[x][y].path = path;
}
}
}
SquareListener는 사각형의 위치와 경로를 인쇄하는 간단한 MouseListener입니다. Map.x, Map.y는지도 크기입니다. getSquares2가 시작점과 함께 호출되고 여섯 개의 이동 거리 인 모든 사각형을 그립니다. 값 "11"인 모든 경우를 장애물로 간주합니다.
내가 잘못한 것을 찾는 것을 도와 줄 수 있습니까?
다음은 결과 스크린 샷입니다. http://img808.imageshack.us/img808/96/screen.gif 빨간색 사각형이 가능한 목표입니다. 실제 하나는 플레이어가 하나의 사각형을 클릭 할 때 정의됩니다 (MouseListener는 SquareListener이므로 걸릴 경로를 알고 있어야합니다). 주택은 장애물 인 "11"값을 가진 경우입니다.
이것이 숙제 인 경우 "숙제"태그를 추가하는 것이 일반적입니다. –
소스를 전체 프로그램에 게시하십시오. –
수업 숙제가 아닙니다. 패스 파인더는 프로그램의 일부일뿐입니다. 추가 할 내용은 무엇입니까? – Cheshire