c++if-statementunless

How can I make an "Unless condition" in an IF statement c++


I'm making blackjack in C++ and I'm nearing the end! I'm making my winning and loss statements. But my issue is that I need my if statements to determine not only whether or not the dealer's/players hand is greater or less than one another it needs to check if either has gone over 21.

So having the if statements determine that isn't the problem I just need it to not go through one if statement if the other is over 21

       if (dealerTotal > 21 || playerTotal > dealerTotal)//For win
        {

        }
        if (playerTotal > 21 || dealerTotal > playerTotal)// For lose
        {

        }

So one the first one it needs to go through that IF UNLESS "playerTotal is over 21" and the opposite for the next IF

Any suggestions would be greatly appreciated. Thank you so much!


Solution

  • In Blackjack, if both the player and the dealer get more than 21 (which is called "busting"), then the player loses and the dealer wins. Therefore, you must design the logic accordingly:

    if (
        //player busted, or
        playerTotal > 21 ||
    
        //dealer not busted and dealer has more points than player
        ( dealerTotal <= 21 && dealerTotal > playerTotal )
    )
    {
        printf( "Dealer wins!\n" );
    }
    else if ( dealerTotal == playerTotal )
    {
        printf( "Tie!\n" );
    }
    else
    {
        printf( "Player wins!\n" );
    }
    

    Note that the parentheses in the first if condition are not necessary, because && has higher operator precedence than ||. I only added them for clarity.

    This logic does not take into account that a blackjack 21 wins against a non-blackjack 21. If one side has blackjack and the other side has a non-blackjack 21, then the logic above will incorrectly report a tie. This issue cannot be fixed without seeing more of your code, because you are only presenting the variables playerTotal and dealerTotal, which do not specify whether one side has blackjack.