Need help adding a dealer to my c++ black jack game and allow the players to cho
ID: 3555676 • Letter: N
Question
Need help adding a dealer to my c++ black jack game and allow the players to choose whether ths Ace is 1 or 11.
I beleive most of my program is basically done, but am just having trouble implementing the dealer into it.
Here is the class of my card.
#include <iostream>
#include <ctime>
#include <istream>
using namespace std;
class card
{
public:
//constructors
card();
card(int r, int s);
//accessors
int getrank() const;
int getsuit() const { return suit; }
int getformat() const { return format; }
int getvalue();
bool fail() const { return err; }
bool getacehigh() const { return acehigh; }
//set value function
bool setcard(int r, int s);
bool setformat(int f);
bool setacehigh(int a);
//I/O
void printcard();
bool getcard();
friend card operator + (const card& c1, const card& c2);
friend card operator + (int cards, const card& c1);
friend bool operator == (const card& c1, const card& c2);
void swap(card&);
private:
int rank;
int suit;
static int format;
static bool acehigh;
bool err;
};
bool operator > (const card& c1, const card& c2);
bool operator >= (const card& c1, const card& c2);
bool operator < (const card& c1, const card& c2);
bool operator <= (const card& c1, const card& c2);
bool operator != (const card& c1, const card& c2);
void compare(card &c1, card &c2);
int card::format = 1;
bool card::acehigh = false;
bool card::getcard()
{
int r, s;
cout << "Enter card rank. (1-13//1 = A//11-13 = J,Q,K,): ";
cin >> r;
cout << "Enter card suit. (1-4 = Heart, DIamond, Club, Spade: ";
cin >> s;
return setcard(r, s);
}
bool card::setcard(int r, int s)
{
bool ok = true;
//Check rank
if (r < 1 || r > 14)
ok = false;
//Check suit
if (s < 1 || s > 4)
ok = false;
if (ok)
rank = r;
suit = s;
err = !ok;
return ok;
}
void card::printcard()
{
const char heart = 3;
const char diamond = 4;
const char club = 5;
const char spade = 6;
switch (format)
{
case 1:
if (rank == 11)
cout << "jack";
else if (rank == 12)
cout << "queen";
else if (rank == 13)
cout << "king";
else if (rank == 1)
cout << "ace";
else
cout << rank;
if (suit == 1)
cout << heart;
if (suit == 2)
cout << diamond;
if (suit == 3)
cout << club;
if (suit == 4)
cout << spade;
break;
case 2:
if (rank == 11)
cout << "jack";
else if (rank == 12)
cout << "queen";
else if (rank == 13)
cout << "king";
else if (rank == 1)
cout << "ace";
else
cout << rank;
if (suit == 1)
cout << "Hearts";
if (suit == 2)
cout << "Diamonds";
if (suit == 3)
cout << "Clubs";
if (suit == 4)
cout << "spades";
break;
}
return;
}
void compare(card &c1, card &c2)
{
char ent;
system("cls");
cout << "Card #1 is: ";
c1.printcard();
cout << " Card #2 is: ";
c2.printcard();
cout << " Card #1 > card #2 returns: " << (c1 > c2) << endl;
cout << " Card #1 >= card #2 returns: " << (c1 >= c2) << endl;
cout << " Card #1 < card #2 returns: " << (c1 < c2) << endl;
cout << " Card #1 <= card #2 returns: " << (c1 <= c2) << endl;
cout << " Card #1 == card #2 returns: " << (c1 == c2) << endl;
cout << " Card #1 != card #2 returns: " << (c1 != c2) << endl;
cout << " Hit Enter.....";
cin.ignore(1000, ' ');
cin.get(ent);
}
card::card()
{
setcard(13, 1);
}
card::card(int r, int s)
{
if (!setcard(r, s))
{
rank = 2;
suit = 2;
}
}
void card::swap(card& c)
{
card temp;
temp.rank = c.rank;
temp.suit = c.suit;
c.rank = this->rank;
c.suit = this->suit;
this->rank = temp.rank;
this->suit = temp.suit;
}
bool card::setformat(int f)
{
if (f >= 1 && f <= 2)
{
format = f;
err = false;
}
else
{
err = true;
}
return !err;
}
int card::getvalue()
{
int val;
if (rank > 10 && rank < 14)
{
val = 10;
return val;
}
else if (acehigh && rank == 1)
{
val = 11;
return val;
}
else
return rank;
}
bool card::setacehigh(int a)
{
if (a == 1 || a == 2)
{
acehigh = (a == 2);
err = false;
}
else
{
err = true;
}
return !err;
}
int card::getrank() const
{
if (acehigh && rank == 1)
return 14;
else
return rank;
}
bool operator == (const card& c1, const card& c2)
{
return c1.rank == c2.rank;
}
bool operator > (const card& c1, const card& c2)
{
if (c1.getrank() > c2.getrank())
return true;
else
return false;
}
bool operator >= (const card& c1, const card& c2)
{
return c1 > c2 || c1 == c2;
}
bool operator <(const card& c1, const card& c2)
{
return !(c1 >= c2);
}
bool operator <= (const card& c1, const card& c2)
{
return c1 < c2 || c1 == c2;
}
bool operator != (const card& c1, const card& c2)
{
return !(c1 == c2);
}
card operator + (const card& c1, const card& c2)
{
card temp;
temp.rank = c1.getrank() + c2.getrank();
return temp;
}
card operator + (int cards, const card& c1)
{
card temp;
temp.rank = cards + c1.getrank();
return temp;
}
Here is my functions for my source code
#include "card.h"
card hand[52];
const int GAMELIMIT = 21;
const int DEALER = 16;
const int CARDSPERPLAYER = 2;
const int PLAYERS = 2;
void introDisplay();
void dealer(string dealer[], int dealerHit[], int i);
void dealCards(int playerValues[]);
void displayCards();
void hitPlayer(int playerValues[]);
void shuffleCards(int counter);
void introDisplay()
{
cout << "Welcome to Black Jack ";
}
void dealer(string dealer[], int dealerHit[], int i)
{
dealer[i + 1] = "Dealer";
dealerHit[i + 1] = DEALER;
}
void dealCards(int playerValues[])
{
int counter = 0;
cout << " Dealing each player " << CARDSPERPLAYER << " cards ";
for (int j = 0; j < CARDSPERPLAYER; j++)
{
for (int i = 0; i < PLAYERS; i++)
{
playerValues[i] += hand[counter++].getvalue();
}
}
for (int x = 0; x < PLAYERS; x++)
{
cout << "The hand for player " << x + 1 << " is: ";
hand[x].printcard();
cout << " and ";
hand[x + PLAYERS].printcard();
cout << " Your hand value is " << playerValues[x] << endl;
}
}
void displayCards()
{
for (int i = 0; i < 52; i++)
{
if (i % 7 == 0)
{
cout << endl;
}
hand[i].printcard();
cout << ' ';
}
cout << " ";
}
void hitPlayer(int playerValues[])
{
int hit = PLAYERS * CARDSPERPLAYER;
char choice;
int x = 0;
for (x = 0; x < PLAYERS; x++)
{
while (playerValues[x] < GAMELIMIT)
{
cout << " Player " << x + 1 << " would you like to hit?: ";
cin >> choice;
if (choice == 'y' || choice == 'Y')
{
playerValues[x] += hand[hit].getvalue();
cout << "Player " << x + 1 << " would like another card: ";
hand[hit].printcard();
hit++;
cout << " Player " << x + 1 << " your hand values is " << playerValues[x] << endl << endl;
}
else
{
break;
}
}
}
}
void shuffleCards(int counter)
{
for (int s = 1; s <= 4; s++)
{
for (int r = 1; r <= 13; r++)
{
hand[counter] = card(r, s);
counter++;
}
}
for (int i = 0; i < 52; i++)
{
hand[i].swap(hand[rand()%52]);
}
}
void results(int playerValue[])
{
for (int i = 0; i < PLAYERS; i++)
{
if (playerValue[PLAYERS] == GAMELIMIT)
{
for (int x = 0; x < PLAYERS; x++)
{
}
}
}
}
And here is my source file.
#include "BlackJackFunctions.h"
void main()
{
int hit[PLAYERS] = { 0 }, cardsValue[PLAYERS] = { 0 }, index = { 0 },
i = 0;
srand((unsigned)(time)(0));
shuffleCards(index);
displayCards();
introDisplay();
dealCards(cardsValue);
hitPlayer(cardsValue);
system("pause");
}
Explanation / Answer
use this dealer loop code.
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 << endl;
if (aceValue > 0)
cout << " or " << dealerTotal + aceValue << endl;
int nextValue = (rand()%13)+1;
int nextSuit = (rand()%4)+1;
cout << "Dealer dealt ";
displayCard(nextValue, nextSuit);
cout << endl;
dealerTotal = dealerTotal + getCardPoints(nextValue, aceValue);
if (dealerTotal + aceValue < 22)
{
bestHand = dealerTotal + aceValue;
}
else
bestHand = dealerTotal;
//cout << "Dealer's current hand point value is ";
//cout << dealerTotal << endl;
}
return dealerTotal;
}
full code:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
int getCardPoints(int value, int & aceValue)
{
if ((value >= 2) && (value <= 10))
{
return (value);
}
else if (value == 1)
{
aceValue = 10; // 11 -1
return 1; // Already contain 1 for the Ace value
}
else if (value == 11)
{
return 10;
}
else if (value == 12)
{
return 10;
}
else if (value == 13)
{
return 10;
}
else
{
cout << "Error! Invalid card value." << endl;
exit (0);
}
}
void displayCardFace(int face)
{
if ((face >= 2) && (face <= 10))
{
cout << face;
}
else if (face == 1)
{
cout << "Ace";
}
else if (face == 11)
{
cout << "Jack";
}
else if (face == 12)
{
cout << "Queen";
}
else if (face == 13)
{
cout << "King";
}
else
{
cout << "Error! Invalid card face." << endl;
exit(0);
}
}
void displayCardSuit(int suit)
{
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 face, int suit)
{
displayCardFace(face);
displayCardSuit(suit);
}
int playerLoop(int playerTotal, int & aceValue)
{
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)
{
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 << endl;
if (aceValue > 0)
cout << " or " << dealerTotal + aceValue << endl;
int nextValue = (rand()%13)+1;
int nextSuit = (rand()%4)+1;
cout << "Dealer dealt ";
displayCard(nextValue, nextSuit);
cout << endl;
dealerTotal = dealerTotal + getCardPoints(nextValue, aceValue);
if (dealerTotal + aceValue < 22)
{
bestHand = dealerTotal + aceValue;
}
else
bestHand = dealerTotal;
//cout << "Dealer's current hand point value is ";
//cout << dealerTotal << endl;
}
return dealerTotal;
}
int main()
{
// reset the random number generator
srand((unsigned)time(0));
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;
/*=== Fix the data for testing
int playerC1_value = 1;
int playerC1_suit = 1;
int dealerC1_value = 1;
int dealerC1_suit = 2;
int playerC2_value = 1;
int playerC2_suit = 3;
// === Fix the data for testing */
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;
//figure player total hand value
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);
cout << "So far, player has " << playerTotal << " or " << playerAceValue+playerTotal << " points" <<endl;
cout << "So far, dealer has " << dealerTotal << " or " << dealerAceValue+dealerTotal << " points" <<endl;
playerTotal = playerLoop(playerTotal, playerAceValue);
dealerTotal = dealerLoop(dealerTotal, dealerAceValue);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.