Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursive Java, Checking for conflicts between nodes
    text
    copied!<p>I am trying to configure out a method for checking conflicts between the head and all of the other nodes. At this point, I'm getting stuck in a loop. Any idea how to loop through the list, comparing head with head.next .next .next.next and so on?</p> <p><strong>EDIT: After adding a return false; after the conflictCheck(a,b.getNext()), there is no longer the loop problem but the output reflects this method NOT comparing each node with the head node.</strong></p> <p><strong>EDIT X 2! : I believe I have the loop checking for conflicts working fine thanks to everyone who commented/answered. Now I have strange results in my program,where conflicts are being detected but nothing is being done about them. I've added my other important method that deals with these shenanigans. Also, my output is below. Is there any reason why Every node's column is moving to either 3, or staying at 1???</strong></p> <pre><code> public static void playChess() { System.out.println("Playing chess"); if (conflictCheck(head, head.getNext())) { if (head.getColumn() == 8) { queens.pop(); } else if (!queens.isEmpty() &amp;&amp; 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 (queens.size() &lt; 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 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() &amp;&amp; a.getColumn()!=b.getColumn() &amp;&amp; !diagonal(a,b)){ conflictCheck(a,b.getNext()); } else { System.out.println("There is a conflict"); return true; } } return false; } </code></pre> <p>My output</p> <p>Playing chess Comparing 8 ,3 And 7 , 1 <strong>There is a conflict with 8,3 And 6,3</strong> Success The stack 8, 3 7, 1 6, 3 5, 1 4, 3 3, 1 2, 3 1, 1 End of stack</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload