Hello everyone,
New here. I am working on the N-Queen program and encountered a problem. The method below works and produces solutions, but it runs infinitely! Can someone take a look and see if you can catch what part of the code I need to change to fix this? I would appreciate any response, thank you!
public static void solveQueens(){ while(!noMoreSol){ for(int r=1;r<=bSize;r++){//For loop if(temp.getCol()+1==bSize && wasConflict){ temp=board.pop(); r=temp.getRow(); tempCol=temp.getCol()+1; back=true; }//End if else{ countConflict=0; }//End else for(int c=1; c<=bSize; c++){//For loop if(back){ if(tempCol==bSize&&c==bSize){ r--; temp=board.pop(); c=temp.getCol()+1; countConflict=c; }//End if else{ c=tempCol; countConflict=c-1; back=false; }//End else }//End if queenPlacer(r,c,board); if(conflictTester(board)){ wasConflict=true; countConflict++; board.pop(); if(countConflict==bSize){ temp=board.pop(); r--; if(temp.getCol()==bSize){ if(!board.isEmpty()){ temp=board.pop(); }//End if c=temp.getCol(); countConflict=c; r--; }//End if else{ c=temp.getCol(); countConflict=c; }//End else }//End If }//End if else{ if(r==bSize){ if(bSize==board.size()){ for(int i=board.size()-1;i>=0;i--){//For loop System.out.print(board.itemAt(i).toString()); }//End for System.out.println(); }//End if r--;//This is making it loop infinitely. temp=board.pop(); while(temp.getCol()==bSize){//While loop temp=board.pop(); r--; }//End while c=temp.getCol(); tempCol=c+1; countConflict=c; back=true; if(r==1&&temp.getCol()==bSize) { noMoreSol=true; }//End if }//End if wasConflict=false; break; }//End of else }//End of For }//End of for }//End of while }// end of solve method.