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;
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;
}
}
}
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;
}
}
}
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;
source
share