Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: N-Queen using Stack

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default N-Queen using Stack

    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.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,407
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: N-Queen using Stack

    it runs infinitely
    What condition(s) needs to be met for the code to exit?
    How are you trying to debug the code to see why the conditions are not met?

    Note: single letter variable names make the code harder to understand.
    Also the code needs comments describing the logic it is using to solve the problem.
    Some of the current comments are redundant. For example // For loop
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: N-Queen using Stack

    sorry about the messy code!
    It's supposed to exit once it it finds all the solutions for the board size (bSize), which is called in the main method.
    For example, if we give the board a size of 4, it should display 2 solutions and stop looping. But it displays the rows and columns where the solutions are and it keeps running infinitely.
    The line that's commented "r--;//This is making it loop infinitely" is where I think he bug is.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,407
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: N-Queen using Stack

    Try adding some print statements that print out the values of variables as the code executes. If you understand what the code is supposed to do, seeing what it is actually doing should help you see the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: N-Queen using Stack

    Thank you for responding by the way!
    I added print statements throughout the method. Right after the r--;//This is making it loop infinitely, before temp=board.pop(); print statement is in the infinite loop. r-- pulls the solutions but keeps reproducing the solutions.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,407
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: N-Queen using Stack

    Does what is printed make sense?

    When should this statement be true?
     if(r==1&&temp.getCol()==bSize) {
    Do the values of r become 1
    and the value returned by getCol() == bSize

    If neither of those tests are true at the same time, why not? What would make that statement have a true value?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: N-Queen using Stack

    I rechecked, that does not have to be there. I deleted that chunk of code, still same issue.
    It is supposed to check until no further solutions are found, then exit, not start all over.
    Last edited by VallisMar; April 11th, 2019 at 09:53 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,407
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: N-Queen using Stack

    check until no further solutions are found, then exit, not start all over.
    Why does that happen? What values are not being changed correctly to find the end?

    There are no comments in the code describing the program's logic so I can not see if the code is following your design or if the design would find the solution.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: N-Queen using Stack

    Your questions made me go back and re-check over and over till I figured it out. I ended one of the loops to test if the board size is equal to getCol(), then return; and worked!
    Thank you for taking the time to respond!

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,407
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: N-Queen using Stack

    You are welcome. I am glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. recursion to find a solution for K queen problem (variation)
    By lidor718 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2018, 07:22 AM
  2. what is difference between call stack and stack tace?
    By me_shankara in forum Exceptions
    Replies: 6
    Last Post: October 27th, 2018, 03:23 AM
  3. Cannot push onto a stack
    By colerelm in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 25th, 2013, 06:20 AM
  4. stack
    By ridg18 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 22nd, 2011, 12:45 PM
  5. Stack
    By AmyH in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 7th, 2010, 04:04 PM