Use the file Blackjack.c as the basis for this C program. // Blackjack.c : Defin
ID: 3697088 • Letter: U
Question
Use the file Blackjack.c as the basis for this C program.
// Blackjack.c : Defines the entry point for the console application.
//
#include <Windows.h>
#include <stdlib.h>
#define CLUBS 0
#define DIAMONDS 1
#define HEARTS 2
#define SPADES 3
enum{ TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE };
int Deck[52];
int CurrentDealCard;
int Dealer[20], Player[20];
int DealerCards, PlayerCards;
int DealerScore, PlayerScore;
void DoShuffle()
{
int i, nextcard;
int Used[52];
/* Here we clear out Used array to zeros, which indicates no
values have been used. */
for (i = 0; i < 52; i++) Used[i] = 0;
/* Loop through the deck of cards, there are 52 values */
for (i = 0; i < 52; i++)
{
/* Here we used a do-while. If the card has already been used,
we need to keep generating random numbers until we find a card
that has not been used. */
do
{
nextcard = rand() % 52; /* Value from 0 to 51 */
} while (Used[nextcard] == 1); /* This is our check */
/* Here we set to 1 so that we remember that this card has been used */
Used[nextcard] = 1;
/* Finally, put the card in the deck. */
Deck[i] = nextcard;
}
}
void DrawCard(int rank, int suit)
{
switch (rank)
{
case TWO:
printf("Two ");
break;
case THREE:
printf("Three ");
break;
case FOUR:
printf("Four ");
break;
case FIVE:
printf("Five ");
break;
case SIX:
printf("Six ");
break;
case SEVEN:
printf("Seven ");
break;
case EIGHT:
printf("Eight ");
break;
case NINE:
printf("Nine ");
break;
case TEN:
printf("Ten ");
break;
case JACK:
printf("Jack ");
break;
case QUEEN:
printf("Queen ");
break;
case KING:
printf("King ");
break;
case ACE:
printf("Ace ");
break;
}
switch (suit)
{
case CLUBS:
printf("Clubs");
break;
case DIAMONDS:
printf("Diamonds");
break;
case HEARTS:
printf("Hearts");
break;
case SPADES:
printf("Spades");
break;
}
}
void DisplayShuffledDeck()
{
int i, suit, rank;
for (i = 0; i < 52; i++)
{
suit = Deck[i] / 13;
rank = Deck[i] % 13;
DrawCard(rank, suit);
printf(" ");
}
}
void DealCards()
{
PlayerCards = DealerCards = CurrentDealCard = 0;
Player[PlayerCards++] = Deck[CurrentDealCard++];
Dealer[DealerCards++] = Deck[CurrentDealCard++];
Player[PlayerCards++] = Deck[CurrentDealCard++];
Dealer[DealerCards++] = Deck[CurrentDealCard++];
}
void DisplayDealtCards()
{
int i;
printf(" Dealer: ");
for (i = 0; i < DealerCards; i++) DrawCard(Dealer[i] % 13, Dealer[i] / 13), printf( " " );
printf("Dealer Score is %d Player: ", DealerScore);
for (i = 0; i < PlayerCards; i++) DrawCard(Player[i] % 13, Player[i] / 13), printf(" ");
printf("Player Score is %d ", PlayerScore);
}
void DisplayChoice()
{
printf("What would you like to do? ");
printf("1. Hit 2. Stand ");
}
int GetChoice(int min, int max)
{
int choice;
do
{
printf("Make a choice from %d to %d:", min, max);
scanf("%d", &choice);
} while (choice < min || choice > max);
return(choice);
}
void CalculateScores()
{
int i, rank, suit ;
PlayerScore = 0;
for (i = 0; i < PlayerCards; i++)
{
rank = Player[i] % 13;
suit = Player[i] / 13;
if (rank >= TWO && rank <= TEN) PlayerScore += ( rank + 2 );
else if (rank >= JACK && rank <= KING) PlayerScore += 10;
else PlayerScore += 11;
}
DealerScore = 0;
for (i = 0; i < DealerCards; i++)
{
rank = Dealer[i] % 13;
suit = Dealer[i] / 13;
if (rank >= TWO && rank <= TEN) DealerScore += (rank + 2);
else if (rank >= JACK && rank <= KING) DealerScore += 10;
else DealerScore += 11;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int choice;
srand(GetTickCount());
DoShuffle();
DealCards();
CalculateScores();
DisplayDealtCards();
DisplayChoice();
choice = GetChoice(1, 2);
if (choice == 1)
{
Player[PlayerCards++] = Deck[CurrentDealCard++];
CalculateScores();
DisplayDealtCards();
}
return 0;
}
You must make the following: 1. Do not show the dealer hole card until after the player stands, and do not show the dealer score until after the player stands. 2. After the player stands, playout the dealer according to the rules <= 16 hit, >= 17 stand. 3. Fix the scoring of aces 4. Add the ability for the player to double down.
Explanation / Answer
#include <Windows.h>
#include <stdlib.h>
#define CLUBS 0
#define DIAMONDS 1
#define HEARTS 2
#define SPADES 3
enum Suit { CLUBS, HEARTS, DIAMONDS, SPADES };
enum Rank { ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE };
enum person { PLAYER, DEALER };
struct Card
{
Suit suit;
Rank rank;
};
class BlackJack
{
public:
BlackJack()
{
srand ( time(NULL) );
playerScore = 0;
dealerScore = 0;
playerBust = false;
dealerBust = false;
Card tmpCard;
for (int i = CLUBS; i < SPADES; i++)
for (int j = ONE; j <= ACE; j++)
{
tmpCard.suit = (Suit)i;
tmpCard.rank = (Rank)j;
deck.push_back(tmpCard);
}
}
void ShuffleDeck() { random_shuffle(deck.begin(), deck.end()); }
void Play()
{
bool hitMe;
cout << " Game starting.... ";
while(1)
{
cout << " Round starting... ";
hitMe = Deal(GetCard(PLAYER), PLAYER);
Deal(GetCard(DEALER), DEALER);
while (hitMe)
{
hitMe = Deal(GetCard(PLAYER), PLAYER);
Deal(GetCard(DEALER), DEALER);
}
if (playerScore > 21)
{
cout << " You bust!! ";
playerBust = true;
}
else if (dealerScore > 21)
{
cout << " Dealer busts!! ";
dealerBust = true;
}
else
{
cout << " You stand at " << playerScore << endl;
cout << "The dealer stands at " << dealerScore << endl;
}
if (playerScore > dealerScore and !playerBust)
cout << " You win!!! ";
else if (playerScore == dealerScore or !playerBust and !dealerBust)
cout << " Tie!!! Dealer wins!!! ";
else
cout << " Dealer wins!!! ";
}
}
Card GetCard(Who w)
{
Card tmpCard;
if (deck.size() != 0)
{
tmpCard = deck[deck.size()-1];
AddScore(tmpCard, w);
deck.pop_back();
}
else
{
cout << " Dealer has run out of cards. Game Over.... ";
exit(1);
}
return tmpCard;
}
void AddScore(Card c,person w)
{
bool faceCard = false;
int faceValue = 0;
switch(c.rank)
{
case JACK :
case QUEEN :
case KING : faceValue = 10;
faceCard = true;
break;
case ACE : faceValue = 11;
faceCard = true;
}
switch (w)
{
case PLAYER : if (faceCard)
playerScore += faceValue;
else
playerScore += c.rank;
break;
case DEALER : if (faceCard)
dealerScore += faceValue;
else
dealerScore += c.rank;
}
}
bool Deal(Card c,person w)
{
if (w == PLAYER)
{
char answer;
do
{
cout << " You have been dealt a" << RankToString(c.rank) << "of" << SuitToString(c.suit) << endl;
cout << "You stand at " << playerScore << endl;
cout << " Hit you? (Type Y or N or Q to quit) ";
cin >> answer;
answer = toupper(answer);
}
while (answer != 'Y' and answer != 'N' and answer != 'Q');
switch (answer)
{
case 'Y' : return true;
break;
case 'N' : return false;
break;
case 'Q' : cout << "Exiting game.... ";
exit(1);
return false;
}
}
else
{
cout << " Dealer takes a" << RankToString(c.rank)<< "of" << SuitToString(c.suit) << endl;
cout << "Dealer stands at " << dealerScore << endl;
}
}
string SuitToString(Suit s)
{
switch(s)
{
case CLUBS : return " clubs ";
case HEARTS : return " hearts ";
case DIAMONDS : return " diamonds ";
case SPADES : return " spades ";
}
}
string RankToString(Rank r)
{
switch(r)
{
case ONE :
return " one ";
case TWO :
return " two ";
case THREE :
return " three ";
case FOUR :
return " four ";
case FIVE :
return " five ";
case SIX :
return " six ";
case SEVEN :
return " seven ";
case EIGHT :
return " eight ";
case NINE :
return " nine ";
case TEN :
return " ten ";
case JACK :
return " jack ";
case QUEEN :
return " queen ";
case KING :
return " king ";
case ACE :
return " ace ";
}
}
private:
vector<Card> deck;
bool playerBust;
bool dealerBust;
int playerScore;
int dealerScore;
};
int main()
{
BlackJack game;
game.ShuffleDeck();
game.Play();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.