I have an issue with a for loop in java. I’m trying to do an Iterative deeping search and the code for generating children at depth n looks like this:
for(Iterator<puzzleBoard> child = generateSuccessorsIDS(pb).iterator(); child.hasNext();){
DLS(child.next(),(depth-1));
}
When not using the return statement the DLS does like it should do, but the value doesn’t reach the calling function because of the missing return statement. When using return DLS(…) it just returns the first value as the iterator generates. How to solve this? I paste the whole DLS and it’s caller function below.
private puzzleBoard IDS(String initial){
puzzleBoard pb = new puzzleBoard(initial,0,new Vector<Integer>(),new Vector<puzzleBoard>(),new Vector<puzzleBoard>());
puzzleBoard result=new puzzleBoard("999999999",0,new Vector<Integer>(),new Vector<puzzleBoard>(),new Vector<puzzleBoard>());
for(int depth=0;depth<3;depth++){//Repeat
System.out.println("DP "+depth);
result = DLS(pb,depth);
System.out.println("Here: "+result.toString());
if(result.isGoalState())
return result;
}
return new puzzleBoard("999999999",0,new Vector<Integer>());
}
private puzzleBoard DLS(puzzleBoard pb, int depth){
pb.printPuzzle();
if(depth==0 && pb.isGoalState()){
System.out.println("!!!!!WOOOOOW!!!!!");
return pb;
}
else if(depth>0){
for(Iterator<puzzleBoard> child = generateSuccessorsIDS(pb).iterator(); child.hasNext();){
DLS(child.next(),(depth-1));
}
}
else
return new puzzleBoard("999999999",0,new Vector<Integer>(),new Vector<puzzleBoard>(),new Vector<puzzleBoard>());
return pb;
}
May be I am wrong, but I think you should place your for loop outside, where you are actually calling your function
DLS(puzzleBoard pb, int depth)for the first time…..Inside yourelse ifonly callDLS(pb,depth);