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;