I have to write this code for my homework assignment. I am in an introductory C++ class, VERY introductory. We had to write a blackjack game, but follow the book at the same time, I know there are many easier ways to do the things my code says, but we haven't learned any of them, so nothing fancy please!=) Our assignment this week says the following "Modify your program from programming exercise 12 by replacing each of the card draws with a function which performs the card draw. DO NOT IMPLEMENT ANY ARRAYS AT THIS TIME! (next assignment)"
I can post the original program if anyone would like, but so far I have made a drawCard function that I use in the displayCard function. I set multiple variables equal to 0 for the next card draws, I am not sure if this is right, but I realized that what they used to be set equal to is the point of making the drawCard function. There are a lot of comments in the program, but if you run it you will see where the problem is. I can't get from the playerLoop into the dealerLoop because getCardPoints is not working. If you could please give me specific instructions and show me how to change it, it will be appreciated!!! I really don't understand a lot of the C++ terms you guys are familiar with, so showing me what I have to change is probably the only way I will understand=) Again, no arrays, that's saved for the next assignment. Thank you for all of your help!!!=)
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void drawCard(int &value, int &suit)
{
value = (rand()%13) + 1;
suit = (rand()%4) + 1;
}
void displayCardValue(int value) // so card values 1,
//11-13 correspond to card names
{
if ((value >= 2) && (value <= 10)) // cards 2-10 are numbers
cout << value;
else if (value == 1)
cout << "Ace";
else if (value == 11)
cout << "Jack";
else if (value == 12)
cout << "Queen";
else if (value == 13)
cout << "King";
else
{
cout << "Error! Invalid card value DISPLAYCARD." << endl;
exit(0);
}
}
void displayCardSuit(int suit) // assign numbers 1-4 to 1 of the 4 suits
{
if (suit == 1)
cout << " of spades";
else if (suit == 2)
cout << " of clubs";
else if (suit == 3)
cout << " of diamonds";
else if (suit == 4)
cout << " of hearts";
else
{
cout << "Error! Invalid card suit." << endl;
exit(0);
}
}
void displayCard(int &value, int &suit) // so don't have to call both
//displayCardFace and displayCardSuit, can call both at once
{
drawCard(value, suit);
displayCardValue(value);
displayCardSuit(suit);
}
int getCardPoints(int &value, int & aceValue)// assigning values to cards for
//black jack addition
{
if ((value >= 2) && (value <= 10))
return (value);
else if (value == 1)
{
aceValue = 10; //11-1
return 1; // see explanation
}
else if (value == 11)
return 10;
else if (value == 12)
return 10;
else if (value == 13)
return 10;
else
{
cout << "Error! Invalid card value GETCARDPOINTS." << endl;
exit(0);
}
}
int playerLoop(int playerTotal, int & aceValue) //for players points and if
//they want to keep getting new cards
{
string answer;
do // loop because player may be asked to hit or stay multiple times
{
cout << "Player's current hand point value is ";
cout << playerTotal;
if (aceValue > 0)
cout << " or " << playerTotal + aceValue << endl;
else
cout << endl; //added in myself
cout << "Do you want another card? (\"hit\" or \"stay\"): ";
getline(cin, answer);
if (answer == "hit")
{
int nextValue = 0; //(rand()%13) + 1; //gets value number
int nextSuit = 0; //(rand()%4) + 1; //gets a suit
cout << "Player is dealt ";
displayCard(nextValue, nextSuit); //calls and displays the card
cout << endl;
//add new points onto old point total for a new total
playerTotal = playerTotal + getCardPoints(nextValue, aceValue);
}
} while ((answer == "hit") && (playerTotal <= 21)); //over 21 lose or at 21 win
return playerTotal; //returns new player total
}
int dealerLoop(int dealerTotal, int & aceValue)
{
int bestHand;
if (dealerTotal + aceValue < 22)
bestHand = dealerTotal + aceValue;
else
bestHand = dealerTotal;
while (bestHand < 17)
{
cout << "Dealer's current hand point value is ";
cout << dealerTotal;
if (aceValue > 0)
cout << " or " << dealerTotal + aceValue;
cout << endl;
int nextValue = 0; //(rand()%13) + 1; //gets face number
int nextSuit = 0; //(rand()%4) + 1; //gets a suit
cout << "Dealer is dealt ";
displayCard(nextValue, nextSuit); //calls and displays the card
cout << endl;
//add new points onto old point total for a new total
dealerTotal = dealerTotal + getCardPoints(nextValue, aceValue);
if (dealerTotal + aceValue < 22)
bestHand = dealerTotal + aceValue;
else
bestHand = dealerTotal;
}
return dealerTotal; //returns dealer's new total
}
int main()
{
// Reset the random number generator
srand((unsigned)time(0));
// draw a card
int playerC1_value = 0; //(rand()%13) + 1;
int playerC1_suit = 0; //(rand()%4) + 1;
int dealerC1_value = 0; //(rand()%13) + 1;
int dealerC1_suit = 0; //(rand()%4) + 1;
int playerC2_value = 0; //(rand()%13) + 1;
int playerC2_suit = 0;//(rand()%4) + 1;
int dealerC2_value = 0; //(rand()%13) + 1;
int dealerC2_suit = 0; //(rand()%4) + 1;
//player dealt 2 cards, know both values
int value = 0, suit = 0;
cout << "Player has ";
displayCard(playerC1_value, playerC1_suit);
cout << " and ";
displayCard(playerC2_value, playerC2_suit);
cout << endl;
//dealer dealt 2 cards, only display 1
cout << "Dealer has ";
displayCard(dealerC1_value, dealerC1_suit);
cout << " showing " << endl;
// figure player total hand value
int playerAceValue = 0;
int dealerAceValue = 0;
int playerTotal = getCardPoints(playerC1_value, playerAceValue) + getCardPoints(playerC2_value, playerAceValue);
playerTotal = playerLoop(playerTotal, playerAceValue);
cout << "BEFORE dealerTotal=getCardPoints to add them up." << endl;
int dealerTotal = getCardPoints(dealerC1_value, dealerAceValue) + getCardPoints(dealerC2_value, dealerAceValue);
cout << "AFTER dealerTotal= getCardPoints to add them up." << endl;
dealerTotal = dealerLoop(dealerTotal, dealerAceValue);
if (playerAceValue >= 1)
cout << "At the end, player has " << playerTotal << " or " << playerTotal + playerAceValue << " points " << endl;
else
cout << "At the end, palyer has " << playerTotal << " points" << endl;
if (dealerAceValue >= 1)
cout << "At the end, dealer has " << dealerTotal << " or " << dealerTotal + dealerAceValue << " points " << endl;
else
cout << "At the end, dealer has " << dealerTotal << " points" << endl;
int playerHighestTotal = playerTotal + playerAceValue;
int dealerHighestTotal = dealerTotal + dealerAceValue;
if ((dealerTotal == playerTotal) || (dealerTotal == playerHighestTotal)
|| (dealerHighestTotal == playerTotal) || (dealerHighestTotal == playerHighestTotal))
cout << "Draw" << endl;
else if (((dealerTotal == 21) && (playerTotal == 21)) || ((dealerTotal == 21) && (playerHighestTotal == 21))
|| ((dealerHighestTotal == 21) && (playerTotal == 21)) || ((dealerHighestTotal == 21) && (playerHighestTotal == 21)))
cout << "Tie!" << endl;
else if ((playerTotal > 21) && (playerHighestTotal > 21))
cout << "Player busted" << endl;
else if (((dealerTotal > 21) && (dealerHighestTotal > 21)) && ((playerTotal <= 21) || (playerHighestTotal <= 21)))
cout << "Dealer busted -- player wins!" << endl;
else if (((dealerTotal > playerTotal) && (dealerTotal > playerHighestTotal)) || ((dealerHighestTotal > playerTotal) &&
(dealerHighestTotal > playerHighestTotal)) && ((dealerHighestTotal <= 21) || (dealerTotal <= 21)) && ((playerHighestTotal < 21)
|| (playerTotal < 21)))
cout << "Dealer wins" << endl;
else
cout << "Player wins!" << endl;
}