ANS:
#include
int Rn_get(int low, int high);
int cvalue(int card);
int toca(int deck[]);
int handva(const int hand[]);
bool pla_hand(char&);
bool draw_u(char&);
void inti(int deck[]);
void d_deck(int deck[], int size);
void dhudd(int deck[], int size);
void show_card(int card);
void card_s(const int cards[], int numCards, bool hideFirstCard);
void wiwh(const int pHand[], const int dHand[]);
void cbust(const int pHand[], const int dHand[], char p1);
void bbjack(const int pHand[], const int dHand[], char p1);
void nbjack(const int pHand[], const int dHand[], char p1);
void play_one();
void scboard(const int pHand[], const int dHand[]);
void bakkdor(const int hand[]);
void sffot(int pHand[]);
void go_to(int dHand[], int pHand[]);
void choo_def();
void sh_rules();
void add_hand(int hand[], int cardToAdd);
void stand_hit(int dHand[], int deck[], int pHand[]);
int tcard = 0;
float wi = 1.00;
int l_1;
int main()
{
sh_rules();
choo_def();
play_one();
return 1;
}
void sh_rules()
{
}
void choo_def()
{
cout << " ";
cout << "Level of Difficulty: Beginner (1), Expert (2). ";
cin >> l_1;
cout << " ";
if(l_1 != 1)
{
if(l_1 != 2)
{
cin.clear();
cin.ignore(numeric_limits::max(), ' ');
cout << "Please enter your l_1 of difficulty using the number 1 or 2." << endl;
choo_def();
}
}
}
void play_one()
{
char p1 = 'N';
do
{
char dr = 'H';
int deck[52];
inti(deck);
dhudd(deck, 51);
int pHand[10] = {0};
int dHand[10] = {0};
add_hand(pHand, toca(deck));
add_hand(dHand, toca(deck));
add_hand(pHand, toca(deck));
add_hand(dHand, toca(deck));
cout << "The Dealer dhuddd and dealt you each your cards. ";
nbjack(pHand, dHand, 'y');
bbjack(pHand, dHand, 'Y');
cout << "Your hand:" << endl;
card_s(pHand, 10, false);
cout << "  Dealer's hand:" << endl;
card_s(dHand, 10, true);
sffot(pHand);
while (draw_u(dr))
{
add_hand(pHand, toca(deck));
cout << "The Dealer dealt you another card. " << endl;
cout << "Your hand:" << endl;
card_s(pHand, 10, false);
sffot(pHand);
cbust(pHand, dHand, 'y');
bbjack(pHand, dHand, 'Y');
}
stand_hit(dHand, deck, pHand);
cout << endl;
cbust(pHand, dHand, 'Y');
bbjack(pHand, dHand, 'Y');
wiwh(pHand, dHand);
cout << endl;
cout << "Winnings multiplier: " << wi << endl;
}
while (pla_hand(p1));
}
void inti(int deck[])
{
int Rank = 101;
int i = 0;
for (i = 0 ; i<=13; i++)
{
deck[i] = Rank++;
}
for (i = 13 ; i<=26; i++)
{
deck[i] = Rank++ + 100 - 14;
}
for (i = 26 ; i<=39; i++)
{
deck[i] = Rank++ + 200 - 28;
}
for (i = 39 ; i<=51; i++)
{
deck[i] = Rank++ + 300 - 42;
}
}
void dhudd(int deck[], int size)
{
for(int i = 0; i < 500; i++)
{
int T1 = 0;
int R1 = Rn_get(0, size);
int R2 = Rn_get(0, size);
T1 = deck[R1];
deck[R1] = deck[R2];
deck[R2] = T1;
}
}
void show_card(int card)
{
if(card == 0)
{
cout << "";
}
else
{
switch(card % 100)
{
case 1:
cout << "A";
break;
case 11:
cout << "J";
break;
case 12:
cout << "K";
break;
case 13:
cout << "Q";
break;
case 14:
cout << "A";
break;
default:
cout << card % 100;
}
}
if(card == 0)
{
cout << "";
}
else
{
if((card >= 101) && (card <=114))
{
cout << static_cast(3);
}
else if ((card >= 201) && (card <= 214))
{
cout << static_cast(4);
}
else if ((card >= 301) && (card <= 314))
{
cout << static_cast(5);
}
else if ((card >= 401) && (card <= 414))
{
cout << static_cast(6);
}
}
}
void card_s(const int deck[], int numCards, bool hideFirstCard)
{
if(hideFirstCard)
{
cout << "** ";
}
else
{
show_card(deck[0]);
cout << " ";
}
for(int i = 1; i < numCards; i++)
{
if(deck[i] != 0)
{
show_card(deck[i]);
cout << " ";
}
else
{
cout << "";
}
}
}
void d_deck(int deck[], int size)
{
for(int i = 0; i < size; i++)
{
cout << i + 1 << ".) " << deck[i] << endl;
}
}
int Rn_get(int low, int high) {
static bool firstTime=true;
int randNum;
if (firstTime) {
srand( static_cast(time(NULL)) );
firstTime=false;
}
randNum = rand() % (high-low+1) + low;
return randNum;
}
int cvalue(int card)
{
int cardVal;
switch(card % 100)
{
case 1:
cardVal = 11;
break;
case 11:
case 12:
case 13:
cardVal = 10;
break;
case 14:
cardVal = 1;
break;
default:
cardVal = (card % 100);
}
return cardVal;
}
int toca(int deck[])
{
for(int i = 0; i < 51; i++)
{
if(deck[i] != 0)
{
tcard = deck[i];
deck[i] = 0;
return tcard;
}
}
}
void add_hand(int hand[], int cardToAdd)
{
for(int i = 0; i < 9; i++)
{
if(hand[i] == 0)
{
hand[i] = cardToAdd;
break;
}
}
}
void stand_hit(int dHand[], int deck[], int pHand[])
{
for(int i = 0; i < 9; i++)
{
if(handva(dHand) < 17)
{
if(l_1 == 1)
{
add_hand(dHand, toca(deck));
}
else if( l_1 == 2)
{
add_hand(dHand, toca(deck));
go_to(dHand, pHand);
}
}
else
{
if(i == 0)
{
cout << "The Dealer stands." << endl;
break;
}
else if(i == 1)
{
cout << "The Dealer hit a card and stands." << endl;
break;
}
else
{
cout << "The Dealer hit " << i << " cards and stands." << endl;
break;
}
}
if(l_1 == 1);
else if( l_1 == 2)
{
go_to(dHand, pHand);
}
}
}
int handva(const int hand[])
{
int addcvalues = 0;
for(int i = 0; i < 9; i++)
{
addcvalues = addcvalues + cvalue(hand[i]);
}
return addcvalues;
}
bool pla_hand(char& p1)
{
cout << endl << " Would you like to play another hand? (Y/N) ";
cin >> p1;
cout << " " << endl;
if(p1 == 'y' || p1 == 'Y')
{
play_one();
return(true);
}
else
{
return(false);
}
}
bool draw_u(char& dr)
{
cout << endl << " Would you like to hit or stand? (H/S) ";
cin >> dr;
cout << " ";
if(dr == 'h' || dr == 'H')
{
return(true);
}
else
{
return(false);
}
}
void wiwh(const int pHand[], const int dHand[])
{
int playerScore = handva(pHand);
int dealerScore = handva(dHand);
scboard(pHand, dHand);
if( (playerScore < 22)
&& (playerScore > dealerScore)
|| ( (dealerScore > 21)
&& (playerScore < 22) ) )
{
cout << " ";
cout << "You wi!" << endl;
wi++;
}
else
{
if(playerScore == dealerScore)
{
cout << " ";
cout << "Push in the Dealer's favor. 0:1 payout." << endl;
}
//loose
//Display message and compute new winings multiplier
else
{
cout << " ";
cout << "You lose." << endl;
wi--;
}
}
}
void cbust(const int pHand[], const int dHand[], char p1)
{
int playerScore = handva(pHand);
int dealerScore = handva(dHand);
if(playerScore > 21)
{
cout << "You bust with " << handva(pHand) << " points." << endl;
cout << " " << endl;
wi--;
cout << "Winnings multiplier: " << wi << endl;
pla_hand(p1);
}
else if(dealerScore > 21)
{
scboard(pHand, dHand);
cout << " " << endl;
cout << "The Dealer went bust. You Win!" << endl;
cout << " " << endl;
wi++;
cout << "Winnings multiplier: " << wi << endl;
pla_hand(p1);
}
}
void bakkdor(const int hand[])
{
if((cvalue(hand[0])%100 == 10)&&(cvalue(hand[1])%100 == 11))//I did not use 1 as an
argument because 11 is the default
//value and this happens before the user can choose.
{
cout << "  ";
cout << "You pulled a Backdoor Kenny! " << endl;
cout << "Win an additional 1:4 payout " << endl;
wi = wi + .25;
cout << "Winnings multiplier: " << wi << endl;
}
}
void bbjack(const int pHand[], const int dHand[], char p1)
{
int playerScore = handva(pHand);
int dealerScore = handva(dHand);
if((playerScore == 21) && (dealerScore != 21))
{
cout << "  ";
scboard(pHand, dHand);
cout << " ";
cout << "Blackjack! You wi a 3:2 payout." << endl;
wi = wi + 1.5;
cout << " ";
cout << "Winnings multiplier: " << wi << endl;
bakkdor(pHand);
pla_hand(p1);
}
else if((playerScore == 21) && (dealerScore == 21))
{
scboard(pHand, dHand);
cout << " ";
cout << "The Dealer and you both got Blackjack. Push in your favor at 1:1 payout!" <<
endl;
wi++;
cout << " ";
cout << "Winnings multiplier: " << wi << endl;
pla_hand(p1);
}
}
void nbjack(const int pHand[], const int dHand[], char p1)
{
int playerScore = handva(pHand);
int dealerScore = handva(dHand);
if((playerScore == 21) && (dealerScore != 21))
{
scboard(pHand, dHand);
cout << " ";
cout << "Natural Blackjack! You wi a 3:2 payout.";
wi = wi + 1.5;
bakkdor(pHand);
pla_hand(p1);
}
}
void scboard(const int pHand[], const int dHand[])
{
cout << "p1er hand: ";
card_s(pHand, 10, false);
cout << " ("<< handva(pHand) << "pts)."<> softOrHard;
if(softOrHard == 1)
{
if(checkAce == 11)
pHand[i] = pHand[i] + 13;
}
else if(softOrHard == 11)
{
if(checkAce == 1)
{
pHand[i] = pHand[i] - 13;
}
}
else if (softOrHard != 1 || softOrHard != 11)
{
cin.clear();
cin.ignore(numeric_limits::max(), ' ');
cout << " Please enter the number 1 or 11." << endl;
sffot(pHand);
}
}
}
}
void go_to(int dHand[], int pHand[])
{
for(int i=0; i<9; i++)
{
if(cvalue(dHand[i]) == 1 || cvalue(dHand[i]) == 11)
{
if(cvalue(dHand[i]) == 11)
{
if (handva(dHand) - cvalue(dHand[i]) + 1 > handva(pHand))
{
if (handva(dHand) - cvalue(dHand[i]) + 1 < 22)
{
dHand[i] = dHand[i] + 13;
}
}
else if (handva(dHand) > 21)
{
dHand[i] = dHand[i] + 13;
}
}
else
{
if (handva(dHand) - cvalue(dHand[i]) + 11 > handva(pHand))
{
if (handva(dHand) - cvalue(dHand[i]) + 11 < 22)
{
dHand[i] = dHand[i] - 13;
}
}
}
}
}
}
Solution
ANS:
#include
int Rn_get(int low, int high);
int cvalue(int card);
int toca(int deck[]);
int handva(const int hand[]);
bool pla_hand(char&);
bool draw_u(char&);
void inti(int deck[]);
void d_deck(int deck[], int size);
void dhudd(int deck[], int size);
void show_card(int card);
void card_s(const int cards[], int numCards, bool hideFirstCard);
void wiwh(const int pHand[], const int dHand[]);
void cbust(const int pHand[], const int dHand[], char p1);
void bbjack(const int pHand[], const int dHand[], char p1);
void nbjack(const int pHand[], const int dHand[], char p1);
void play_one();
void scboard(const int pHand[], const int dHand[]);
void bakkdor(const int hand[]);
void sffot(int pHand[]);
void go_to(int dHand[], int pHand[]);
void choo_def();
void sh_rules();
void add_hand(int hand[], int cardToAdd);
void stand_hit(int dHand[], int deck[], int pHand[]);
int tcard = 0;
float wi = 1.00;
int l_1;
int main()
{
sh_rules();
choo_def();
play_one();
return 1;
}
void sh_rules()
{
}
void choo_def()
{
cout << " ";
cout << "Level of Difficulty: Beginner (1), Expert (2). ";
cin >> l_1;
cout << " ";
if(l_1 != 1)
{
if(l_1 != 2)
{
cin.clear();
cin.ignore(numeric_limits::max(), ' ');
cout << "Please enter your l_1 of difficulty using the number 1 or 2." << endl;
choo_def();
}
}
}
void play_one()
{
char p1 = 'N';
do
{
char dr = 'H';
int deck[52];
inti(deck);
dhudd(deck, 51);
int pHand[10] = {0};
int dHand[10] = {0};
add_hand(pHand, toca(deck));
add_hand(dHand, toca(deck));
add_hand(pHand, toca(deck));
add_hand(dHand, toca(deck));
cout << "The Dealer dhuddd and dealt you each your cards. ";
nbjack(pHand, dHand, 'y');
bbjack(pHand, dHand, 'Y');
cout << "Your hand:" << endl;
card_s(pHand, 10, false);
cout << "  Dealer's hand:" << endl;
card_s(dHand, 10, true);
sffot(pHand);
while (draw_u(dr))
{
add_hand(pHand, toca(deck));
cout << "The Dealer dealt you another card. " << endl;
cout << "Your hand:" << endl;
card_s(pHand, 10, false);
sffot(pHand);
cbust(pHand, dHand, 'y');
bbjack(pHand, dHand, 'Y');
}
stand_hit(dHand, deck, pHand);
cout << endl;
cbust(pHand, dHand, 'Y');
bbjack(pHand, dHand, 'Y');
wiwh(pHand, dHand);
cout << endl;
cout << "Winnings multiplier: " << wi << endl;
}
while (pla_hand(p1));
}
void inti(int deck[])
{
int Rank = 101;
int i = 0;
for (i = 0 ; i<=13; i++)
{
deck[i] = Rank++;
}
for (i = 13 ; i<=26; i++)
{
deck[i] = Rank++ + 100 - 14;
}
for (i = 26 ; i<=39; i++)
{
deck[i] = Rank++ + 200 - 28;
}
for (i = 39 ; i<=51; i++)
{
deck[i] = Rank++ + 300 - 42;
}
}
void dhudd(int deck[], int size)
{
for(int i = 0; i < 500; i++)
{
int T1 = 0;
int R1 = Rn_get(0, size);
int R2 = Rn_get(0, size);
T1 = deck[R1];
deck[R1] = deck[R2];
deck[R2] = T1;
}
}
void show_card(int card)
{
if(card == 0)
{
cout << "";
}
else
{
switch(card % 100)
{
case 1:
cout << "A";
break;
case 11:
cout << "J";
break;
case 12:
cout << "K";
break;
case 13:
cout << "Q";
break;
case 14:
cout << "A";
break;
default:
cout << card % 100;
}
}
if(card == 0)
{
cout << "";
}
else
{
if((card >= 101) && (card <=114))
{
cout << static_cast(3);
}
else if ((card >= 201) && (card <= 214))
{
cout << static_cast(4);
}
else if ((card >= 301) && (card <= 314))
{
cout << static_cast(5);
}
else if ((card >= 401) && (card <= 414))
{
cout << static_cast(6);
}
}
}
void card_s(const int deck[], int numCards, bool hideFirstCard)
{
if(hideFirstCard)
{
cout << "** ";
}
else
{
show_card(deck[0]);
cout << " ";
}
for(int i = 1; i < numCards; i++)
{
if(deck[i] != 0)
{
show_card(deck[i]);
cout << " ";
}
else
{
cout << "";
}
}
}
void d_deck(int deck[], int size)
{
for(int i = 0; i < size; i++)
{
cout << i + 1 << ".) " << deck[i] << endl;
}
}
int Rn_get(int low, int high) {
static bool firstTime=true;
int randNum;
if (firstTime) {
srand( static_cast(time(NULL)) );
firstTime=false;
}
randNum = rand() % (high-low+1) + low;
return randNum;
}
int cvalue(int card)
{
int cardVal;
switch(card % 100)
{
case 1:
cardVal = 11;
break;
case 11:
case 12:
case 13:
cardVal = 10;
break;
case 14:
cardVal = 1;
break;
default:
cardVal = (card % 100);
}
return cardVal;
}
int toca(int deck[])
{
for(int i = 0; i < 51; i++)
{
if(deck[i] != 0)
{
tcard = deck[i];
deck[i] = 0;
return tcard;
}
}
}
void add_hand(int hand[], int cardToAdd)
{
for(int i = 0; i < 9; i++)
{
if(hand[i] == 0)
{
hand[i] = cardToAdd;
break;
}
}
}
void stand_hit(int dHand[], int deck[], int pHand[])
{
for(int i = 0; i < 9; i++)
{
if(handva(dHand) < 17)
{
if(l_1 == 1)
{
add_hand(dHand, toca(deck));
}
else if( l_1 == 2)
{
add_hand(dHand, toca(deck));
go_to(dHand, pHand);
}
}
else
{
if(i == 0)
{
cout << "The Dealer stands." << endl;
break;
}
else if(i == 1)
{
cout << "The Dealer hit a card and stands." << endl;
break;
}
else
{
cout << "The Dealer hit " << i << " cards and stands." << endl;
break;
}
}
if(l_1 == 1);
else if( l_1 == 2)
{
go_to(dHand, pHand);
}
}
}
int handva(const int hand[])
{
int addcvalues = 0;
for(int i = 0; i < 9; i++)
{
addcvalues = addcvalues + cvalue(hand[i]);
}
return addcvalues;
}
bool pla_hand(char& p1)
{
cout << endl << " Would you like to play another hand? (Y/N) ";
cin >> p1;
cout << " " << endl;
if(p1 == 'y' || p1 == 'Y')
{
play_one();
return(true);
}
else
{
return(false);
}
}
bool draw_u(char& dr)
{
cout << endl << " Would you like to hit or stand? (H/S) ";
cin >> dr;
cout << " ";
if(dr == 'h' || dr == 'H')
{
return(true);
}
else
{
return(false);
}
}
void wiwh(const int pHand[], const int dHand[])
{
int playerScore = handva(pHand);
int dealerScore = handva(dHand);
scboard(pHand, dHand);
if( (playerScore < 22)
&& (playerScore > dealerScore)
|| ( (dealerScore > 21)
&& (playerScore < 22) ) )
{
cout << " ";
cout << "You wi!" << endl;
wi++;
}
else
{
if(playerScore == dealerScore)
{
cout << " ";
cout << "Push in the Dealer's favor. 0:1 payout." << endl;
}
//loose
//Display message and compute new winings multiplier
else
{
cout << " ";
cout << "You lose." << endl;
wi--;
}
}
}
void cbust(const int pHand[], const int dHand[], char p1)
{
int playerScore = handva(pHand);
int dealerScore = handva(dHand);
if(playerScore > 21)
{
cout << "You bust with " << handva(pHand) << " points." << endl;
cout << " " << endl;
wi--;
cout << "Winnings multiplier: " << wi << endl;
pla_hand(p1);
}
else if(dealerScore > 21)
{
scboard(pHand, dHand);
cout << " " << endl;
cout << "The Dealer went bust. You Win!" << endl;
cout << " " << endl;
wi++;
cout << "Winnings multiplier: " << wi << endl;
pla_hand(p1);
}
}
void bakkdor(const int hand[])
{
if((cvalue(hand[0])%100 == 10)&&(cvalue(hand[1])%100 == 11))//I did not use 1 as an
argument because 11 is the default
//value and this happens before the user can choose.
{
cout << "  ";
cout << "You pulled a Backdoor Kenny! " << endl;
cout << "Win an additional 1:4 payout " << endl;
wi = wi + .25;
cout << "Winnings multiplier: " << wi << endl;
}
}
void bbjack(const int pHand[], const int dHand[], char p1)
{
int playerScore = handva(pHand);
int dealerScore = handva(dHand);
if((playerScore == 21) && (dealerScore != 21))
{
cout << "  ";
scboard(pHand, dHand);
cout << " ";
cout << "Blackjack! You wi a 3:2 payout." << endl;
wi = wi + 1.5;
cout << " ";
cout << "Winnings multiplier: " << wi << endl;
bakkdor(pHand);
pla_hand(p1);
}
else if((playerScore == 21) && (dealerScore == 21))
{
scboard(pHand, dHand);
cout << " ";
cout << "The Dealer and you both got Blackjack. Push in your favor at 1:1 payout!" <<
endl;
wi++;
cout << " ";
cout << "Winnings multiplier: " << wi << endl;
pla_hand(p1);
}
}
void nbjack(const int pHand[], const int dHand[], char p1)
{
int playerScore = handva(pHand);
int dealerScore = handva(dHand);
if((playerScore == 21) && (dealerScore != 21))
{
scboard(pHand, dHand);
cout << " ";
cout << "Natural Blackjack! You wi a 3:2 payout.";
wi = wi + 1.5;
bakkdor(pHand);
pla_hand(p1);
}
}
void scboard(const int pHand[], const int dHand[])
{
cout << "p1er hand: ";
card_s(pHand, 10, false);
cout << " ("<< handva(pHand) << "pts)."<> softOrHard;
if(softOrHard == 1)
{
if(checkAce == 11)
pHand[i] = pHand[i] + 13;
}
else if(softOrHard == 11)
{
if(checkAce == 1)
{
pHand[i] = pHand[i] - 13;
}
}
else if (softOrHard != 1 || softOrHard != 11)
{
cin.clear();
cin.ignore(numeric_limits::max(), ' ');
cout << " Please enter the number 1 or 11." << endl;
sffot(pHand);
}
}
}
}
void go_to(int dHand[], int pHand[])
{
for(int i=0; i<9; i++)
{
if(cvalue(dHand[i]) == 1 || cvalue(dHand[i]) == 11)
{
if(cvalue(dHand[i]) == 11)
{
if (handva(dHand) - cvalue(dHand[i]) + 1 > handva(pHand))
{
if (handva(dHand) - cvalue(dHand[i]) + 1 < 22)
{
dHand[i] = dHand[i] + 13;
}
}
else if (handva(dHand) > 21)
{
dHand[i] = dHand[i] + 13;
}
}
else
{
if (handva(dHand) - cvalue(dHand[i]) + 11 > handva(pHand))
{
if (handva(dHand) - cvalue(dHand[i]) + 11 < 22)
{
dHand[i] = dHand[i] - 13;
}
}
}
}
}
}

ANS#include iostream int Rn_get(int low, int high); int cva.pdf

  • 1.
    ANS: #include int Rn_get(int low,int high); int cvalue(int card); int toca(int deck[]); int handva(const int hand[]); bool pla_hand(char&); bool draw_u(char&); void inti(int deck[]); void d_deck(int deck[], int size); void dhudd(int deck[], int size); void show_card(int card); void card_s(const int cards[], int numCards, bool hideFirstCard); void wiwh(const int pHand[], const int dHand[]); void cbust(const int pHand[], const int dHand[], char p1); void bbjack(const int pHand[], const int dHand[], char p1); void nbjack(const int pHand[], const int dHand[], char p1); void play_one(); void scboard(const int pHand[], const int dHand[]); void bakkdor(const int hand[]); void sffot(int pHand[]); void go_to(int dHand[], int pHand[]); void choo_def(); void sh_rules(); void add_hand(int hand[], int cardToAdd); void stand_hit(int dHand[], int deck[], int pHand[]); int tcard = 0; float wi = 1.00; int l_1; int main() { sh_rules(); choo_def(); play_one(); return 1;
  • 2.
    } void sh_rules() { } void choo_def() { cout<< " "; cout << "Level of Difficulty: Beginner (1), Expert (2). "; cin >> l_1; cout << " "; if(l_1 != 1) { if(l_1 != 2) { cin.clear(); cin.ignore(numeric_limits::max(), ' '); cout << "Please enter your l_1 of difficulty using the number 1 or 2." << endl; choo_def(); } } } void play_one() { char p1 = 'N'; do { char dr = 'H'; int deck[52]; inti(deck); dhudd(deck, 51); int pHand[10] = {0}; int dHand[10] = {0}; add_hand(pHand, toca(deck)); add_hand(dHand, toca(deck));
  • 3.
    add_hand(pHand, toca(deck)); add_hand(dHand, toca(deck)); cout<< "The Dealer dhuddd and dealt you each your cards. "; nbjack(pHand, dHand, 'y'); bbjack(pHand, dHand, 'Y'); cout << "Your hand:" << endl; card_s(pHand, 10, false); cout << " Dealer's hand:" << endl; card_s(dHand, 10, true); sffot(pHand); while (draw_u(dr)) { add_hand(pHand, toca(deck)); cout << "The Dealer dealt you another card. " << endl; cout << "Your hand:" << endl; card_s(pHand, 10, false); sffot(pHand); cbust(pHand, dHand, 'y'); bbjack(pHand, dHand, 'Y'); } stand_hit(dHand, deck, pHand); cout << endl; cbust(pHand, dHand, 'Y'); bbjack(pHand, dHand, 'Y'); wiwh(pHand, dHand); cout << endl; cout << "Winnings multiplier: " << wi << endl; } while (pla_hand(p1)); } void inti(int deck[]) {
  • 4.
    int Rank =101; int i = 0; for (i = 0 ; i<=13; i++) { deck[i] = Rank++; } for (i = 13 ; i<=26; i++) { deck[i] = Rank++ + 100 - 14; } for (i = 26 ; i<=39; i++) { deck[i] = Rank++ + 200 - 28; } for (i = 39 ; i<=51; i++) { deck[i] = Rank++ + 300 - 42; } } void dhudd(int deck[], int size) { for(int i = 0; i < 500; i++) { int T1 = 0; int R1 = Rn_get(0, size); int R2 = Rn_get(0, size); T1 = deck[R1]; deck[R1] = deck[R2]; deck[R2] = T1; } } void show_card(int card) { if(card == 0) { cout << "";
  • 5.
    } else { switch(card % 100) { case1: cout << "A"; break; case 11: cout << "J"; break; case 12: cout << "K"; break; case 13: cout << "Q"; break; case 14: cout << "A"; break; default: cout << card % 100; } } if(card == 0) { cout << ""; } else { if((card >= 101) && (card <=114)) { cout << static_cast(3); } else if ((card >= 201) && (card <= 214)) {
  • 6.
    cout << static_cast(4); } elseif ((card >= 301) && (card <= 314)) { cout << static_cast(5); } else if ((card >= 401) && (card <= 414)) { cout << static_cast(6); } } } void card_s(const int deck[], int numCards, bool hideFirstCard) { if(hideFirstCard) { cout << "** "; } else { show_card(deck[0]); cout << " "; } for(int i = 1; i < numCards; i++) { if(deck[i] != 0) { show_card(deck[i]); cout << " "; } else { cout << ""; } }
  • 7.
    } void d_deck(int deck[],int size) { for(int i = 0; i < size; i++) { cout << i + 1 << ".) " << deck[i] << endl; } } int Rn_get(int low, int high) { static bool firstTime=true; int randNum; if (firstTime) { srand( static_cast(time(NULL)) ); firstTime=false; } randNum = rand() % (high-low+1) + low; return randNum; } int cvalue(int card) { int cardVal; switch(card % 100) { case 1: cardVal = 11; break; case 11: case 12: case 13: cardVal = 10; break; case 14: cardVal = 1; break; default: cardVal = (card % 100);
  • 8.
    } return cardVal; } int toca(intdeck[]) { for(int i = 0; i < 51; i++) { if(deck[i] != 0) { tcard = deck[i]; deck[i] = 0; return tcard; } } } void add_hand(int hand[], int cardToAdd) { for(int i = 0; i < 9; i++) { if(hand[i] == 0) { hand[i] = cardToAdd; break; } } } void stand_hit(int dHand[], int deck[], int pHand[]) { for(int i = 0; i < 9; i++) { if(handva(dHand) < 17) { if(l_1 == 1) { add_hand(dHand, toca(deck)); }
  • 9.
    else if( l_1== 2) { add_hand(dHand, toca(deck)); go_to(dHand, pHand); } } else { if(i == 0) { cout << "The Dealer stands." << endl; break; } else if(i == 1) { cout << "The Dealer hit a card and stands." << endl; break; } else { cout << "The Dealer hit " << i << " cards and stands." << endl; break; } } if(l_1 == 1); else if( l_1 == 2) { go_to(dHand, pHand); } } } int handva(const int hand[]) { int addcvalues = 0; for(int i = 0; i < 9; i++)
  • 10.
    { addcvalues = addcvalues+ cvalue(hand[i]); } return addcvalues; } bool pla_hand(char& p1) { cout << endl << " Would you like to play another hand? (Y/N) "; cin >> p1; cout << " " << endl; if(p1 == 'y' || p1 == 'Y') { play_one(); return(true); } else { return(false); } } bool draw_u(char& dr) { cout << endl << " Would you like to hit or stand? (H/S) "; cin >> dr; cout << " "; if(dr == 'h' || dr == 'H') { return(true); } else { return(false); } } void wiwh(const int pHand[], const int dHand[])
  • 11.
    { int playerScore =handva(pHand); int dealerScore = handva(dHand); scboard(pHand, dHand); if( (playerScore < 22) && (playerScore > dealerScore) || ( (dealerScore > 21) && (playerScore < 22) ) ) { cout << " "; cout << "You wi!" << endl; wi++; } else { if(playerScore == dealerScore) { cout << " "; cout << "Push in the Dealer's favor. 0:1 payout." << endl; } //loose //Display message and compute new winings multiplier else { cout << " "; cout << "You lose." << endl; wi--; } } } void cbust(const int pHand[], const int dHand[], char p1) { int playerScore = handva(pHand); int dealerScore = handva(dHand); if(playerScore > 21)
  • 12.
    { cout << "Youbust with " << handva(pHand) << " points." << endl; cout << " " << endl; wi--; cout << "Winnings multiplier: " << wi << endl; pla_hand(p1); } else if(dealerScore > 21) { scboard(pHand, dHand); cout << " " << endl; cout << "The Dealer went bust. You Win!" << endl; cout << " " << endl; wi++; cout << "Winnings multiplier: " << wi << endl; pla_hand(p1); } } void bakkdor(const int hand[]) { if((cvalue(hand[0])%100 == 10)&&(cvalue(hand[1])%100 == 11))//I did not use 1 as an argument because 11 is the default //value and this happens before the user can choose. { cout << " "; cout << "You pulled a Backdoor Kenny! " << endl; cout << "Win an additional 1:4 payout " << endl; wi = wi + .25; cout << "Winnings multiplier: " << wi << endl; } } void bbjack(const int pHand[], const int dHand[], char p1) { int playerScore = handva(pHand); int dealerScore = handva(dHand); if((playerScore == 21) && (dealerScore != 21))
  • 13.
    { cout << " "; scboard(pHand, dHand); cout << " "; cout << "Blackjack! You wi a 3:2 payout." << endl; wi = wi + 1.5; cout << " "; cout << "Winnings multiplier: " << wi << endl; bakkdor(pHand); pla_hand(p1); } else if((playerScore == 21) && (dealerScore == 21)) { scboard(pHand, dHand); cout << " "; cout << "The Dealer and you both got Blackjack. Push in your favor at 1:1 payout!" << endl; wi++; cout << " "; cout << "Winnings multiplier: " << wi << endl; pla_hand(p1); } } void nbjack(const int pHand[], const int dHand[], char p1) { int playerScore = handva(pHand); int dealerScore = handva(dHand); if((playerScore == 21) && (dealerScore != 21)) { scboard(pHand, dHand); cout << " "; cout << "Natural Blackjack! You wi a 3:2 payout."; wi = wi + 1.5; bakkdor(pHand); pla_hand(p1); }
  • 14.
    } void scboard(const intpHand[], const int dHand[]) { cout << "p1er hand: "; card_s(pHand, 10, false); cout << " ("<< handva(pHand) << "pts)."<> softOrHard; if(softOrHard == 1) { if(checkAce == 11) pHand[i] = pHand[i] + 13; } else if(softOrHard == 11) { if(checkAce == 1) { pHand[i] = pHand[i] - 13; } } else if (softOrHard != 1 || softOrHard != 11) { cin.clear(); cin.ignore(numeric_limits::max(), ' '); cout << " Please enter the number 1 or 11." << endl; sffot(pHand); } } } } void go_to(int dHand[], int pHand[]) { for(int i=0; i<9; i++) { if(cvalue(dHand[i]) == 1 || cvalue(dHand[i]) == 11) {
  • 15.
    if(cvalue(dHand[i]) == 11) { if(handva(dHand) - cvalue(dHand[i]) + 1 > handva(pHand)) { if (handva(dHand) - cvalue(dHand[i]) + 1 < 22) { dHand[i] = dHand[i] + 13; } } else if (handva(dHand) > 21) { dHand[i] = dHand[i] + 13; } } else { if (handva(dHand) - cvalue(dHand[i]) + 11 > handva(pHand)) { if (handva(dHand) - cvalue(dHand[i]) + 11 < 22) { dHand[i] = dHand[i] - 13; } } } } } } Solution ANS: #include int Rn_get(int low, int high); int cvalue(int card); int toca(int deck[]); int handva(const int hand[]);
  • 16.
    bool pla_hand(char&); bool draw_u(char&); voidinti(int deck[]); void d_deck(int deck[], int size); void dhudd(int deck[], int size); void show_card(int card); void card_s(const int cards[], int numCards, bool hideFirstCard); void wiwh(const int pHand[], const int dHand[]); void cbust(const int pHand[], const int dHand[], char p1); void bbjack(const int pHand[], const int dHand[], char p1); void nbjack(const int pHand[], const int dHand[], char p1); void play_one(); void scboard(const int pHand[], const int dHand[]); void bakkdor(const int hand[]); void sffot(int pHand[]); void go_to(int dHand[], int pHand[]); void choo_def(); void sh_rules(); void add_hand(int hand[], int cardToAdd); void stand_hit(int dHand[], int deck[], int pHand[]); int tcard = 0; float wi = 1.00; int l_1; int main() { sh_rules(); choo_def(); play_one(); return 1; } void sh_rules() { } void choo_def() {
  • 17.
    cout << ""; cout << "Level of Difficulty: Beginner (1), Expert (2). "; cin >> l_1; cout << " "; if(l_1 != 1) { if(l_1 != 2) { cin.clear(); cin.ignore(numeric_limits::max(), ' '); cout << "Please enter your l_1 of difficulty using the number 1 or 2." << endl; choo_def(); } } } void play_one() { char p1 = 'N'; do { char dr = 'H'; int deck[52]; inti(deck); dhudd(deck, 51); int pHand[10] = {0}; int dHand[10] = {0}; add_hand(pHand, toca(deck)); add_hand(dHand, toca(deck)); add_hand(pHand, toca(deck)); add_hand(dHand, toca(deck)); cout << "The Dealer dhuddd and dealt you each your cards. "; nbjack(pHand, dHand, 'y'); bbjack(pHand, dHand, 'Y'); cout << "Your hand:" << endl; card_s(pHand, 10, false);
  • 18.
    cout << " Dealer's hand:" << endl; card_s(dHand, 10, true); sffot(pHand); while (draw_u(dr)) { add_hand(pHand, toca(deck)); cout << "The Dealer dealt you another card. " << endl; cout << "Your hand:" << endl; card_s(pHand, 10, false); sffot(pHand); cbust(pHand, dHand, 'y'); bbjack(pHand, dHand, 'Y'); } stand_hit(dHand, deck, pHand); cout << endl; cbust(pHand, dHand, 'Y'); bbjack(pHand, dHand, 'Y'); wiwh(pHand, dHand); cout << endl; cout << "Winnings multiplier: " << wi << endl; } while (pla_hand(p1)); } void inti(int deck[]) { int Rank = 101; int i = 0; for (i = 0 ; i<=13; i++) { deck[i] = Rank++; } for (i = 13 ; i<=26; i++)
  • 19.
    { deck[i] = Rank+++ 100 - 14; } for (i = 26 ; i<=39; i++) { deck[i] = Rank++ + 200 - 28; } for (i = 39 ; i<=51; i++) { deck[i] = Rank++ + 300 - 42; } } void dhudd(int deck[], int size) { for(int i = 0; i < 500; i++) { int T1 = 0; int R1 = Rn_get(0, size); int R2 = Rn_get(0, size); T1 = deck[R1]; deck[R1] = deck[R2]; deck[R2] = T1; } } void show_card(int card) { if(card == 0) { cout << ""; } else { switch(card % 100) { case 1: cout << "A";
  • 20.
    break; case 11: cout <<"J"; break; case 12: cout << "K"; break; case 13: cout << "Q"; break; case 14: cout << "A"; break; default: cout << card % 100; } } if(card == 0) { cout << ""; } else { if((card >= 101) && (card <=114)) { cout << static_cast(3); } else if ((card >= 201) && (card <= 214)) { cout << static_cast(4); } else if ((card >= 301) && (card <= 314)) { cout << static_cast(5); } else if ((card >= 401) && (card <= 414))
  • 21.
    { cout << static_cast(6); } } } voidcard_s(const int deck[], int numCards, bool hideFirstCard) { if(hideFirstCard) { cout << "** "; } else { show_card(deck[0]); cout << " "; } for(int i = 1; i < numCards; i++) { if(deck[i] != 0) { show_card(deck[i]); cout << " "; } else { cout << ""; } } } void d_deck(int deck[], int size) { for(int i = 0; i < size; i++) { cout << i + 1 << ".) " << deck[i] << endl; }
  • 22.
    } int Rn_get(int low,int high) { static bool firstTime=true; int randNum; if (firstTime) { srand( static_cast(time(NULL)) ); firstTime=false; } randNum = rand() % (high-low+1) + low; return randNum; } int cvalue(int card) { int cardVal; switch(card % 100) { case 1: cardVal = 11; break; case 11: case 12: case 13: cardVal = 10; break; case 14: cardVal = 1; break; default: cardVal = (card % 100); } return cardVal; } int toca(int deck[]) { for(int i = 0; i < 51; i++) {
  • 23.
    if(deck[i] != 0) { tcard= deck[i]; deck[i] = 0; return tcard; } } } void add_hand(int hand[], int cardToAdd) { for(int i = 0; i < 9; i++) { if(hand[i] == 0) { hand[i] = cardToAdd; break; } } } void stand_hit(int dHand[], int deck[], int pHand[]) { for(int i = 0; i < 9; i++) { if(handva(dHand) < 17) { if(l_1 == 1) { add_hand(dHand, toca(deck)); } else if( l_1 == 2) { add_hand(dHand, toca(deck)); go_to(dHand, pHand); } } else
  • 24.
    { if(i == 0) { cout<< "The Dealer stands." << endl; break; } else if(i == 1) { cout << "The Dealer hit a card and stands." << endl; break; } else { cout << "The Dealer hit " << i << " cards and stands." << endl; break; } } if(l_1 == 1); else if( l_1 == 2) { go_to(dHand, pHand); } } } int handva(const int hand[]) { int addcvalues = 0; for(int i = 0; i < 9; i++) { addcvalues = addcvalues + cvalue(hand[i]); } return addcvalues; } bool pla_hand(char& p1) {
  • 25.
    cout << endl<< " Would you like to play another hand? (Y/N) "; cin >> p1; cout << " " << endl; if(p1 == 'y' || p1 == 'Y') { play_one(); return(true); } else { return(false); } } bool draw_u(char& dr) { cout << endl << " Would you like to hit or stand? (H/S) "; cin >> dr; cout << " "; if(dr == 'h' || dr == 'H') { return(true); } else { return(false); } } void wiwh(const int pHand[], const int dHand[]) { int playerScore = handva(pHand); int dealerScore = handva(dHand); scboard(pHand, dHand); if( (playerScore < 22) && (playerScore > dealerScore)
  • 26.
    || ( (dealerScore> 21) && (playerScore < 22) ) ) { cout << " "; cout << "You wi!" << endl; wi++; } else { if(playerScore == dealerScore) { cout << " "; cout << "Push in the Dealer's favor. 0:1 payout." << endl; } //loose //Display message and compute new winings multiplier else { cout << " "; cout << "You lose." << endl; wi--; } } } void cbust(const int pHand[], const int dHand[], char p1) { int playerScore = handva(pHand); int dealerScore = handva(dHand); if(playerScore > 21) { cout << "You bust with " << handva(pHand) << " points." << endl; cout << " " << endl; wi--; cout << "Winnings multiplier: " << wi << endl; pla_hand(p1); }
  • 27.
    else if(dealerScore >21) { scboard(pHand, dHand); cout << " " << endl; cout << "The Dealer went bust. You Win!" << endl; cout << " " << endl; wi++; cout << "Winnings multiplier: " << wi << endl; pla_hand(p1); } } void bakkdor(const int hand[]) { if((cvalue(hand[0])%100 == 10)&&(cvalue(hand[1])%100 == 11))//I did not use 1 as an argument because 11 is the default //value and this happens before the user can choose. { cout << " "; cout << "You pulled a Backdoor Kenny! " << endl; cout << "Win an additional 1:4 payout " << endl; wi = wi + .25; cout << "Winnings multiplier: " << wi << endl; } } void bbjack(const int pHand[], const int dHand[], char p1) { int playerScore = handva(pHand); int dealerScore = handva(dHand); if((playerScore == 21) && (dealerScore != 21)) { cout << " "; scboard(pHand, dHand); cout << " "; cout << "Blackjack! You wi a 3:2 payout." << endl; wi = wi + 1.5; cout << " ";
  • 28.
    cout << "Winningsmultiplier: " << wi << endl; bakkdor(pHand); pla_hand(p1); } else if((playerScore == 21) && (dealerScore == 21)) { scboard(pHand, dHand); cout << " "; cout << "The Dealer and you both got Blackjack. Push in your favor at 1:1 payout!" << endl; wi++; cout << " "; cout << "Winnings multiplier: " << wi << endl; pla_hand(p1); } } void nbjack(const int pHand[], const int dHand[], char p1) { int playerScore = handva(pHand); int dealerScore = handva(dHand); if((playerScore == 21) && (dealerScore != 21)) { scboard(pHand, dHand); cout << " "; cout << "Natural Blackjack! You wi a 3:2 payout."; wi = wi + 1.5; bakkdor(pHand); pla_hand(p1); } } void scboard(const int pHand[], const int dHand[]) { cout << "p1er hand: "; card_s(pHand, 10, false); cout << " ("<< handva(pHand) << "pts)."<> softOrHard; if(softOrHard == 1)
  • 29.
    { if(checkAce == 11) pHand[i]= pHand[i] + 13; } else if(softOrHard == 11) { if(checkAce == 1) { pHand[i] = pHand[i] - 13; } } else if (softOrHard != 1 || softOrHard != 11) { cin.clear(); cin.ignore(numeric_limits::max(), ' '); cout << " Please enter the number 1 or 11." << endl; sffot(pHand); } } } } void go_to(int dHand[], int pHand[]) { for(int i=0; i<9; i++) { if(cvalue(dHand[i]) == 1 || cvalue(dHand[i]) == 11) { if(cvalue(dHand[i]) == 11) { if (handva(dHand) - cvalue(dHand[i]) + 1 > handva(pHand)) { if (handva(dHand) - cvalue(dHand[i]) + 1 < 22) { dHand[i] = dHand[i] + 13;
  • 30.
    } } else if (handva(dHand)> 21) { dHand[i] = dHand[i] + 13; } } else { if (handva(dHand) - cvalue(dHand[i]) + 11 > handva(pHand)) { if (handva(dHand) - cvalue(dHand[i]) + 11 < 22) { dHand[i] = dHand[i] - 13; } } } } } }