this is a revised version of the code that i thought would work better but it is not correctly adding a 1 or eleven to the total. the spot with all the slashes is where i have written up that part of the code.
string player1; // used to know player1's name;
string comp; // used to know player2's name;
string choice1; // used to decide hit or stay
string choice2; // used to decide hit or stay
int player1Total = dealOneCard() + dealOneCard(); //Player 1 is dealt two cards;
int compTotal = dealOneCard() + dealOneCard(); //Player 2 is dealt two cards;
string aceChoice;
int cardCharlie = 0;
// asks and stores name
cout << "enter your name please player 1 " << endl;
cin >> player1;
cin.get();
// test for a blackjack
if (player1Total == 21) {
i = 2;
cout << "congratulations " << player1 << " you win with Blackjack." << endl;
cin.get();
}
if (compTotal == 21) {
cout << "Computer wins with blackjack."<< endl;
cin.get();
}
// shows original hand of player1
while (i == 1) {
cout << "Hello " << player1 << " you currently have a total of " << player1Total << " points. Would you like to hit or stay." << endl;
cin >> choice1;
////////////// handles what player1 wants to do
if (choice1 == "hit") {
player1Total += dealOneCard();
cardCharlie ++;
} if (choice1 == "stay") {
i = 2;
cout << "You still have a total of " << player1Total << " points" << endl;
} if (choice1 == "hit" && dealOneCard() == 11) {
cardCharlie ++;
if (player1Total + 11 < 22) {
player1Total += 11;
cout << "ace is automaticall an 11 " << player1Total;
} else {
player1Total += 1;
cout << "ace is automatically a 1 " << player1Total;
}
}
// handles 5 card charlie
if (cardCharlie == 5) {
cout << player1 << "got 5 card charlie and wins";
}
// handles if player 1 busts
if (player1Total > 21) {
i = 2;
cout << player1 << " has busted. Computer Wins" << endl;
cin.get();
}
}
// descides when the computer should hit
while (compTotal < 17 && compTotal < player1Total && player1Total < 22) {
compTotal = compTotal + dealOneCard();
} if (compTotal > 16) {
}
if (compTotal > 21) {
cout << "Computer has busted with " << compTotal << " points. You win." << endl;
}
// descides who wins
if (player1Total > compTotal && player1Total < 22) {
cout << "Congratulations " << player1 << ". You win." << endl;
} else if (compTotal > player1Total && compTotal < 22) {
cout << "Computer wins with " << compTotal << " points." << endl;
}
// recognizes a tie
if (player1Total == compTotal) {
cout << "Tie Game." << endl;
}
// new hand
player1Total = 0;
player1NumAces = 0;
// when the player is dealt an ace
++player1NumAces;
player1Total += 11;
// after the player is dealt any card
if(player1Total > 21)
{
// player busts
if(player1NumAces > 0)
{
// but they have an ace, so change the Ace to be 1 instead of 11 so they don't bust
player1Total -= 10;
--player1NumAces ;
}
}
if(player1Total > 21)
{
// now they REALLY bust
// ...
}
This way you don't have to bother with asking the user what they want. And also what they want is free to change depending on the next cards they're dealt.
EDIT: fixed to handle the situation where they are dealt multiple aces.