Java Loop не делает то, что должен делать - PullRequest
1 голос
/ 27 марта 2012

Возникла проблема с рекурсивным методом конфликтом ().Это выглядит хорошо сейчас.У меня есть строки для тестирования, которые хорошо выглядят.Теперь, когда что-то является конфликтом (возвращает true), мой метод playChess () должен исправить конфликт, настроив вторую переменную узла и т. Д.

Мой результат буквально:

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

1 Ответ

2 голосов
/ 27 марта 2012

Поскольку это домашняя работа, есть небольшая подсказка:

Причина, по которой вы не действуете при обнаружении конфликта, наиболее вероятна, поскольку вы игнорируете возвращаемое значение conflictCheck(a,b.getNext());, которое вы вызываете рекурсивно изboolean conflictCheck(QueenNode a, QueenNode b).

Проблема восьми королев подробно рассматривается в этой превосходной работе EWDijkstra на стр. 72. Она закодирована в ALGOL-60 (которая устарела еще до моего времени), но идеи не зависят от языка.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...