We will develop a C++ program that implements a simple blackjack card game. Students may be asked to complete aspects of the design or implementation of such a game and the instructor may provide refinements to the design/implementation of the as we go through the semester and introduce more object-oriented programming concepts.

### Basic Blackjack Game Overview

A blackjack game has a player and a dealer. A player is provided with a certain sum of money with which to play. A player can place a bet between 0 and the amount of money the player has. A player is dealt cards (called a hand), each of which has a point value. The objective of the game is to get as close to 21 points as possible without going over. A player that goes over is out of the game.

### Basic Blackjack Rules

The dealer deals cards to itself and a player. The dealer must play by slightly different rules than the player. The dealer does not place bets.

A player is dealt two cards face up. If the point total is exactly 21 the player immediately wins. If the total is not 21, the dealer is dealt two cards, one face up and one face down. A player then determines whether to ask the dealer for another card (called a "hit") or to "stay" with his/her current cards. A player may ask for several "hits". When the player decides to "stay" the dealer begins to play. If the dealer has 21 it immediately wins the game. Otherwise, the dealer must take a "hit" until the total points in its hand is 17 or over, at which point the dealer must "stay". If the dealer goes over 21 while taking "hits", the game is over and the player wins. If the dealer's points total 21, the dealer wins immediately. When the dealer and player have finished playing their hands the one with the highest point total is the winner. Play is repeated until a player enters a \$0 bet.

### Scoring Points

Points are calculated as follows:

• Ace 1 or 11 (whichever is better)
• 2-10 Face value of the card
• Jack, Queen, King 10

An ace may be valued as 1 point or 11 points by the owner of the hand (i.e., the player or the dealer). The owner of the hand may change the value of an ace at any time during their turn at play.

### Developing A Solution

• Identify candidate classes
• Should be relevant to problem domain
• Should have clear cut responsibilities/roles
• Model important relationships between classes to understand how they need to connect
• Iterate through the above steps & refine

• Card
• Deck
• Hand
• Player
• Dealer
• Game

### Common Associations

• A is a physical part of B
• Wing / Airplane
• A is a logical part of B
• SalesLineItem / Sale
• FlightLeg / FlightRoute
• A is physically contained in/on B
• Register / Store
• A is a description for B
• ProductDescription / Product
• A is known/reported/captured in B
• Sale / Register
• A is an organizational subunit of B
• Department / Store
• A is a member of B
• Cashier / Store
• A uses/manages B
• Pilot / Airplane
• Cashier / Register
• A communicates with B
• Customer / Cashier
• A is a transaction related to transaction B
• Payment / Sale
• A is an event related to B
• Sale / Customer
• Sale / Store
• Departure / Flight

See image.

See image.

### Card Class Issues

A card will be valued from 1-10.

Ace will be treated as a 1 by default, since it shouldn’t be a responsibility of the Card class to know anything about the state of the game---determining whether to value an ace as ‘1’ or ‘11’ will be done by some other class.

The show() method will simply return card information as a string, e.g., “3-hearts”

The calcValue() method could be made private, since it will only be called by constructors

### Deck Class Issues

A deck will be modeled as a collection of Card objects. Could use an array, vector, or some other mechanism to hold cards.

The next attribute is the index of the next card to be dealt.

The nextCard() method returns the card dealt. This method should also be responsible for determining what is done when the deck becomes empty --- one possibility is to create a new deck when a deck becomes empty…giving the appearance of an infinite deck

The empty() method returns true if the deck is empty.

The shuffle() method shuffles the deck. There are many ways this could be done. This will require using a random number generator

### Hand Class Issues

A hand will be modeled as a collection of Card objects.

The addCard() method adds a card to the hand, and calls the calcValue() method to determine the card’s point value.

The calcValue() method returns the point value of the card that was most recently added to the hand. It needs to have a decision rule built in to determine whether an ace should be valued as 1 point or 11 points. Note that this method could be made private assuming no other object needs to call it.