1

I made a blackjack game for my C++ course. I have a problem checking who won the game. During the game I check after each draw whether or not that person busted (exceeded 21 in total). If they did bust I store it in a variable. They are playerBust and dealerBust. They are initialized to 0.

The win-check is a standard if/else-if segment. It says that playerBust == 1 is always false, and that dealerBust == 0 is always true.

However, the last test of the game I logged both of these, and dealerBust = 1 by the end.

A few explanations of my code:

deck.newdeck gives me a new, shuffled deck.

initializeGame(); sets the player's and dealer's hand total to 0.

.toString() simply return a string naming a card, for instance "Ace of Spades".

getPlayerCardValue(...)and getDealerCardValue(...) just evaluates the numerical value of the card 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's total: " << playerHand << endl;
            if (playerHand > 21){
                playerBust = 1;
                stopDraw = 1;
            }
        }
        else if (playerDraw == 0){
            stopDraw = 1;
        }
    }
}

// DEALERS TURN
dealerHand += getDealerCardValue(dealerFaceDown, dealerHand);
cout << "Dealer's face down card is: " << dealerFaceDown.toString() << endl
<< "Dealer's 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's 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's bust: " << playerBust << endl << "Dealer's bust: " << dealerBust << endl;
19
  • 3
    Have you tried just saying dealerBust = true; and checking with if (dealerBust)? Commented Feb 22, 2016 at 22:29
  • 4
    @Auclair: bool is an actual data type in C++, and true and false are actual Boolean keywords. You should be using them in C++ code. It is not recommended to use integers for boolean values and comparisons. That is what people do in C instead, because C does not have a native boolean data type. Things like if (playerDraw == 1) { ... } else if (playerDraw == 0) { ... } should be if (playerDraw) { ... } else { ... } in C++. Commented Feb 22, 2016 at 22:32
  • 3
    @Ben, you should never #define true 1 in C. This is just terribly wrong, and can lead to much headache. Instead, #define false 0 #define true !false.
    – SergeyA
    Commented Feb 22, 2016 at 22:37
  • 3
    @Ben "Why is this wrong?" Because any value other than 0 will evaluate to true. Commented Feb 22, 2016 at 22:54
  • 2
    @SergeyA 1 and !0 are identical, in C
    – M.M
    Commented Feb 22, 2016 at 22:58

2 Answers 2

12

You are using logical or (||) where you actually want to use logical and (&&) instead.

0
2

Consider this:

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

If the first branch is not taken then we know that playerBust is not true, and neither is dealerBust. So in the else if there's no point testing if playerBust is true (it can't be) or if dealerBust is false (it must be).

Not the answer you're looking for? Browse other questions tagged or ask your own question.