CSC 161 Program #1

Blackjack!

Program #1 is worth 75 points
Assigned: Mon Jun 16, 2003 Due: Fri Jun 27, 2003

 

Concepts

In this program, we will review concepts covered in CSC 160, including:

  • Ch 2: Introduction
  • Ch 3: Expressions
  • Ch 4: Conditional statements: if, else, switch
  • Ch 5: Looping: while, for
  • Ch 6: Functions
  • Ch 7: Classes and Objects
  • Ch 8: Arrays
  • Also, multiple-file programs, random numbers, and more!

Description

Implement a program that plays Blackjack or "21". This card game has a "dealer" and one or more "players". The goal of the game is to get as close to a total of 21 with your cards, without going over that amount.

En route to 21, each card has a value:

  • Cards 2-10 are worth their face value
  • Jacks, Queens, and Kings are worth 10
  • Aces can be worth 1 or 11

Step 1. Initial deal

The game starts with four cards being dealt: two to the player, two to the dealer. The player cards are dealt "face up", in other words, they are shown. Only one of the dealer's two cards is shown; the other is hidden from the player.

Step 2. Player's turn

The player, based on his two cards and the dealer's showing card, can decide to add a card to his hand, called a "hit". He/she can stop at any time, by deciding to "stay". If the player's total exceeds 21, then he/she has "busted" and the turn ends.

There is one special case. If the player's first two cards total 21, he/she has a "Blackjack" and is an automatic winner. This is only possible if one of the cards is an Ace and the other a card having a value of 10. With a Blackjack, the player is not offered the opportunity to "hit" because he/she has already won.

Step 3. Dealer's turn

If the player does not have a Blackjack (an automatic winner) or has not busted (an automatic loser), then it is the dealer's turn. The dealer shows his hidden card, and then takes additional cards until they total 17 or higher. Three cases are possible at this point:

  • If the dealer "busts", then the player wins.
  • If the value of the player's hand is greater than the dealer's, then the player wins.
  • If the value of the dealer's hand is greater than the player's, then the dealer wins.

Example Session

Here's an example session, player responses in bold:

Blackjack!
Dealing... 
 
Dealer shows: 3 of Clubs
You have: 8 of Diamonds, 5 of Clubs
Would you like to [H]it or [S]tay? H
 
Player hits... card is 7 of Spades
You have: 8 of Diamonds, 5 of Clubs, 7 of Spades
Would you like to [H]it or [S]tay? S
Your total is: 20
 
Dealer has: 3 of Clubs, Queen of Clubs
Press enter to continue: <enter>
 
Dealer hits... card is 5 of Diamonds
Dealer has: 3 of Clubs, Queen of Clubs, 5 of Diamonds
Press enter to continue: <enter>
 
Dealer stays.
Dealer total is: 18
You win!
 
Another game [Y/N]? 
 

Implementation

There are some sticky issues here:

  1. How do you generate "random" cards? A full deck of cards has 52 cards. After a specific card is dealt (or randomly selected), it should no longer be available in the deck. Also, the number of available cards is decreased by 1.
  2. An array sounds like a nice way to store the 52 cards in a deck. Is there a "nice" way of initializing the array of cards, so that you can use a loop rather than assigning each card individually.
  3. It is generally bad form to put cin/cout statements (the input/output or I/O) directly in "general" classes. Can you create your classes such that the I/O is done only in the controlling main() function.
  4. When determining the value of a hand of cards, the value of an Ace can be 1 or 11. How will you handle that "gracefully"?

Grading

Your program will be graded on three areas: design, quality, and function

Design (20%)

For this object-oriented program, your design will be be represented by the class header files in your program. You should be able to compile your header and "stubs" for your member functions for this. We will discuss in class how to do this.

Accompanying your design, you should complete a "halfway" checkpoint. On your k: drive, create a test program that 1) prints out all the cards in a deck in some order (by suit?) and 2) deals random cards off a deck.

Quality (20%)

Your code should be well-commented, properly indented and structured, and contain informative and consistent names. The purpose of each functions and its parameters should be well-commented at the top of the function.

Function (60%)

Your program must implement the program features per the description above.

Place any comments you have in your README file.


Notes/Hints

Be as tangible as you can in defining your classes. Try it.

To create a different random seed (by calling srand() once at the beginning of your program) each time you run your program, use this:

#include <ctime>

...

srand( (unsigned)time( NULL ) );

BTW, if you're unfamiliar with playing cards, there are 52 cards in a deck. There are 4 suits: Clubs, Diamonds, Hearts, and Spades. There are 13 cards of each suit: Ace, 2-9, Jack, Queen, and King.

Don't forget a README file in your folder detailing your journey and your testing methodology.

Additional fun things you can add to this program:

  • Ask the player for his/her name at the beginning of the session
  • Track how many games the player has won or lost
  • Allow player to bet... for entertainment purposes only
  • Implement more sophisticated Blackjack options, like "doubling down"