Hi, I'm putting together a Blackjack program (yes it's homework, no I'm not looking for answers, just a hint or pointer in the right direction.
The issue is that the player_total variable gets displayed as 1 as part of the cout statement in the win loop. This obviously makes no sense, because the player_total has to equal 21 to even enter into the loop in the first place, so it should display 21. Prior iterations of the hands will correctly show the total (so it might be 13, then 19, and then suddenly it's 1). What's also odd is that my win loop is basically a mirror of my bust loop, but the bust loop displays the total just fine.
int main()
{
//Define variables
int player_card1
, player_card2
, player_card_hit //Separate variable for the card hit value
, player_total;
//Use named constants for max and min card values
constint minvalue = 1
, maxvalue = 10;
char hit
, repeat_game = 'Y'; //Initializing repeat value to begin game
while (repeat_game == 'Y' || repeat_game == 'y')
{
player_total = 0; //Initialize total to 0 at start of game
//Generate initial card values and total
srand(time(0));
player_card1 = (rand() % (maxvalue - minvalue) + 1) + minvalue; //Restricting card values between 1 and 10
player_card2 = (rand() % (maxvalue - minvalue) + 1) + minvalue;
player_total = player_card1 + player_card2;
//Display card values and total
cout << "First cards: " << player_card1 << ", " << player_card2 << endl;
cout << "Total: " << player_total << endl;
cout << "Do you want another card? (y/n)" << endl;
cin >> hit;
//HIT loop: repeat as long as player hits
while (hit == 'Y' || hit == 'y')
{
//Generate hit card value and new total
player_card_hit = (rand() % (maxvalue - minvalue) + 1) + minvalue;
player_total = player_total + player_card_hit;
//KEEP PLAYING loop as long as total is less than 21
while (player_total < 21 & (hit == 'Y' || hit =='y')) //Have to account for both the total value and whether player wants to hit
{
//Display hit card value and new total
cout << "Card: " << player_card_hit << endl;
cout << "Total: " << player_total << endl;
cout << "Do you want another card? (y/n)" << endl;
cin >> hit;
//Recalculate player total to determine which loop triggers next
player_card_hit = (rand() % (maxvalue - minvalue) + 1) + minvalue;
player_total = player_total + player_card_hit;
}
//BUST LOOP when total is greater than 21
while (player_total > 21 & (hit == 'Y' || hit == 'y'))
{
//Display card value, total, and bust message
cout << "Card: " << player_card_hit << endl;
cout << "Total: " << player_total << endl;
cout << "Bust." << endl;
//Reset hit and total values to end bust loop
player_total = 0;
hit = 'n';
}
//WIN LOOP if total = 21 and player wins
while (player_total = 21 & (hit == 'Y' || hit == 'y'))
{
//Display card value, total, and win message
cout << "Card: " << player_card_hit << endl;
cout << "Total: " << player_total << endl;
cout << "Win." << endl;
//Reset hit and total values to end bust loop
player_total = 0;
hit = 'n';
}
}
//Ask if player wants to repeat
cout << "Would you like to play again? (y/n)" << endl;
cin >> repeat_game;
}
return 0;
}
Actual output:
First cards: 4, 9
Total: 13
Do you want another card? (y/n)
y
Card: 6
Total: 19
Do you want another card? (y/n)
y
Card: 2
Total: 1 *HERE IS THE ISSUE, THIS SHOULD DISPLAY 21*
Win.
Would you like to play again? (y/n)