# Classwork 6: Blackjack Strategy

## Objectives

To practice using if statements.

## The Assignment

For this assignment, you will write a program that tells the user to 'hit' or 'stand' in a game of Blackjack (also known as Twenty-one).

Blackjack is a casino card game where the objective is to have the cards you are dealt total up as close to 21 as possible. If you go over 21 (a bust), you lose. The cards are from a standard deck (most casinos use several decks at once). Cards 2-10 have the values shown. Face cards (Jack, Queen and King) have value 10. An Ace is either 1 or 11, whichever is to your advantage.

Each player is initially dealt two cards face up. The dealer is given 1 card face up and 1 card face down. Then, each player gets one turn to ask for as many extra cards as he wants, one at a time. To receive another card, the player "hits". When he doesn't want any more cards, he "stands".

If you don't know Blackjack, you can find the rules for Blackjack on Wikipedia, but it is simplest to wait for the demo in class.

The strategy that you will implement is a rather simple one. You will probably lose money slowly in a casino if you follow this strategy. (If you don't follow a strategy like this one, you will lose money quickly.)

• If your cards total 17 or higher, always stand regardless of what the dealer is showing in his face-up card.
• If your cards total 11 or lower, always hit.
• If your cards add up to 13 to 16 (inclusive), hit if the dealer is showing 7 or higher, otherwise stand.
• If your cards add up to 12, hit unless the dealer is showing 4 to 6 (inclusive). In that case, stand.

The sample runs of your program should look like this:

```
PT[122]% gcc -Wall blackjack.c

PT[123]% ./a.out
Tell me which card the dealer is showing.
Enter 2-9, 10 (Jack, Queen, King) or 11 (Ace): 7
What is the combined total of your hand? 12
You should: Hit

PT[124]% ./a.out
Tell me which card the dealer is showing.
Enter 2-9, 10 (Jack, Queen, King) or 11 (Ace): 5
What is the combined total of your hand? 12
You should: Stand

PT[125]% ./a.out
Tell me which card the dealer is showing.
Enter 2-9, 10 (Jack, Queen, King) or 11 (Ace): 11
What is the combined total of your hand? 19
You should: Stand

PT[126]%

```

## Notes

• You will obviously use lots of if statements. For ease of debugging, make sure that you indent your program properly. Always, use curly braces, { and }, even when the body of the if or else part only has a single statement.

• Use && for logical AND and || for logical OR.

• You may have to use if statements inside another if statement.

• Blackjack is one of very few games where the player has a statistical chance of actually beating the casino using a strategy called "card counting" where the player tries to remember the distribution of cards that have been played. (For example, the player might try to remember the number of Aces that have been played.) Casinos routinely kick out card counters. Here's a recent Newsweek article on Edward Thorp, the math professor who developed the first card-counting strategy.

## What to submit

Use the script command to record yourself compiling and running your program several times with different hit/stand results. (Do not record yourself editing your program!) Exit from script. Submit your program and the typescript file:

```submit cs104_chang cw06 blackjack.c typescript
```