# Stuck in Infinite Loop, Please!

• 03-27-2012, 05:44 PM
jakaldama
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

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();         } }```
• 03-27-2012, 06:05 PM
jakaldama
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:

Code:

```Success The stack 8, 3 7, 1 6, 3 5, 1 4, 3 3, 1 2, 3 1, 1 End of stack```
• 03-27-2012, 06:10 PM
noobplus
Re: Stuck in Infinite Loop, Please!
Quote:

Originally Posted by jakaldama
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
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
• 03-27-2012, 06:19 PM
jakaldama
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:

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;         }```
• 03-27-2012, 06:32 PM
noobplus
Re: Stuck in Infinite Loop, Please!
Quote:

Originally Posted by jakaldama
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
• 03-27-2012, 06:38 PM
jakaldama
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?
• 03-27-2012, 06:53 PM
noobplus
Re: Stuck in Infinite Loop, Please!
Quote:

Originally Posted by jakaldama
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..

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,
Code:

`temp=temp.getNext();`

regards,
dhilip
• 03-27-2012, 06:55 PM
jakaldama
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

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;         }```
• 03-27-2012, 06:59 PM
noobplus
Re: Stuck in Infinite Loop, Please!
Quote:

Originally Posted by jakaldama
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
• 03-27-2012, 07:03 PM
noobplus
Re: Stuck in Infinite Loop, Please!
example,
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(); }```
• 03-27-2012, 07:08 PM
noobplus
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
• 03-27-2012, 07:15 PM
jakaldama
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

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;         }```
• 03-27-2012, 07:28 PM
noobplus
Re: Stuck in Infinite Loop, Please!
Quote:

Originally Posted by jakaldama
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,
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
• 03-27-2012, 07:41 PM
noobplus
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' :P: forgot to add this smiley seriously!)

example, (while loop inside the method)
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
• 03-27-2012, 07:53 PM
noobplus
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