简体   繁体   中英

Why does my if linked list empty statement not output anything?

I'm writing a game in java and I have an else statement that checks the length of a linked list of objects, and if it is empty, should return "You win" in the console. Using breakpoints in intelliJ I can see that the code is being executed on each tick of the game, but it never seems to register the condition as true.

The game is a simple rhythm game where the player has to time their button presses to score points, and whether the player misses, or hits the beat, the first instance of the object type is removed from the linked list, so theoretically the list should be empty if the player wins the game. and the console does output a "no such element" exception when checking for beats after the last one is missed or hit.

From this I know that the code is executing and that the linked list is empty. I've also tried several methods for checking whether the linked list is empty.

public void checkBeats(){
        Beat currentObject = (Beat) Game.beatLinkedList.getFirst();
        //Find the x position of the current object
        int currentBeatPosition = currentObject.getX();

        //Condition to award points to the player if they time hitting the beat correctly
        if (currentBeatPosition <= 70) {
            //Add 100 to the score variable.
            Player.lives -= 1;
            Game.beatLinkedList.removeFirst();
            for (GameObject obj : Handler.object) {
                if (obj instanceof Beat) {
                    Handler.object.remove(obj);
                    break;
                }
            }
            checkLives();
        }

        else if (beatLinkedList.isEmpty() == true){
            System.out.println("YOU WIN");
        }
    }

I have also used beatLinkedList.size ==0 , and a few others such as if null and nothing seems to work. Would appreciate it if anyone knew why.

Edit:

Tried with removing the else , and changed Game.beatLinkedList to just beatLinkedList , as I am working within the Game class anyway.

public void checkBeats(){
    Beat currentObject = (Beat) beatLinkedList.getFirst();
    //Find the x position of the current object
    int currentBeatPosition = currentObject.getX();

    //Condition to award points to the player if they time hitting the beat correctly
    if (currentBeatPosition <= 70) {
        //Add 100 to the score variable.
        Player.lives -= 1;
        Game.beatLinkedList.removeFirst();
        for (GameObject obj : Handler.object) {
            if (obj instanceof Beat) {
                Handler.object.remove(obj);
                break;
            }
        }
        checkLives();
    }

    if (beatLinkedList.isEmpty()){
        System.out.println("YOU WIN");
    }
}

Edit 2:

If I change the code to if (beatLinkedList.size() == 1) Then when it reaches the second to last beat, the you win message prints several times in the console, not sure what is going on when it reaches 0.

Can it be, that when the list is empty empty also the first contortion (currentBeatPosition <= 70) is true?

If so, you won't get into the "else" if block in the first place.

In that case just make it

 if (beatLinkedList.isEmpty() == true){
        System.out.println("YOU WIN");
    }

(Drop the "else")

First of all: Change the else if to and if . That way it will check everytime if the players has won.

Second Your code inside the else if might be wrong(?) in almost every line you interact with your beatLinkedList you type Game. infront. However, in your if else you don't use Game.BeatlinkedList , you only use beatLinkedList . Any reason for this?

if (Game.beatLinkedList.isEmpty()){ //you don't need == true.
        System.out.println("YOU WIN");
}

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