简体   繁体   中英

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("-");
    }
}

}

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM