"The condition is always true" when I know that it is not

I made a blackjack game for my C ++ course. I have a problem checking who won the game. During the game, I check after every draw, regardless of whether this person was beaten (21 people exceeded). If they did this, I store it in a variable. They playerBustand dealerBust. They are initialized to 0.

The winning check is the standard if else segment. He says that is playerBust == 1always a lie and that is dealerBust == 0always true.

However, the last test of the game I ran both of them dealerBust = 1to the end.

A few explanations of my code:

deck.newdeck gives me a new, shuffled deck.

initializeGame();sets the total amount of the player and dealer 0.

.toString() just return the line denoting the card, for example "Ace of Spades".

getPlayerCardValue(...)and getDealerCardValue(...)just evaluates the numerical value of the map just drawn.

void Blackjack::playGame(){
deck.newDeck();
initializeGame();
drawInitialCards();
bool playerBust = 0;
bool dealerBust = 0;
Card newCard;

// PLAYERS TURN
if (playerHand > 21){
    playerBust = 1;
}
else if (playerHand < 21){
    bool stopDraw = 0;
    while (stopDraw == 0){
        bool playerDraw = askPlayerDrawCard();
        if (playerDraw == 1){
            newCard = deck.drawCard();
            cout << endl << "You drew: " << newCard.toString() << endl;
            playerHand += getPlayerCardValue(newCard);
            cout << "Player total: " << playerHand << endl;
            if (playerHand > 21){
                playerBust = 1;
                stopDraw = 1;
            }
        }
        else if (playerDraw == 0){
            stopDraw = 1;
        }
    }
}

// DEALERS TURN
dealerHand += getDealerCardValue(dealerFaceDown, dealerHand);
cout << "Dealer face down card is: " << dealerFaceDown.toString() << endl
<< "Dealer total: " << dealerHand << endl;

if (dealerHand > 21){
    dealerBust = 1;
}
else if (dealerHand < 21){
    while (dealerHand < 17){
        newCard = deck.drawCard();
        cout << endl << newCard.toString() << endl;
        dealerHand += getDealerCardValue(newCard, dealerHand);
        cout << "Dealer hand totals: " << dealerHand << endl;
        if (dealerHand > 21){
            dealerBust = 1;
        }
    }
}

// WINNING CONDITIONS
if (playerBust == 1 || dealerBust == 1){
    cout << "Tie" << endl;
}
else if (playerBust == 1 || dealerBust == 0){
    cout << "Dealer wins" << endl;
}
else if (playerBust == 0 || dealerBust == 1){
    cout << "Player wins" << endl;
}
else if (playerBust == 0 || dealerBust == 0){
    if (playerHand > dealerHand){
        cout << "Player wins" << endl;
    }
}
cout << endl << "Player bust: " << playerBust << endl << "Dealer bust: " << dealerBust << endl;
+1
source share
2 answers

Consider this:

if (playerBust == 1 || dealerBust == 1){
    cout << "Tie" << endl;
}
else if (playerBust == 1 || dealerBust == 0)

If the first branch is not executed, then we know that playerBustit is not true, and none of them is dealerBust. Thus, there is else ifno point testing if it is playerBusttrue (it cannot be) or if it dealerBustis false (it should be).

+1
source

You use boolean or ( ||) where you really want to use boolean and ( &&).

+11
source

Source: https://habr.com/ru/post/1629974/


All Articles