A buddy and I have been working together to try and create a blackjack game for a project we are doing, and while we have a couple of issues we are trying to get worked out, for some reason we are perplexed as to how we can keep score.
Right now, it keeps score but it doesn't reset when it adds a card. So if you had 14 and you hit, and added 3; you would have 31 instead of 17.
Any help to point us in the right direction would be appreciated, I'm a complete C++ noob.
// initialize players and hands
srand(static_cast<unsignedint>(time(0)));
GetPlayerInfo(players);
do
{
int score = 0;
bool busted = (score > 21);
ShuffledDeck(deck);
DealHands(deck, players, 2);
// show the cards
cout << players[0].id << ":\n";
for (unsignedint i = 0; i < players[0].hand.size(); ++i)
{
WriteCard(players[0].hand[i]);
cout << endl;
score += Value(players[0].hand[i]);
}
cout << "Current Score: " << score << endl;
if (busted)
{
cout << players[0].id << " busted.\n";
return;
}
selection = (GetValidChoice("Enter (H)it or (S)tand: ", "HhSs"));
//add switch case
//stand calls evaluate hand function
// wasn't able to get the hit option to deal or write new card to hand
switch (selection)
{
case'h':
DealHands(deck, players, 1);
cout << players[0].id << ":\n";
for (unsignedint i = 0; i < players[0].hand.size(); ++i)
{
WriteCard(players[0].hand[i]);
cout << endl;
score += Value(players[0].hand[i]);
}
cout << "Current Score: " << score << endl;
DisplayResults(players);
break;
case's':
DisplayResults(players);
default:
break;
}
// asks to play again at end of hand
cout << "Do you want to play another hand? (Y)es or (N)o?\n";
cin >> playagain;
if (playagain == 'y')
{
PlayTheGame();
reset = true;
}
else
{
reset;
}
} while (reset);
return HoldConsole();
}
Your for loop on line 39 starts at the 0th card in player 1's hand, causing the loop to readd the original two cards dealt before adding the value of the third. Compare that for loop to the one on line 13 (where you total the value of the player's first two cards) for a bit more clarification.