Search code examples
javamethodsnetbeansbooleanuser-input

Why won't the method return true when conditions are met?


I'm coding a game in Netbeans using the GUI. In the game, a random amount of stones between 15-30 is generated and the user and the computer take turns removing stones between 1-3 until there are none left. The last player that takes the last stones loses. My problem is that my method (validIn()) won't be used in the playerMove(), so even when the conditions are met, the game doesn't update. Here is where I think the problem is:

private boolean validIn(int num){
    //
    return num < 3 || num > 1 || num < totalStone;
}

public void playerMove(){
    // Geting the user input
    int userIn = Integer.parseInt(txtIn.getText());
    // Declaring and assigning a boolean
    boolean check = false;

    // If the boolean returns true
    if (check == true) {
        // Subtracting how much the player took from the total amount of rocks
        totalStone -= userIn;
        // Displaying how many rocks were taken and how many are left
        txtOut.setText(txtOut.getText() +"\nYou picked up " +userIn +" stone(s). There are " +totalStone +" stone(s) left.");                
    }
        // Else,
        else {
            // Assign the boolean check to what the method validIn returns 
            check = validIn(userIn);
        }

}

public void checkWinner(String player){
    // If there are no more stones left,
    if (totalStone == 0){                              
    // Display a message that says who won
    txtOut.setText(txtOut.getText() +"\nThere are no more stones left. "+player +" wins!");
   }  
}

private void btnEnterActionPerformed(java.awt.event.ActionEvent evt) {                                         
    //
    if (totalStone > 0){
        // Display how many rocks there are
        txtOut.setText(txtOut.getText() +"\nThere are " +totalStone +" stone(s).");
        // The player does their move
        playerMove();
        // Checking if the computer won
        checkWinner("The computer");
        // Computer does a turn
        computerMove();
        // Checking if the player won
        checkWinner("The player");            
    }

        //
        else if (totalStone == 0){
            //
            txtOut.setText(txtOut.getText() + "\nThe game has ended.");
        }
}                                        

Solution

    1. int comIn = (int)(Math.random() * 2) + 1;
      // Declaring and assigning a boolean boolean check = false; // If number generated is bigger than the total stones, if (check == true && validIn(userIn) == true){...}

    In above code you save the generated number in comIn but you never check comeIn is bigger than total stones in your code.

    Then in if condition you are checking that is check equals to boolean true, which is not because you gave check a false value just above and never changed it so it will never run the if loop because inside it the first condition is wrong.

    1. private boolean validIn(int num){ return num < 3 || num > 1 || num < totalStone; }

    Here it is returning true no matter what because the num provided is always less than 3 and greater than 1 if you are playing by your rules

    1. `check = false;

      // If the boolean returns true if (check == true) {...} `

    And here again you are setting the value of check false and checking that whether it is true in if loop, which will never run because check will never be true.