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?
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.
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???
public static void playChess() {
System.out.println("Playing chess");
if (conflictCheck(head, head.getNext())) {
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 (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 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());
}
else {
System.out.println("There is a conflict");
return true;
}
}
return false;
}
My output
Playing chess
Comparing 8 ,3 And 7 , 1
There is a conflict with 8,3 And 6,3
Success
The stack
8, 3
7, 1
6, 3
5, 1
4, 3
3, 1
2, 3
1, 1
End of stack
You’re mixing the cycle with the recursive call.
If I understand you correctly, you would want something like:
I didn’t run the code, so I’m hoping you get the picture, there’s probably a syntax error or 4.