Use the file here Blackjack.c as the basis for this program. You must make the f
ID: 3694949 • Letter: U
Question
Use the file here Blackjack.c as the basis for this program.
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.
This is the c file:
#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
program:
#include <Windows.h>
#include <stdlib.h>
#define CLUBS 0
#define DIAMONDS 1
#define HEARTS 2
#define SPADES 3
{
public void run()
{
/*
The dealer hits on a total of 16 or less
and stands on a total of 17 or more. Dealer wins ties.
A new deck of cards is used for each game.
*/
int money,playercards,delarcards; // Amount of money the user has.
int bet; // Amount user bets on a game
printf("Welcome to the game of blackjack.");
money = 100; // User starts with $100.
while (true)
{
printf("You have " + money + " dollars.");
do
{
printf("How many dollars do you want to bet? (Enter 0 to end.)");
printf("? ");
if (bet < 0 || bet > money)
{
printf("Your answer must be between 0 and " + money + '.');
}
} while (bet < 0 || bet > money);
if (bet == 0)
{
break;
}
userWins = playBlackjack();
if (userWins)
{
money = money + bet;
} else
{
money = money - bet;
}
if (money == 0)
{
printf("Looks like you've are out of money!");
break;
}
}
printf("You leave with $" + money + '.');
} // end main()
Void boolean playBlackjack()
{
// Let the user play one game of Blackjack.
// Return true if the user wins, false if the user loses.
Vector dealerHand; // The dealer's hand.
Vector userHand; // The user's hand.
// Create an unshuffled deck of cards.
deck = new int[52];
int cardCt = 0; // How many cards have been created so far.
for (int suit = 0; suit <= 3; suit++)
{
for (int value = 1; value <= 13; value++)
{
deck[cardCt] = value;
cardCt++;
}
}
currentPosition = 0;
dealerHand = new Vector();
userHand = new Vector();
/* Shuffle the deck, then deal two cards to each player. */
shuffle()
PlayerCards = DealerCards = CurrentDealCard = 0;
Player[PlayerCards++] = Deck[CurrentDealCard++];
Dealer[DealerCards++] = Deck[CurrentDealCard++];
Player[PlayerCards++] = Deck[CurrentDealCard++];
Dealer[DealerCards++] = Deck[CurrentDealCard++];
/* Check if one of the players has Blackjack (two cards totaling to 21).
The player with Blackjack wins the game. Dealer wins ties.
*/
if (value(dealerHand) == 21)
{
printf("Dealer has the " + showCard(getCard(dealerHand, 0)) + " and the " + showCard(getCard(dealerHand, 1)) + ".");
printf("User has the " + showCard(getCard(userHand, 0)) + " and the " + showCard(getCard(userHand, 1)) + ".");
printf("Dealer has Blackjack. Dealer wins.");
return false;
}
if (value(userHand) == 21)
{
printf("Dealer has the " + showCard(getCard(dealerHand, 0)) + " and the " + showCard(getCard(dealerHand, 1)) + ".");
printf("User has the " + showCard(getCard(userHand, 0)) + " and the " + showCard(getCard(userHand, 1)) + ".");
printf(“”);
printf("You have Blackjack. You win.");
return true;
}
/* If neither player has Blackjack, play the game. The user gets a chance
to draw cards (i.e., to "Hit"). The while loop ends when the user
chooses to "Stand" or when the user goes over 21.
*/
while (true)
{
/* Display user's cards, and let user decide to Hit or Stand. */
printf(“”);
printf();
printf("Your cards are:");
for (int i = 0; i < userHand.size(); i++)
{
printf(" " + showCard(getCard(userHand, i)));
}
printf("Your total is " + value(userHand));
printf(“”);
printf("Dealer is showing the " + showCard(getCard(dealerHand, 0)));
printf();
printf("Hit (H) or Stand (S)? ");
char userAction; // User's response, 'H' or 'S'.
do
{
userAction = Character.toUpperCase(scanner.next().charAt(0));
if (userAction != 'H' && userAction != 'S')
{
printf("Please respond H or S: ");
}
} while (userAction != 'H' && userAction != 'S');
/* If the user Hits, the user gets a card. If the user Stands, the
dealer gets a chance to draw and the game ends.
*/
if (userAction == 'S')
{
// Loop ends; user is done taking cards.
break;
} else
{ // userAction is 'H'.
// Give the user a card. If the user goes over 21, the user loses.
printf(“”);
printf("User hits.");
printf("Your card is the " + showCard(newCard));
printf("Your total is now " + value(userHand));
if (value(userHand) > 21)
{
printf(“”);
printf("You busted by going over 21. You lose.");
printf("Dealer's other card was the " + showCard(getCard(dealerHand,1)));
return false;
}
}
} // end while loop
/* If we get to this point, the user has Stood with 21 or less. Now, it's
the dealer's chance to draw. Dealer draws cards until the dealer's total is > 16.
*/
printf (“”);
printf ("User stands.");
printf ("Dealer's cards are");
printf (" " + showCard(getCard(dealerHand, 0)));
printf (" " + showCard(getCard(dealerHand, 1)));
while (value(dealerHand) <= 16)
{
int newCard = dealCard();
printf ("Dealer hits and gets the " + showCard(newCard));
}
printf ("Dealer's total is " + value(dealerHand));
/* Now, the winner can be declared. */
printf (“”);
if (value(dealerHand) > 21)
{
printf ("Dealer busted by going over 21. You win.");
return true;
} else
{
if (value(dealerHand) == value(userHand))
{
printf ("Dealer wins on a tie. You lose.");
return false;
} else
{
if (value(dealerHand) > value(userHand))
{
printf ("Dealer wins, " + value(dealerHand) + " points to " + value(userHand) + ".");
return false;
} else
{
printf ("You win, " + value(userHand) + " points to " + value(dealerHand) + ".");
return true;
}
}
}
} // end playBlackjack()
Void int dealCard()
{
// Deals one card from the deck and returns it.
if (currentPosition == 52)
{
shuffle();
}
currentPosition++;
return deck[currentPosition - 1];
}
public void shuffle()
{
// Put all the used cards back into the deck, and shuffle it into
// a random order.
for (int i = 51; i > 0; i--)
{
int rand = (int) (Math.random() * (i + 1));
int temp = deck[i];
deck[i] = deck[rand];
deck[rand] = temp;
}
currentPosition = 0;
}
void int getCard(Vector hand, int position)
{
// Get the card from the hand in given position, where positions
// are numbered starting from 0. If the specified position is
// not the position number of a card in the hand, then null
// is returned.
if (position >= 0 && position < hand.size())
{
Printf(“position of integer value”);
} else
{
return 0;
}
}
Void int value(Vector hand)
{
// Returns the value of this hand for the
// game of Blackjack.
int val; // The value computed for the hand.
boolean ace; // This will be set to true if the
// hand contains an ace.
int cards; // Number of cards in the hand.
val = 0;
ace = false;
cards = hand.size();
for (int i = 0; i < cards; i++)
{
// Add the value of the i-th card in the hand.
int card; // The i-th card;
int cardVal; // The blackjack value of the i-th card.
card = getCard(hand, i);
cardVal = getCardValue(card); // The normal value, 1 to 13.
if (cardVal > 10)
{
cardVal = 10; // For a Jack, Queen, or King.
}
if (cardVal == 1)
{
ace = true; // There is at least one ace.
}
val = val + cardVal;
}
// Now, val is the value of the hand, counting any ace as 1.
// If there is an ace, and if changing its value from 1 to
// 11 would leave the score less than or equal to 21,
if (ace == true && val + 10 <= 21)
{
val = val + 10;
}
return val;
}
Void int getCardValue(int card)
{
int result = card;
switch (card)
{
case 11:
case 12:
case 13:
result = 10;
}
return result;
}
Void String showCard(int card)
{
switch (card)
{
case 1:
return "Ace";
case 2:
return "2";
case 3:
return "3";
case 4:
return "4";
case 5:
return "5";
case 6:
return "6";
case 7:
return "7";
case 8:
return "8";
case 9:
return "9";
case 10:
return "10";
case 11:
return "Jack";
case 12:
return "Queen";
case 13:
return "King";
default:
return "??";
}
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.