CS Logo

Project 2

 

Write a blackjack simulator using control structures, your own functions, and rand().

 

The basic premise of the game is that you want to have a hand value that is closer to 21 than that of the dealer, without going over 21. The rules of play for the dealer are strictly dictated, leaving no decisions up to the dealer. 

 

Rules of Blackjack

 

Dealing

 

Two cards are dealt to the dealer. One is made visible to the player.

The player is dealt two cards both of which are visible.

 

Both the player and dealer may "hit" or "stay." Hit means that a new card is dealt and added to the hand. Stay means that no more cards are added to the hand and that the player or dealer's turn is over.

 

Display cards to the user numerically except with face cards where you should display Q, J, K, and A for the ace. Use rand() to draw cards with a probability of 1/13 for each card (since suite doesn't matter).

Values of the cards

In blackjack, the cards are valued as follows:
   An Ace can count as either 1 or 11, as demonstrated below.
   The cards from 2 through 9 are valued as indicated.
   The 10, Jack, Queen, and King are all valued at 10.

The suits of the cards do not have any meaning in the game.

The value of a hand is simply the sum of the values of each card in the hand.  For example, a hand containing (5,7,9) has the value of 21.  The Ace can be counted as either 1 or 11.  You need not specify which value the Ace has.  It's assumed to always have the value that makes the best hand.  An example will illustrate:  Suppose that you have the beginning hand (Ace, 6).  This hand can be either 7 or 17.  If you stop there, it will be 17.  Let's assume that you draw another card to the hand and now have (Ace, 6, 3).  Your total hand is now 20, counting the Ace as 11.   Let's backtrack and assume that you had instead drawn a third card which was an 8.  The hand is now (Ace, 6, 8) which totals 15.  Notice that now the Ace must be counted as only 1 to avoid going over 21.

A hand that contains an Ace is called a "soft" total if the Ace can be counted as either 1 or 11 without the total going over 21.  For example (Ace, 6) is a soft 17.  The description stems from the fact that the player can always draw another card to a soft total with no danger of  "busting" by going over 21.  The hand (Ace,6,10) on the other hand is a "hard" 17, since now the Ace must be counted as only 1, again because counting it as 11 would make the hand go over 21.

How the dealer plays his hand

The dealer must play his hand in a specific way, with no choices allowed. In our game we require the dealer to obey the following rule:

"Dealer stands on all 17s":  The dealer must continue to take cards ("hit") until his total is 17 or greater.  An Ace in the dealer's hand is always counted as 11 if possible without the dealer going over 21.  For example, (Ace,8) would be 19 and the dealer would stop drawing cards ("stand").  Also, (Ace,6) is 17 and again the dealer will stand.  (Ace,5) is only 16, so the dealer would hit.  He will continue to draw cards until the hand's value is 17 or more.  For example, (Ace,5,7) is only 13 so he hits again.  (Ace,5,7,5) makes 18 so he would stop ("stand") at that point.

If the value of the dealers hand exceeds 21 then the dealer "busts" and the player is returned twice his bet. If the value of the dealer's hand ever exceeds the value of the player's hand without being more than 21 the dealer wins and the players bet is lost.

If the dealers hand is greater than 17 and equal to the player’s hand then it is called a "push." In a push the current bet remains in the game and the player has the opportunity to add to it, then new hands are dealt and a new round takes place. The game may be pushed indefinitely so long as the player and dealer keep getting the same score.

 How the player plays his hand

In this game we are going to simplify things a lot and only allow the player to draw another card ("hit"), or stop at the current total ("stand"). You aren't required to implement "insurance," "doubling down," "splitting pairs" or "even money." 

If the player hits and the sum of his cards is more than 21 he loses and the bet is lost. The player may continue to hit so long as the total remains under 21. If the player hits and the new hand is worth 21 the player wins immediately and twice his bet is returned to him.

 

If the player stands then the dealer displays the second card he was dealt. If the value of the dealers hand is less than 17 and less than the value of the players hand then the dealer continues to hit (draw more cards).

 

 

Example run: (user input in red)

 

Welcome to CS151 Blackjack!

 

You have $100.

 

Enter your bet: 10

 

Dealer drew 8.

Player drew 8 and 5.

Value of Dealer's Hand: (8)

Value of Player's Hand: (13)

 

Hit or Stay? h

 

Player drew 5.

Value of Dealer's Hand: (8)

Value of Player's Hand: (18)

 

Hit or Stay? s

 

Dealer drew 6.

Value of Dealer's Hand: (14)

Value of Player's Hand: (18)

 

Dealer hits.

 

Dealer drew Q.

Value of Dealer's Hand: (24)

Value of Player's Hand: (18)

 

Dealer busts!

 

You win $20. You have $110.

 

Play again? (y/n) y

 

Enter your bet: 20

 

Dealer draws K.

Player draws 5 and 2

Value of Dealer's Hand: (10)

Value of Player's Hand: (7)

 

Hit or Stay? h

 

Player draws J.

Value of Dealer's Hand: (10)

Value of Player's Hand: (17)

 

Hit or Stay? h

 

Player draws A.

Value of Dealer's Hand: (10)

Value of Player's Hand: (18)

 

Hit or Stay? s

 

Dealer draws Q.

Value of Dealer's Hand: (20)

Value of Player's Hand: (18)

 

Dealer wins.

 

You lose $20. You have $90.

 

Play again (y/n) y

 

Enter your bet: 5

 

Dealer draws 7.

Player draws K and A

Value of Dealer's Hand: (7)

Value of Player's Hand: (21)

 

BLACKJACK!

 

You win $10. You have $100.

 

Play again (y/n) n

 

Come back soon!

 

 

Basic rules taken from blackjackinfo.com