Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By jakaldama

Thread: Stuck in Infinite Loop, Please!

  1. #1
    jakaldama is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Unhappy Stuck in Infinite Loop, Please!

    I'm getting stuck in a loop, and I don't know how to modify my code to fix the issue. I'm dealing wiht the 8-queens problem nad think I have the logic all figured out, but after placing the second queen and adjusting it so there are no possible conflicts, everything just dies instead of moving to the next queen to be placed

    These are the two important methods. I really appreciate any feedback, I'm completely stuck and getting frustrated.

    EDIT:
    I think the problem must be the conflictCheck method. Now that it's not a conflict, it's dying out

    Java Code:
    	public static boolean conflictCheck() {
    		QueenNode temp = head;
    		//walk through stack and check for conflicts
    
    		while(temp!=null) {
    			//if there is no next node, there is no conflict with it
    			if (temp.getNext() == null){
    				System.out.println("No next node");
    				if (queens.size() < 8 ) {
    					System.out.println("No problems");
    					return false;
    				}
    			}
    			else if (temp.getRow() ==temp.getNext().getRow() || temp.getColumn() == temp.getNext().getColumn() ||
    					diagonal(temp, temp.getNext())){
    				System.out.println("There's a conflict");
    				return true;
    			}
    		}
    		return false;
    	}
    	
    	public static void playChess() {
    		System.out.println("Playing chess");
    		if (conflictCheck()) {
    			if (head.getColumn() == 8) {
    				queens.pop();
    			}
    			else if (!queens.isEmpty() && head.getColumn()!= 8) {
    				System.out.println("Adjusting head");
    				head.setColumn(head.getColumn()+1);
    				System.out.println("Head is now " + head.getRow() + ", " + head.getColumn());
    				playChess();
    				
    			}
    		}
    		
    		else if (!conflictCheck() & queens.size() < 8) {
    			System.out.println("Stack isn't full yet");
    			queens.push(queens.size()+1,1);
    			queens.viewPieces();
    			playChess();
    			}
    		else {
    			success= true;
    			System.out.println("Success");
    			queens.viewPieces();
    			return;
    		}
    	}
    	
    	public static void main(String[] args) {
    		queens.push(1, 1);
    		queens.viewPieces();
    		success = false;
    		playChess();
    	}
    }
    Last edited by jakaldama; 03-27-2012 at 05:59 PM.

  2. #2
    jakaldama is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: Stuck in Infinite Loop, Please!

    Figured out why I was getting stuck in the loop. I needed another else statement for when there isn't a conflict!! XD
    But now, of course, the final result is wrong anyway:

    Java Code:
    Success
    The stack
    8, 3
    7, 1
    6, 3
    5, 1
    4, 3
    3, 1
    2, 3
    1, 1
    End of stack
    noobplus likes this.

  3. #3
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Stuck in Infinite Loop, Please!

    Quote Originally Posted by jakaldama View Post
    Figured out why I was getting stuck in the loop. I needed another else statement for when there isn't a conflict!! XD
    good show dude

    Quote Originally Posted by jakaldama View Post
    But now, of course, the final result is wrong anyway:
    could I know which part of the code goes wrong.. I'm pretty good in if else codings

    regards
    dhilip

  4. #4
    jakaldama is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: Stuck in Infinite Loop, Please!

    The results is wrogg because my conflictCheck is only checking the head with the second node. Head needs to be compared with everything.

    So i'm trying to do something like this:

    Java Code:
    	public static boolean conflictCheck(QueenNode a, QueenNode b) {
    		QueenNode temp = head;
    		//if there's just one node, return false
    		if (queens.size()== 1)
    			return false;
    		//if head doesnt conflict with head.next queen, then compare head with following, if there is one
    		if (temp.getRow()!=temp.getNext().getRow() && temp.getColumn() != temp.getNext().getColumn() &&
    					!diagonal(temp, temp.getNext())) {
    			if (b.getNext() != null){
    				conflictCheck(head, b.getNext());
    			}
    			//at the end  of the stack, no conflicts
    			else return false;
    		}
    		
    		return true;
    	}

  5. #5
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Stuck in Infinite Loop, Please!

    Quote Originally Posted by jakaldama View Post
    The results is wrogg because my conflictCheck is only checking the head with the second node. Head needs to be compared with everything.
    I think, "temp.getNext()" stores the node into 'temp' itself, i.e., "temp.getNext()" needs to be stored into a new temporary variable //edited:that was wrong comment sorry, working on that..


    regards
    dhilip
    Last edited by noobplus; 03-27-2012 at 06:42 PM.

  6. #6
    jakaldama is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: Stuck in Infinite Loop, Please!

    I was walking through the list and compare each pair but I think I need to compare the head with every node. return false if no conflict after reaching the end, or true once a conflict is picked up so that the head can be refactored. Any idea how to go about that?

  7. #7
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Stuck in Infinite Loop, Please!

    Quote Originally Posted by jakaldama View Post
    The results is wrogg because my conflictCheck is only checking the head with the second node. Head needs to be compared with everything.
    I've got you..

    Java Code:
    else if (temp.getRow() ==temp.getNext().getRow() || temp.getColumn() == temp.getNext().getColumn() ||
                        diagonal(temp, temp.getNext())){
                    System.out.println("There's a conflict");
                    return true;
    //Edited:post#1 line 14,15,16

    the while() loop doesn't increment
    unless the 'temp' is incremented, it doesn't go to check the next node..
    example,
    before while loop ends, add
    Java Code:
    temp=temp.getNext();

    regards,
    dhilip
    Last edited by noobplus; 03-27-2012 at 06:56 PM.

  8. #8
    jakaldama is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: Stuck in Infinite Loop, Please!

    Yeah but that's moving the head pointer. I want to compare the very top of the list (which is really the head) to every node following it. Kind of like this, but it's not working XD

    Java Code:
    	public static boolean conflictCheck(QueenNode a, QueenNode b) {
    		//checks for conflicts between head and all other nodes in the stack
    		a= head;
    		while (b != null) {
    			if (a.getRow()!=b.getRow() && a.getColumn()!=b.getColumn() && !diagonal(a,b)){
    				conflictCheck(a,b.getNext());
    				return false;
    			}
    			else {
    				System.out.println("There is a conflict");
    				return true;
    			}
    		}
    		return false;
    	}

  9. #9
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Stuck in Infinite Loop, Please!

    Quote Originally Posted by jakaldama View Post
    Yeah but that's moving the head pointer.
    easy.. store only incremented one to a new variable, and put the new variable into the while loop

    regards
    dhilip

  10. #10
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Stuck in Infinite Loop, Please!

    example,
    Java Code:
    nexttemp=temp.getNext();
    while(nexttemp!=NULL)
    {
     if(temp.getRow()==nexttemp.getRow())
        {
            //
         }
    if(tempnext.getNext()!=NULL) //forgot to add this. maybe this needs to be checked
    nexttemp=temp.getNext();
    }
    Last edited by noobplus; 03-27-2012 at 08:11 PM.

  11. #11
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Stuck in Infinite Loop, Please!

    Hi,
    I edited the previous post (post #10)
    (ive added a new line to the coding line no.8)

    regards
    dhilip

  12. #12
    jakaldama is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: Stuck in Infinite Loop, Please!

    I am testing this right now, which seems to be doing it's job but now I am having problems elsewhere!! argh

    Java Code:
    	public static boolean conflictCheck(QueenNode a, QueenNode b) {
    		//checks for conflicts between head and all other nodes in the stack
    		if (b == null) {
    			System.out.println("Attempting Conflict Check: Nothing to Compare to");
    			return false;
    		}
    		
    		if (a.getRow()!=b.getRow() && a.getColumn()!=b.getColumn() && !diagonal(a,b)){
    			System.out.println("Comparing " + a.getRow() + " ," + a.getColumn() +
    										" And " + b.getRow() + " , " + b.getColumn());
    			conflictCheck(a,b.getNext());
    		}
    		else {
    			System.out.println("There is a conflict with " +a.getRow() + "," + a.getColumn()
    								+ " And " + b.getRow() + "," + b.getColumn());
    			return true;
    			}
    		return false;
    	}

  13. #13
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Stuck in Infinite Loop, Please!

    Quote Originally Posted by jakaldama View Post
    I am testing this right now, which seems to be doing it's job but now I am having problems elsewhere!! argh
    it doesn't have a while loop, so, it can be called in a loop during checking process, like,
    Java Code:
    for(QueenNode a=temp.getNext();a<temp.getLast();a=a.getNext()) //or maybe a while loop if ".getLast()" is not in the class
    {
      conflictCheck(temp,a);
    }
    I meant it in the sense, the conflictCheck() doesn't have while loop, rather compares.
    Edited to add:(ur conflictCheck() in OP is different from that in ur previous code)

    regards
    dhilip
    Last edited by noobplus; 03-27-2012 at 07:30 PM.

  14. #14
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Stuck in Infinite Loop, Please!

    all the problems u have is in checking the head of the temp..

    either u create while loop inside the conflictCheck() method, or postpone it to checking it in main() method(ur recent code (a,b)),
    you should create a new variable (maybe this word would be clear 'tempSuccessor' forgot to add this smiley seriously!)

    example, (while loop inside the method)
    Java Code:
    tempSuccessor=temp.getNext();
    while(nexttemp!=NULL)
    {
     if(temp.getRow()==tempSuccessor.getRow())
        {
            //
         }
    if(temp.getNext()!=NULL) //forgot to add this. maybe this needs to be checked
    tempSuccessor=temp.getNext();
    }
    //same code as post #10
    regards
    dhilip
    Last edited by noobplus; 03-27-2012 at 07:51 PM.

  15. #15
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: Stuck in Infinite Loop, Please!

    hi,
    mi sorry, u would still get errors,
    change 'temp' (post 14 line 8) into 'tempSuccessor'
    and it will work fine

    regards
    dhilip

Similar Threads

  1. Infinite loop
    By F.S. in forum New To Java
    Replies: 3
    Last Post: 03-09-2012, 01:52 PM
  2. Stuck in a infinite loop
    By Pojahn_M in forum New To Java
    Replies: 8
    Last Post: 08-23-2011, 03:17 AM
  3. how to end infinite loop
    By search4survival in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 09:59 AM
  4. Infinite loop
    By jDennis79 in forum New To Java
    Replies: 7
    Last Post: 08-14-2010, 12:45 AM
  5. Infinite Loop
    By bosoxfan in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 02:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •