Assignment 10

due Thursday 14 April at 01:00 AM

For this assignment, we will continue the Blackjack game we started in assignment 9. (A solution to that assignment appears in the notes for 5 April.)

In this version, the player is dealt two cards initially, and then can choose, one at a time, whether to receive additional cards. The goal is to score as close as possible to 21, without excedding it.

Numeric cards have their scores written on them (2–10), face cards (Jack, Queen, Kind) are worth 10 points, and the Ace is worth either 1 or 11.

If your first two cards sum to 21 (an Ace and a face card), then you hav 21 points right away, and the game is over. Otherwise, you see your score and decide whether to hit (take another card) or stay (keep what you've got). You may continue hitting as long as your score is under 212. Once you exceed 21, you have busted, losing that hand.

Below are some transcripts, hints, and sample code.

1 Transcripts of sample runs

Here is an example in which we win right away:

10 Of Spades
Ace Of Spades
Your score: 21
BLACKJACK!

One where we take one additional card and stop at 18 points:

7 Of Clubs
5 Of Clubs
Your score: 12
Would you like to hit (1) or stay (0)? 1
6 Of Diamonds
Your score: 18
Would you like to hit (1) or stay (0)? 0
Your score: 18

One where we take an additional card and get lucky with an ace:

4 Of Spades
6 Of Clubs
Your score: 10
Would you like to hit (1) or stay (0)? 1
Ace Of Diamonds
Your score: 21
BLACKJACK!

One where we take an additional card and bust:

King Of Diamonds
5 Of Diamonds
Your score: 15
Would you like to hit (1) or stay (0)? 1
7 Of Diamonds
Your score: 22
BUST!

Here is an extremely interesting example: we take one card, and it's an Ace which, if we count it high (11 points), sums to 19. But we choose to hit again anyway, and get a 9. So rather than busting, now the Ace is counted as 1 point, for a total of 18.

6 Of Clubs
2 Of Spades
Your score: 8
Would you like to hit (1) or stay (0)? 1
Ace Of Hearts
Your score: 19
Would you like to hit (1) or stay (0)? 1
9 Of Spades
Your score: 18
Would you like to hit (1) or stay (0)? 0
Your score: 18

2 Some constants

You may want to use the following constants in your program, to make it easier to keep track of ranks and suits. Modify the code from assignment 9 to use them in the switch statements, for example.

const int ACE = 1;
const int JACK = 11;
const int QUEEN = 12;
const int KING = 13;

const int HEARTS = 0;
const int CLUBS = 1;
const int DIAMONDS = 2;
const int SPADES = 3;

const int TARGET_SCORE = 21;

3 Prototypes of recommended functions

int deal_a_card();
 /* As provided for assignment 9. Prints out the rank and
    suit ("Queen of Spades") but then returns the number
    corresponding to the rank (ranging from 1 to 13). */

int rank_to_score(int rank);
 /* As provided for assignment 9. Takes a rank and
    converts that to a score. Count aces low (1) for now,
    and face cards as 10. */

int adjusted_score(int score, int aces);
 /* The parameters are a base score (which always counts
    aces as 1), and the number of aces seen so far.
    Returns an adjusted score where, as long as it doesn't
    go over 21, counts an ace as 11 points. This is
    similar to the reasoning in the first if statement of
    the assignment 9 solution. */

void deal_and_update(int& score, int& aces);
  /* Using some of the above functions, deal one card, and
     then increment the score variable (a reference
     parameter) according to its rank. If the rank of the
     new card is an ace, then also increment the number of
     aces. */

4 Recommended variables in main

int hit = 1;   // User response to "Would you like to hit?"
int score = 0; // Base score so far (aces low)
int aces = 0;  // Number of aces received so far.

You don't actually have to store the adjusted score in a variable, you can just call that function when you need it, something like:

while(adjusted_score(score, aces) < TARGET_SCORE)
{
   // ...
}