2012-03-27 11 views
1

재귀적인 conflictCheck() 메소드에 문제가있었습니다. 이제 괜찮아 보입니다. 나는 시험을위한 인쇄 라인을 가지고있다. 자, 무언가 충돌 (사실을 반환), 내 playChess() 메서드는 노드의 두 번째 변수를 조정하여 충돌을 해결하기로되어 있습니다.Java 루프가 수행중인 작업을 수행하지 않습니다

내 결과는 그대로입니다 :

1,1

2,3

3,1

4,3

5,1 ...

출력에 "충돌이 있습니다."라고 표시 되더라도 작동하지 않습니다. 그것에는 X를, 도달하면 3

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; 
} 


    public static void playChess() { 
    System.out.println("Playing chess"); 
    //Either there is a conflict between head and another node, the stack isn't full, or we have solution 
    if (conflictCheck(head, head.getNext())) { 
     if (head.getColumn() == 8) { 
      queens.pop(); 
     } 
     else if (!queens.isEmpty()) { 
      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; 
    } 
} 

전체 출력 : 당신이 충돌에서 행동하지

이유 :이 숙제이기 때문에

The stack 
1, 1 
End of stack 
Playing chess 
Attempting Conflict Check: Nothing to Compare to 
Stack isn't full yet 
The stack 
2, 1 
1, 1 
End of stack 
Playing chess 
There is a conflict with 2,1 And 1,1 
Adjusting head 
Head is now 2, 2 
Playing chess 
problem 
There is a conflict with 2,2 And 1,1 
Adjusting head 
Head is now 2, 3 
Playing chess 
Comparing 2 ,3 And 1 , 1 
Attempting Conflict Check: Nothing to Compare to 
Stack isn't full yet 
The stack 
3, 1 
2, 3 
1, 1 
End of stack 
Playing chess 
Comparing 3 ,1 And 2 , 3 
There is a conflict with 3,1 And 1,1 
Stack isn't full yet 
The stack 
4, 1 
3, 1 
2, 3 
1, 1 
End of stack 
Playing chess 
There is a conflict with 4,1 And 3,1 
Adjusting head 
Head is now 4, 2 
Playing chess 
problem 
There is a conflict with 4,2 And 3,1 
Adjusting head 
Head is now 4, 3 
Playing chess 
Comparing 4 ,3 And 3 , 1 
There is a conflict with 4,3 And 2,3 
Stack isn't full yet 
The stack 
5, 1 
4, 3 
3, 1 
2, 3 
1, 1 
End of stack 
Playing chess 
Comparing 5 ,1 And 4 , 3 
There is a conflict with 5,1 And 3,1 
Stack isn't full yet 
The stack 
6, 1 
5, 1 
4, 3 
3, 1 
2, 3 
1, 1 
End of stack 
Playing chess 
There is a conflict with 6,1 And 5,1 
Adjusting head 
Head is now 6, 2 
Playing chess 
problem 
There is a conflict with 6,2 And 5,1 
Adjusting head 
Head is now 6, 3 
Playing chess 
Comparing 6 ,3 And 5 , 1 
There is a conflict with 6,3 And 4,3 
Stack isn't full yet 
The stack 
7, 1 
6, 3 
5, 1 
4, 3 
3, 1 
2, 3 
1, 1 
End of stack 
Playing chess 
Comparing 7 ,1 And 6 , 3 
There is a conflict with 7,1 And 5,1 
Stack isn't full yet 
The stack 
8, 1 
7, 1 
6, 3 
5, 1 
4, 3 
3, 1 
2, 3 
1, 1 
End of stack 
Playing chess 
There is a conflict with 8,1 And 7,1 
Adjusting head 
Head is now 8, 2 
Playing chess 
problem 
There is a conflict with 8,2 And 7,1 
Adjusting head 
Head is now 8, 3 
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 
+0

전체 출력을 표시하십시오. – talnicolas

+0

지금 추가하기, 원래 질문을 편집하겠습니다. – jackie

답변

2

, 작은 힌트가있다 검색하는 값은 대체로 boolean conflictCheck(QueenNode a, QueenNode b)에서 호출하는 conflictCheck(a,b.getNext());의 반환 값을 무시하기 때문에 발생합니다.

여덟 명의 여왕 문제는 7235 페이지의 this excellent work by E.W.Dijkstra에서 자세히 살펴 봅니다. ALGOL-60으로 코딩되었지만 아이디어는 언어에 구애받지 않습니다.

+0

왜 값을 무시할 지 이해하지 못합니다. – jackie

+0

@JackieAldama'getNext()'에서'conflictCheck'를 호출하기 때문에 반환 값이 무시되지만, boolean이 다시 돌아 오면 코드는이를 반환하지 않으며 추가 분석을 위해 로컬 변수에 할당하지 않습니다. – dasblinkenlight

+0

그러나 그 방법을 깨고, 비교가 일어날 때마다, "x와 y를 비교하는 것"을 인쇄해야하지만 첫 번째 비교시에만 그것을 출력합니다. 나는 스택의 다른 것들을 비교한다는 것을 안다. 왜냐하면 나는 "x와 z와의 충돌"을 인쇄 할 수 있기 때문에 비교를하고있다. 그것은 부울 반환을 넘어서는 무언가입니다. – jackie

관련 문제