You must make the following: 1. Do not show the dealer hole card until after the
ID: 3694024 • Letter: Y
Question
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.
// 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;
}
Explanation / Answer
// 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;
int PlayerStand;
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 PlayerStand)
{
int i;
printf(" Dealer: ");
for (i = 0; i < DealerCards; i++){
/*Check if it is the dealer hold*/
if(i==1){
/*Only show dealer hold if the player stand*/
if(PlayerStand==1){
DrawCard(Dealer[i] % 13, Dealer[i] / 13), printf( " " );
}
}else{
DrawCard(Dealer[i] % 13, Dealer[i] / 13), printf( " " );
}
}
/*Show the Dealer score only if Player Stand*/
if(PlayerStand==1){
printf("Dealer Score is %d Player: ", DealerScore);
}
for (i = 0; i < PlayerCards; i++) DrawCard(Player[i] % 13, Player[i] / 13), printf(" ");
if(PlayerDoubled==1){
printf("Player has Doubled Down");
}
printf("Player Score is %d ", PlayerScore);
}
void DisplayDoubleDownChoice(){
printf("Would you like to Double Down? ");
printf("1. Yes 2. No ");
}
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;
int aces=0;
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;
aces++;
}
/*As we have stored the number of aces, we use them to get as near as possible to 21.
We have added all aces as 11. If we bust, we start substracting 10 until we run out of aces
*/
while(PlayerScore>21 && aces>0){
PlayerScore-=10;
aces--;
}
}
aces=0;
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;
aces++;
}
/*As we have stored the number of aces, we use them to get as near as possible to 21.
We have added all aces as 11. If we bust, we start substracting 10 until we run out of aces
*/
while(DealerScore>21 && aces>0){
DealerScore-=10;
aces--;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int choice;
int doubleDown;
srand(GetTickCount());
DoShuffle();
DealCards();
CalculateScores();
DisplayDealtCards();
DisplayDoubleDownChoiceChoice();
PlayerDoubled = GetChoice(0,1);
DisplayChoice();
choice = GetChoice(1,2);
/*Check if player double down*/
if(doubleDown==0){
/*Keep Asking until the Player Stand or Bust*/
while (choice == 1 && PlayerScore<21)
{
Player[PlayerCards++] = Deck[CurrentDealCard++];
CalculateScores();
DisplayDealtCards();
}
}
PlayerStand=1;
/*At this point the Palyer has ended, now play the Dealer*/
if(DealerScore < 17){
Dealer[DealCardsCards++] = Deck[CurrentDealCard++];
CalculateScores();
DisplayDealtCards();
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.