Assignment #4: Classes for Part I
From our design work in class we decided that the Blackjack program
should have three classes - one to represent a Card, one to represent
a Blackjack hand, and one to represent a Player. For Part I you need
to complete the Card class, the Hand class, and the Player class
and write a driver program to play a game between the dealer and a person.
In developing your classes it would be wise to have a test program
that tests each method as you write a class.
Card class: This class represents a playing card.
A card has two attributes -
a suit and a rank. Hence, the Card class should have two instance
variables:
- suit - this will be an integer (for example,
1 represents Spades, 2 represents Hearts,
3 represents Diamond, and 4 represents Clubs)
- rank - an integer representing the rank (1 represents Ace,
numbers 2 - 10 represent the number, 11 represents Jack, 12 represents
Queen, and 13 represents King)
Provide the
following public methods:
- Card() -- a parameterless constructor that randomly generates the
rank and suit of the card.
- getSuit() - returns a String representation of the suit (for example
if the suit instance variable is 1 the String "Spades" will be returned).
- getPointValue() -- returns the point value of the card: 1 for Ace
(or you can return an 11 - your choice), 10 for
face cards, the number on the card for everything else.
- getRank() -- returns an integer representing the card's rank (1..13).
Note that in this system, Ace is the lowest ranking card.
- String toString() -- returns a string representing the card as "<rank> of
<suit>", e.g., "3 of Diamonds" or "King of Spades". Method toString
should call other
methods (one is the getSuit method, the other you will have to write) to
generate the name and suit strings;
it can then just stick the results together
into the final string and return it.
Hand class: This class represents a Blackjack hand. It should have
at least the following instance data:
- two Card objects - one representing the "top" card in the hand
and the other representing the "bottom" card in the hand (these are
the first two cards dealt - bottom is dealt first, then the top).
- an integer that keeps track of the current count (the point
total for the hand)
- a way to keep track of whether or not the current count is
a soft count (that is if the current count includes an ace that
was added in as 11)
- a way to keep track of whether or not the hand is blackjack
Some of the methods the Hand class needs (add others if needed):
- Clearly it needs a constructor. Your constructor should
"deal" the first two cards (that is instantiate the bottom
and top cards - that generates two random cards). It will have
no parameters. As usual, the constructor should also initialize
all other instance variables as appropriate.
- Getter methods for most of the instance
data.
- A method that returns a boolean indicating whether or
not the hand is blackjack.
- A method that returns whether or not the hand is bust.
- A method that simulates a "hit" - this method needs to generate
a new card (an object of type Card), update the count for the
hand, and return the new Card.
Player class: This class simulates a Blackjack player. For now
the player is one of two types - the dealer or a person. In part II
we'll add the capability of the player being the computer. The player
should have the following instance data:
- A name
- A type (this could be an int where 1 means the player is the dealer
and 2 means the player is a person)
- A hand - this should be an object of type Hand
The methods the class should provide include:
- A constructor that takes one parameter - the name of the player.
If the name is "Dealer" (allow for any combination of cases), then
the player type is a dealer, otherwise the player type is a person.
The constructor should instantiate a new hand in addition to
initializing the name and type instance data.
- Getter methods.
- A method that simulates the player taking its turn. This method
should call private helper methods depending on which type the player
is. For example there could be a method that simulates the dealer's
turn which would use the dealer's rules for playing the game (hitting
as long as the count is under 17). The method representing
the player's turn would be interactive letting the player choose whether
to hit or stick on each iteration.
With these classes the main class that plays the game will be fairly
simple (all the work is in the classes). It needs to instantiate
two Player objects - a dealer and a person (it should read in the
person's name). Then it will play according to
the rules of the game checking to see who won.