heres a code that i wrote up for blackjack but the part where the computer decides if the ace is a one or 11 isn't working properly. if someone could tell me whats not working right that would be great.
int player1Total = dealOneCard() + dealOneCard(); //Player 1 is dealt two cards;
int compTotal = dealOneCard() + dealOneCard(); //Player 2 is dealt two cards;
int cardCharlie = 0; // used to test for 5 card charlie
// 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) {
i = 2;
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" && dealOneCard() != 11) {
player1Total += dealOneCard();
cardCharlie ++;
} else if (choice1 == "stay") {
i = 2;
cout << "You still have a total of " << player1Total << " points" << endl;
} else if (choice1 == "hit" && dealOneCard() == 11) {
cardCharlie ++;
// handles if ace is 11 or 1
if (player1Total + 11 < 22 && player1Total + 11 != 22) {
player1Total += 11;
cout << "Ace is automaticall an 11 " << endl;
cout << player1Total;
} else {
player1Total += 1;
cout << "Ace is automatically a 1 " << endl;
cout << player1Total << " ";
}
}
// handles 5 card charlie
if (cardCharlie == 5 && player1Total < 22) {
cout << player1 << "got 5 card charlie and wins";
}
// handles if player 1 busts
if (player1Total > 21) {
i = 2;
cout << player1 << " has busted with, "<< player1Total << ". 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;
}
I can probably tell you why this isn't working right. Because there is nothing to indicate if the player was ever actually dealt an 'Ace'. Right now you're just assigning +11 or +1 based on the players current total points.