NoviceProgrammer123
NoviceProgrammer123

Reputation: 11

Can't find cause of infinite loop

My code is causing an infinite loop, I just can't find where and it's driving me insane. Any help on this? It's gotta be in one of the three methods below. It involves one linked-list. Any help would be awesome. Thanks

 public boolean checkNotSamePlacements() {

    Link current = head;

    while (current != null) {

        Link current2 = head;

        while (current2 != null) {

            if (current != current2) {

                if (current.piece.col == current2.piece.col && current.piece.row == current2.piece.row) {

                    return true;
                }
            }
            current2 = current2.next;
        }
        current = current.next;
    }

    return false;
}


public void checkAttacking () {
    boolean foundPieces = false;
    Link current = head;

    while (current != null) {

        Link current2 = head;
        while (current2 != null) {

            if (current != current2) {

                if ((current.piece.isAttacking(current2.piece)) && foundPieces == false) {

                    System.out.print(current.piece.pieceType + " " + current.piece.col +
                    " " + current.piece.row + " " + current2.piece.pieceType +
                    " " + current2.piece.col + " " + current2.piece.row);
                    foundPieces = true;
                }
            }
            current2 = current2.next;
        }
        current = current.next;
    }
    if (foundPieces == false) {
        System.out.print("-");
    }
}

public void checkSpotFound (int col, int row) {

    boolean foundPiece = false;
    Link current = head;

    while (current != null) {

        if (current.piece.col == col && current.piece.row == row) {

            System.out.print(current.piece.pieceType);

            foundPiece = true;
        }
    }

    if (foundPiece == false) {
        System.out.print("-");
    }
}

}

Upvotes: 0

Views: 60

Answers (1)

Naman
Naman

Reputation: 31878

The current in your code is never updated -

while (current != null) {

    if (current.piece.col == col && current.piece.row == row) {

        System.out.print(current.piece.pieceType);

        foundPiece = true;
    }

    current = current.next; // you might want to add this to your code
}

Upvotes: 2

Related Questions