Question
Hello I have a program that plays a simple version of blackjack in c++ and I'm running into problems and I was hoping if anyone
Hello I have a program that plays a simple version of blackjack in c++ and I'm running into problems and I was hoping if anyone can fix it ?
The first problem is I don't know in the playHand() function how to return 1 to represent win , -1 to represent loss , and 0 to represent tie
I have coded all the situations where the player win or dealer win or tie situations and a counter that update if the conditions are met and return those values.
The second problem is to get the program to play again I tried a few things but unable to get the working
I hope to display the wins loss and ties when the user enter n or N to not play again.
I WILL UPVOTE ALL SUITABLE SOLUTIONS THANK YOU!
#include
#include
#include
#include
#include
#include
using namespace std;
// Constants
const int win = 1;
const int lose = -1;
const int tie = 0;
// Function Prototypes
void welcomeMessage();
void displayCard(int card);
int getCard();
int cardScore(int card);
int deal(bool isPlayer, bool isShown);
void hitOrStand(char choice, int playerScore, int dealerScore);
int playHand(int playerScore, int dealerScore);
int playerWin(int playerScore, int dealerScore);
int dealerWin(int playerScore, int dealerScore);
int tieSituation(int playerScore, int dealerScore);
void results(int winResult, int lossResult, int tieResult);
int main()
{
// greeting message
welcomeMessage();
// initate random seed
srand(time(NULL));
bool playerTurn = true;
bool show = true;
int cardValueScore = 0;
int playerCard1 = getCard();
playerCard1 = cardScore(playerCard1);
deal(playerTurn, show);
displayCard(playerCard1);
int dealerCard = getCard();
dealerCard = cardScore(dealerCard);
playerTurn = false;
deal(playerTurn, show);
displayCard(dealerCard);
int playerCard2 = getCard();
playerCard2 = cardScore(playerCard2);
playerTurn = true;
deal(playerTurn, show);
displayCard(playerCard2);
show = false;
deal(playerTurn, show);
// spacing
cout
// scores
int playerScore = playerCard1 + playerCard2;
int dealerScore = dealerCard;
playerTurn = true;
show = true;
deal(playerTurn, show);
cout
playerTurn = false;
deal(playerTurn, show);
cout
// spacing
cout
// h or s
char choice;
cout
cin >> choice;
hitOrStand(choice, playerScore, dealerScore);
// dont knot what to do with this part
// char playAgain;
// cout
// cin >> playAgain;
// // scores
return 0;
}
void welcomeMessage()
{
cout
cout
}
void displayCard(int card)
{
string mystr = "";
string suit = "";
if (card == 1) {
mystr += "Ace of";
}
if (card == 2) {
mystr += "Two of";
}
if (card == 3) {
mystr += "Three of";
}
if (card == 4) {
mystr += "Four of";
}
if (card == 5) {
mystr += "Five of";
}
if (card == 6) {
mystr += "Six of";
}
if (card == 7) {
mystr += "Seven of";
}
if (card == 8) {
mystr += "Eight of";
}
if (card == 9) {
mystr += "Nine of";
}
if (card == 10) {
mystr += "Ten of";
}
if (card == 11) {
mystr += "Jack of";
}
if (card == 12) {
mystr += "Queen of";
}
if (card == 13) {
mystr += "King of";
}
int cardSuit = 0;
cardSuit = (rand() % 4) + 1;
if (cardSuit == 1) {
suit += " Clubs ";
}
if (cardSuit == 2) {
suit += " Diamonds ";
}
if (cardSuit == 3) {
suit += " Hearts ";
}
if (cardSuit == 4) {
suit += " Spades ";
}
cout
}
int getCard()
{
int cardValue = (rand() % 13) + 1;
return cardValue;
}
int cardScore(int card)
{
int cardValue = 0;
if (card == 1) {
cardValue = 11;
}
else if (card == 11 || card == 12 || card == 13) {
cardValue = 10;
}
else {
cardValue = card;
}
return cardValue;
}
int deal(bool isPlayer, bool isShown)
{
int cardValueScore = 0;
if (isPlayer == true && isShown == true) {
cout
}
else if (isPlayer == false && isShown == true) {
cout
}
else {
cout
}
return cardValueScore;
}
void hitOrStand(char choice, int playerScore, int dealerScore)
{
bool playerTurnz = true;
bool showz = true;
int playerCard3 = 0;
int dealerCard2 = 0;
while (choice == 'H' || choice == 'h') {
playerCard3 = getCard();
cardScore(playerCard3);
deal(playerTurnz, showz);
displayCard(playerCard3);
dealerCard2 = getCard();
cardScore(dealerCard2);
// spacing
cout
playerTurnz = false;
deal(playerTurnz, showz);
cout
playerTurnz = true;
deal(playerTurnz, showz);
playerScore += playerCard3;
cout
playerTurnz = false;
deal(playerTurnz, showz);
dealerScore += dealerCard2;
cout
playerWin(playerScore, dealerScore);
cout
break;
cout
cin >> choice;
playerWin(playerScore, dealerScore);
}
if (choice == 'S' || choice == 's') {
cout
playerTurnz = false;
deal(playerTurnz, showz);
cout
dealerCard2 = getCard();
cardScore(dealerCard2);
playerTurnz = true;
deal(playerTurnz, showz);
playerScore = playerScore;
cout
playerTurnz = false;
deal(playerTurnz, showz);
dealerScore += dealerCard2;
cout
playHand(playerScore,dealerScore);
}
}
int playHand(int playerScore, int dealerScore)
{
if (playerWin(playerScore, dealerScore)){
return 1;
}
else if (dealerWin(playerScore, dealerScore)){
return -1;
}
else{
return 0;
}
}
int playerWin(int playerScore, int dealerScore)
{
int winResult = 0;
if (playerScore == 21 && dealerScore
cout
cout
cout
winResult += 1;
}
else if (playerScore dealerScore) {
cout
cout
cout
winResult += 1;
}
return winResult;
}
int dealerWin(int playerScore, int dealerScore)
{
int lossResult = 0;
if (playerScore > 21 && dealerScore
cout
cout
cout
lossResult += 1;
}
if (playerScore playerScore) {
cout
cout
cout
lossResult += 1;
}
return lossResult;
}
int tieSituation(int playerScore, int dealerScore)
{
int tieResult = 0;
if (playerScore == dealerScore && playerScore != 21) {
cout
cout
cout
tieResult += 1;
}
return tieResult;
}
void results(int winResult, int lossResult, int tieResult ){
cout
cout
cout
cout
}
expected output
Microsoft Visual Stud sample program output BLACKJACK Do you want to play a hand of blackjack (y to play)? y PLAYER was dealt five of hearts DEALER was dealt three of spades PLAYER was dealt nine of hearts DEALER was dealt a hidden card PLAYER score - 14 DEALER score - 3 Enter n to hit or s to stand: h PLAYER was dealt eight of diamonds DEALER reveals its second card PLAYER score - 22 DEALER score - 13 Bust! You lose. Do you want to play another hand (y to play)? y PLAYER was dealt seven of hearts DEALER was dealt four of clubs PLAYER was dealt three of hearts DEALER was dealt a hidden card PLAYER score = 10 DEALER score - 4 Enter h to hit or s to stand: h PLAYER was dealt nine of diamonds PLAYER score - 19 DEALER score - 4 Enter h to hit or s to stand: s DEALER reveals its second card PLAYER score - 19 DEALER score - 12 DEALER was dealt ace of hearts PLAYER score - 19 DEALER score - 23 Dealer is bust, you win! Do you want to play another hand (y to play)? y PLAYER was dealt king of clubs DEALER was dealt queen of clubs PLAYER was dealt seven of clubs DEALER was dealt a hidden card PLAYER score - 17 DEALER score - 10 Enter h to hit or s to stand: s DEALER reveals its second card PLAYER score - 17 DEALER score - 20 Dealer's score is higher, you lose! Do you want to play another hand (y to play)?n Thanks for playing, your record was: wins: 1 losses: draws: 2Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started