2012-03-27 82 views
1

遞歸conflictCheck()方法有問題。現在看來很好。我有測試打印線,看起來不錯。現在,當某些事情發生衝突(返回true)時,我的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 2012-03-27 16:42:41

+0

現在添加,我會編輯我原來的問題 – jackie 2012-03-27 16:43:08

回答

2

由於這是功課,有一個小提示:

您不要在衝突採取行動的理由您發現的原因很可能是因爲您忽略了從boolean conflictCheck(QueenNode a, QueenNode b)遞歸調用的返回值conflictCheck(a,b.getNext());

第012頁的this excellent work by E.W.Dijkstra深入探討了八個女王的問題。它使用ALGOL-60編碼(甚至在我的時間之前就已經過時),但這些想法與語言無關。

+0

我不明白爲什麼它會忽略值,雖然 – jackie 2012-03-27 16:48:58

+0

@JackieAldama返回值被忽略,因爲你調用'getNext()''conflictCheck',但是當布爾值會返回給您,您的代碼既不會返回它,也不會將其分配給局部變量以供進一步分析。 – dasblinkenlight 2012-03-27 16:51:29

+0

雖然打破了這種方法,但每次進行比較時,都應該打印出「比較x和y」,但只打印第一次比較的結果。我知道它是在比較堆棧中的其他人,因爲我可以將它打印爲「與x和z衝突」,因此它正在進行比較。這是超越布爾返回的東西,不是嗎? – jackie 2012-03-27 16:54:27