Can you make sure it runs in C programming Use the file here Blackjack.c as the
ID: 3695702 • Letter: C
Question
Can you make sure it runs in C programming
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 <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
You will provide one or more implementations of four separate abstractions for this project: a deck of
cards, a blackjack hand, a blackjack player, and a game driver. All files referenced in this specification
are located at:
/afs/[url removed, login to view]
You may copy them to your private directory space, but may not modify them in any way. This will help
ensure that your submitted project compiles correctly. For this project, the penalty for code that does
not compile will be severe, regardless of the reason.
The Deck ADT
Your first task is to implement the following ADT representing a deck of cards:
class DeckEmpty { // An exception type
};
const int DeckSize = 52;
class Deck {
// A standard deck of 52 playing cards---no jokers
Card deck[DeckSize]; // The deck of cards
int next; // The next card to deal
public:
Deck();
// EFFECTS: constructs a "newly opened" deck of cards. first the
// spades from 2-A, then the hearts, then the clubs, then the
// diamonds. The first card dealt should be the 2 of Spades.
void reset();
// EFFECTS: resets the deck to the state of a "newly opened" deck
// of cards:
void shuffle(int n);
// REQUIRES: n is between 0 and 52, inclusive.
// MODIFIES: this
// EFFECTS: cut the deck into two segments: the first n cards,
// called the "left", and the rest called the "right". Note that
// either right or left might be empty. Then, rearrange the deck
// to be the first card of the right, then the first card of the
// left, the 2nd of right, the 2nd of left, and so on. Once one
// side is exhausted, fill in the remainder of the deck with the
// cards remaining in the othe rside. Finally, make the first
// card in this shuffled deck the next card to deal. For example,
// shuffle(26) on a newly-reset() deck results in: 2-clubs,
// 2-spades, 3-clubs, 3-spades ... A-diamonds, A-hearts.
//
// Note: if shuffle is called on a deck that has already had some
// cards dealt, those cards should first be restored to the deck
// in the order in which they were dealt, preserving the most
// recent post-shuffled/post-reset state.
Card deal();
// MODIFIES: this
// EFFECTS: returns the next card to be dealt. If no cards
// remain, throws an instance of DeckEmpty.
int cardsLeft();
// EFFECTS: returns the number of cards in the deck that have not
// been dealt since the last reset/shuffle.
};
The Deck ADT is specified in deck.h. The Deck ADT depends on the following Card type:
enum Suit {
SPADES, HEARTS, CLUBS, DIAMONDS
};
extern const char *SuitNames[DIAMONDS+1];
enum Spot {
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN,
JACK, QUEEN, KING, ACE
};
extern const char *SpotNames[ACE+1];
struct Card {
Spot spot;
Suit suit;
};
which is declared in card.h, implemented by [url removed, login to view], and included by deck.h. The file
[url removed, login to view] defines SpotNames and SuitNames for you, so that SuitNames[HEARTS] evaluates
to "Hearts", and so on.
You are to put your implementation of this ADT in a file named "[url removed, login to view]". You must use exactly this
name.
The Hand Interface
Your second task is to implement the following ADT representing a blackjack hand:
struct HandValue {
int count; // Value of hand
bool soft; // true if hand value is a soft count
};
class Hand {
// OVERVIEW: A blackjack hand of zero or more cards
// Note: this really is the only private state you need!
HandValue curValue;
public:
Hand();
// EFFECTS: establishes an empty blackjack hand.
void discardAll();
// MODIFIES: this
// EFFECTS: discards any cards presently held, restoring the state
// of the hand to that of an empty blackjack hand.
void addCard(Card c);
// MODIFIES: this
// EFFECTS: adds the card "c" to those presently held. The
// count field is the highest blackjack total possible without
// going over 21. The soft field should be true if and only if at
// least one ACE is present, and its value is counted as 11 rather
// than 1. If the hand is over 21, any value over 21 may be
// returned.
HandValue handValue() const;
// EFFECTS: returns the present value of the blackjack hand.
//
// Note: the const qualifier at the end of handValue means that
// you are not allowed to change any member variables inside
// handValue. It is required because Players only get const Hands
// passed to them, and therefore can only call methods guaranteed
// not to change the hand.
};
The Hand ADT is specified in hand.h The Hand ADT depends on the Card type, and includes card.h.
You are to put your implementation of this ADT in a file named "[url removed, login to view]". You must use exactly this
name.
The Player Interface
Your third task is to implement three different blackjack players. The interface for a Player is:
class Player {
// A virtual base class, providing the player interface
public:
virtual int bet(unsigned int bankroll,
unsigned int minimum) = 0;
// REQUIRES: bankroll >= minimum
// EFFECTS: returns the player's bet, between minimum and bankroll
// inclusive
virtual bool draw(Card dealer, // Dealer's "up card"
const Hand &player) = 0; // Player's current hand
// EFFECTS: returns true if the player wishes to be dealt another
// card, false otherwise.
virtual void expose(Card c) = 0;
// EFFECTS: allows the player to "see" the newly-exposed card c.
// For example, each card that is dealt "face up" is expose()d.
// Likewise, if the dealer must show his/her "hole card", it is
// also expose()’d. Note: not all cards dealt are expose()’d---if
// the player goes over 21 or is dealt a natural 21, the dealer
// does not expose his/her hole card.
virtual void shuffled() = 0;
// EFFECTS: tells the player that the deck has been re-shuffled.
};