#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void displayCardFace(int value)// gives 10 value cards a face to make gameplay more realistic
{
if ((value >= 2) && (value <=10))
cout << value;
else if (value == 1)
cout << "Ace";
else if (value == 11)
cout << "Jack";
else if (value == 12)
cout << "Quenn";
else if (value == 13)
cout << "King";
else
{
cout << "Error! Invalid card face." << endl; //error catch
exit(0);
}
}
//=======================================================
void displayCardSuit(int suit)//assign card a suit value
{
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; //error catch
exit (0);
}
}
//=========================================================
void displayCard(int value, int suit)
{
displayCardFace(value);
displayCardSuit(suit);
}
//==========================================================
int getCardPoints(int value, int & aceValue) //assign card a face value
{
if ((value >= 2) && (value <= 10))
return (value);
else if (value == 1)
{
if (aceValue == 0)
aceValue = 10;
return 1;
}
else if (value == 11)
return 10;
else if (value == 12)
return 10;
else if (value == 13)
return 10;
else
{ cout << "Error! Invalid card face." << endl; // error catch
exit (0);
}
}
//============================================================
int playerLoop(int playerTotal, int & aceValue) // execute the players gameplay
{
string answer;
do
{
cout << "Player's current hand point value is ";
cout <<playerTotal;
if (aceValue > 0)
cout << " or " << playerTotal + aceValue;
cout << endl;
cout << "Do you want another card? (\"hit \" or \"stay\"): ";
getline (cin, answer);
if (answer == "hit")
{
int nextValue = (rand()%13) + 1;
int nextSuit = (rand()%4) + 1;
cout << "Player is dealt ";
displayCard(nextValue, nextSuit);
cout << endl;
playerTotal = playerTotal + getCardPoints(nextValue, aceValue);
}
}while ((answer == "hit") && (playerTotal <= 21));
return playerTotal;
}
//==================================================================
int dealerLoop(int dealerTotal, int & aceValue)// execute the dealers gameplay
{
int bestHand;
if (dealerTotal + aceValue <22)
bestHand = dealerTotal + aceValue;
else
bestHand = dealerTotal;
while (bestHand < 17)// stops dealer from pulling cards at 17 to stay within blackjacks rules
{
cout <<"Dealer's current hand point value is ";
cout << dealerTotal;
if (aceValue > 0)
cout << " or " << dealerTotal + aceValue;
cout << endl;
int nextValue = (rand()%13) + 1;
int nextSuit = (rand()%4) + 1;
cout << "Dealer is dealt ";
displayCard(nextValue, nextSuit);
cout << endl;
dealerTotal = dealerTotal + getCardPoints(nextValue, aceValue);
if (dealerTotal + aceValue < 22)
bestHand = dealerTotal + aceValue;
else
bestHand = dealerTotal;
}
return dealerTotal;
}
void finalPoints(int playerTotal,int playerAceValue, int dealerTotal, int dealerAceValue) // use if value == 1 instead of the other stuff
{
if ((playerTotal + playerAceValue) > (21))
cout << "At the end, player has " << playerTotal << " or " << playerTotal + playerAceValue << " points"
<< endl;
else
{
cout << "At the end, player has " << playerTotal << " points" <<endl;
}
if ((dealerTotal + playerAceValue) > (21))
cout << "At the end, dealer has " << dealerTotal << " or " << dealerTotal + dealerAceValue << " points"
<< endl;
else
{
cout << "At the end, dealer has " << dealerTotal << " points" << endl;
}
// who won the game ifs
if ((playerTotal || playerTotal + playerAceValue) == (dealerTotal))
cout <<"Draw";
else if ((playerTotal && playerTotal + playerAceValue) > (dealerTotal))
cout <<"Player wins!";
else if ((dealerTotal) > (playerTotal && playerTotal + playerAceValue))
cout <<"Dealer wins";
else if ((playerTotal && playerTotal + playerAceValue) > (21))
cout <<"Player busted";
else if (dealerTotal > 21)
cout <<"Dealer busted -- player wins!";
else
{
cout <<"Error! invalid game";
}
}
int main()
{
//reset
srand((unsigned)time(0));
//draw card
int playerC1_value = (rand()%13) +1;
int playerC1_suit = (rand()%4) + 1;
int dealerC1_value = (rand()%13) +1;
int dealerC1_suit = (rand()%4) + 1;
int playerC2_value = (rand()%13) +1;
int playerC2_suit = (rand()%4) + 1;
int dealerC2_value = (rand()%13) +1;
int dealerC2_suit = (rand()%4) + 1;
/*===================================FIXED DATA
playerC1_value = 1;
playerC1_suit = 1;
dealerC1_value = 1;
dealerC1_suit = 2;
playerC2_value = 1;
playerC2_suit = 3;
//++++++++++++++++++++++++++++++++++ fixed data */
cout << "Player has ";
displayCard(playerC1_value, playerC1_suit);
cout << " and ";
displayCard(playerC2_value, playerC2_suit);
cout << endl;
cout << "Dealer has ";
displayCard(dealerC1_value, dealerC1_suit);
cout << " showing " << endl;
//figures total hand values for player and dealer below 5
int playerAceValue = 0;
int dealerAceValue = 0;
int playerTotal =
getCardPoints(playerC1_value, playerAceValue) +
getCardPoints(playerC2_value, playerAceValue);
int dealerTotal =
getCardPoints(dealerC1_value, dealerAceValue) +
getCardPoints(dealerC2_value, dealerAceValue);
playerTotal = playerLoop(playerTotal, playerAceValue);
dealerTotal = dealerLoop(dealerTotal, dealerAceValue);
finalPoints(playerTotal, playerAceValue, dealerTotal, dealerAceValue);
}
Hi everyone, I am struggling like crazy with this program. I got the majority of everything I want working except for my finalPoints() function. I left the other code open to see if any other function is messing up that one. Any direction would be greatly appreciated.