-1

I am building a simple game of 21. Everything comes together okay, but when I click on the Button that I have assigned to my 'Stand' function, none of the if-statement blocks trigger event though I am meeting conditions of one or the other depending on what cards have already been dealt. I have tested all variations of the statements and I want to have some insight, or a second pair of eyes to see something I do not.

I have tested the function multiple times, and re-written it multiple times. I've tested the function with just that statement present, and it still does not trigger.

This is the function in question:

//when player hits stand button
public void Stand(TextField playerNum, TextField dealerNum, TilePane b, Button hit, Button stand, Button deal, TextField handsLostNum, TextField handsWonNum) {
    //obtain current final scores when player stands
    playerFinal = Integer.parseInt(playerNum.getText());
    dealerFinal = Integer.parseInt(dealerNum.getText());

    if (playerFinal > dealerFinal) {
        hit.setVisible(false);
        stand.setVisible(false);
        deal.setVisible(true);

        playerNum.setText("YOU WIN!");
        dealerNum.setText("YOU WIN!");
        handsWon += 1;
        String temp = Integer.toString(handsWon);
        handsWonNum.setText(temp);
    }

    if (dealerFinal > playerFinal) {
        hit.setVisible(false);
        stand.setVisible(false);
        deal.setVisible(true);

        playerNum.setText("YOU LOSE!");
        dealerNum.setText("YOU LOSE!");
        handsLost += 1;
        String temp = Integer.toString(handsLost);
        handsLostNum.setText(temp);
    }

    if (dealerFinal == playerFinal) {
        playerNum.setText("DRAW! PLAY AGAIN!");
        dealerNum.setText("DRAW! PLAY AGAIN!");
        hit.setVisible(false);
        stand.setVisible(false);
        deal.setVisible(true);
    }
    handsWon = 0;
    handsLost = 0;
} //END STAND METHOD

And the condition that helps to meet it is here:

//method to add scores to text fields
public void addScores(int pScore, int dScore, TextField playerNum, TextField dealerNum) {
    //ADD PLAYER SCORE
    String playerScore = playerNum.getText();
    int playerCurrent = Integer.parseInt(playerScore);
    int newCurrent = playerCurrent + dScore;
    String newScore = Integer.toString(newCurrent);
    playerNum.setText(newScore);

    //ADD DEALER SCORE
    String dealerScore = dealerNum.getText();
    int dealerCurrent = Integer.parseInt(dealerScore);
    int newDealCurrent = dealerCurrent + pScore;
    String newDealScore = Integer.toString(newDealCurrent);
    dealerNum.setText(newDealScore);
}

I add the scores to text fields and then pull them again later in the project. Yet, even when the values are meeting the conditions of being larger than the opponents value, the statement does not trigger.

The expected result is when I click on the 'Stand' button, the statement is triggered and then the variable that adds to the total tally is activated.

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
  • 1
    Are you sure your input is what you think it is? Why do you think none of the `if` blocks are being executed? First thing to do is to use a debugger or add `printlin()` statements after you set dealerFinal and playerFinal to make sure those fields contain what you expect. Then add `System.out.println("Player win / Dealer win / Push ");` as appropriate in each of your if blocks to see if you're getting any debugging output in the console. Your UI just may not be reflecting program state properly. Make sure you run down the actual problem correctly so no one wastes time chasing the wrong thing. – MarsAtomic May 13 '19 at 02:18
  • Are you sure button `on-click` handler is properly assigned? Probably your click handler is not called at all – Alexander Pavlov May 13 '19 at 02:19
  • 1
    It will be more clear and helpful to set breakpoints and debug the program with IDE. – qtopierw May 13 '19 at 02:33
  • 1
    See [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Bohemian May 13 '19 at 02:38
  • 1
    Those ar not event handler methods you posted. We've basically no idea if and how those methods are invoked. Questions need to provide a [mcve]. In this case a sequence of calls for both methods could keep the size of the code to a minimum, but I guess you that's not where the error is... – fabian May 13 '19 at 10:16

1 Answers1

-2

Try putting System.out in every step to check if it is actually getting there. Put one as the first statement in the Stand method like: System.out.println("In Stand method");

Then put more of those before the if statements and inside them like:

System.out.format("Before: playerFinal : %s, dealerFinal: %s, playerFinal > dealerFinal: %d %n", playerFinal, dealerFinal, playerFinal > dealerFinal);
if (playerFinal > dealerFinal) {
System.out.format("In: playerFinal : %s, dealerFinal: %s, playerFinal > dealerFinal: %d %n", playerFinal, dealerFinal, playerFinal > dealerFinal);

Do that for each of the methods, to see if that method is actually running and what the values are.

If you see that the if statements are executing and the flow going into them, but you don't see any changes on the GUI elements, then try using:

Platform.runLater(() -> {
 // Your GUI changes code
  playerNum.setText("YOU WIN!");
  dealerNum.setText("YOU WIN!");
});

Platform.runLater receives a runnable as an argument, and is the right way to update the GUI if you are using JavaFX.

Sometimes you may save the file and run it and the IDE would not actually compile it, running the old code. In that case, you can try restarting the IDE and trying again.

Jeremy Then
  • 525
  • 2
  • 12