Personal tools
You are here: Home Classes Fall 2004 - Spring 2005 CS 150 labs lab7 BlackJack
Document Actions

BlackJack

by admin last modified 2005-05-11 13:32

Lab 7 -- Blackjack

In this lab, you will write a program which plays the card game of Blackjack.  The program will present a GUI interface which will allow an interactive user to play a game of Blackjack against the computer, which will act as the dealer.

In case you don't know the rules of BlackJack, it works like this. You start with a hand of 2 cards. The value of a hand is the sum of the values of its cards: 10 for face cards (Jacks, Queens and Kings), 1 or 11 for Aces, and the face value for the rest. You can ask for as many additional cards as you like, one at a time, and stop whenever you like. The goal is to bring the value of your hand as close to 21 as you can without exceeding 21. After you have taken as many cards as you like the dealer repeats this process with her own hand. The winner is the player who comes closest 21 without going over.

To implement the game, you'll write a group of classes to model important components of the game:

  • playing card
  • card deck
  • a player's hand
We have provided a complete GUI interface and also a class that implements the rules of BlackJack (see the jar file).  You need to write code that interfaces these classes, so make sure that you follow the specifications carefully.
Click here for the jar file.

Rules of the game

The objective of the game is to accumulate the hand with the highest value, not exceeeding 21.  A hand is evaluated as follows:
  • The value of a hand is the sum of the values of the cards in the hand.
  • The values of cards are:
    • Two through Ten are counted at their face values.
    • Jack, Queen and King have a value of 10.
    • An Ace has a value of either 1 or 11, at the player's discretion.  
A hand with an ace and a 10-point card is a Blackjack, the best possible hand.

Play of the game

The dealer deals two cards to each player and to himself.  (In our case, we will just have one player, the interactive user.)  The dealer shows one of his cards face up, the other is kept face down.

Each player then has the opportunity to request additional cards from the dealer.  The player has the choice of requesting a Hit; that is, an additional card, or to Stand; that is, to finish his turn and keeping his hand as it is.

Eventually, the player either busts or stands pat.  A bust is an automatic loss.  If the player stands without busting, the dealer then plays his hand.  The dealer has the same choices (hit or stand) that the other players do.  If the dealer busts, the player wins. Otherwise, the hand with the highest value wins.  If the dealer's hand and the player's hand have the same value, the game is a "push" (a tie game), and the player's bet is returned.  (Note:  A hand with Blackjack beats a hand with 21, but not Blackjack.)

Part 0: The jar file

If you download the jar file and expand it, you will find the following files:

BlackJackInterface.java      This is a GUI interface for the game; you do not need to change anything in this file.
BlackJack.java                   This plays a game of BlackJack using the rules above. You don't need to change anything in this file or to add to it, but since it refers to the classes you need to build you may want to see how it is using them. The main() method for the BlackJack program is in this class.

The BlackJack.java file makes references to three other classes: Card (a single playing card), Deck (a pack of 52 Cards), and Hannd (a list of Cards). Your job in this lab is to implement these three classes, according to the descriptions given below. Do not change the names of the methods, or your BlackJack class will not be able to find the methods it needs. When these three classes are fully implemented you should have a working BlackJack game. Meanwhile, I have made suggestions for including temporary main() methods in each of the classes as you build them to help debug them. These temporary main() methods should be deleted (or commented out) after you are confident that each class is functioning properly.

Part 1: The Card class

The Card class is used to model a playing card.  A Card is defined by two values:  a suit (clubs, diamonds, hearts, or spades) and a rank (ace, two, three, etc.).  These can be stored as integer instance variables, using the following codes:

suit
code
clubs
0
diamonds
1
hearts
2
spades
3

rank code
ace
1
two
2
three
3
four
4
five
5
six
6
seven
7
eight
8
nine
9
ten
10
jack
11
queen
12
king
13



Create a file Card.java to hold class Card. Give it instance variables for its suit and rank data, and write the following public methods for class Card:

1.    Card(int suit, int rank);  This is a constructor to initialize the suit and rank of a card.

2.    int value();  This method returns the value of the Card in the game of BlackJack.  (Ace=11, Two=2, ... , Ten=10, Jack=10, Queen=10, King=10). Don't worry for now that an Ace might be valued at 1 or 11; just use value 11 for Aces.

3.    String toString();  This method should return a String representing the Card, in the form

Ace of Hearts
Three of Diamonds
King of Clubs

etc.

Suggestion:  Set up static final arrays of the suit names and rank names.  For example,

private static final String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };

A suit name can then be obtained from the suits array by using the suit code as an index.  Ranks can be handled in the same way.

4.    main(String[] args);  For testing only, write a main method which creates two or three cards and writes them to System.out using the toString method. Once you are sure this class is implemented correctly, remove this main() method.
 

Part 2: The Deck class

The Deck class represents a deck of 52 playing cards.  It has two instance variables:
  • an array of 52  cards, and
  • an integer index into the array, which refers to the next card to be dealt from the deck.  For example, if the index has a value of 12, it means that cards 0-11 have already been dealt; the next card to be dealt is at position 12 in the array.

Create a file Deck.java to hold class Deck; give it instance variables and the following public methods:

1.    Deck();  A constructor that initializes a deck of playing cards in order by suit and then rank.  The constructor makes52 new cards and stores them in its array, starting with the ace of clubs, two of clubs, three of clubs, ... , king of clubs, ace of diamonds, two of diamonds, etc. You can use a double for-loop for this: one loop to go through the suits and another for the ranks. You will need to increment a counter for the array holding the cards each time you make a new one.

2.    static Deck shuffledDeck();  Returns a new, randomized deck of cards.  A random deck can be created by starting with a standard deck and then shuffling it.  Here's one way to shuffle the deck:  Pick any two cards at random, and then swap them in the deck.  By itself, this won't shuffle the cards very much.  So, repeat it, say 1000 times.  The deck should be well shuffled by then. Here is a function that will help with this:

private static int randomIndex() {
   return (int) (DECK_SIZE*Math.random());
}

where DECK_SIZE is a static final int with value 52. This returns a random index into the card array, with value between 0 and 51.

3.    int sizeRemaining();  Returns a count of the number of cards which have not yet been dealt.

4.    Card dealCard();  Return the next card from the top of the deck.  This method can be used by any object that wants to deal from the deck.  When a card is dealt, the index pointing to the top card should be advanced by one card.

5.    static void main(String[] args);  For testing purposes, write a main method which creates a shuffled deck and displays all 52 of its cards, one per line. Again, after this tests out correctly delete this main() method.

Part 3: The Hand class

In the game of Blackjack, a player is dealt 2 cards initially.  After that, the player may request being "hit" with additional cards.  The objective is to obtain a hand whose value is as close as possible to 21, without going over 21.  (A hand with a value greater than 21 is a "bust", and automatically loses the game.)

To model a Blackjack hand, you can use an array to store the cards held in the hand.  You should also keep track of the number of cards in the hand.  You may assume that a hand never has more than 20 cards.  Then write the following methods:

1.    Hand(Card card1, Card card2);  This constructor initializes a Hand with two cards dealt from the dealer.

2.    Card getCard(int i);  Return the ith Card from the Hand.

3.    void add(Card card);  Add a Card to a Hand.

4.    int value();  Compute the value of the Hand.  The value of the Hand is the sum of the values of the cards in the Hand.  Now we need to take into account the rule that Aces can be valued as 1 or 11. Suppose v is the sum of the values of the cards in the hand (counted Aces as 11) and n is the number of Aces. Let j be the smallest number <= n so that v-10*j is <= 21 (i.e., j is the number of Aces we will count as 1 rather than11). Then the value of the hand is v-10j;

5.    boolean hasBlackJack();  A Hand has BlackJack if it contains an ace and a 10-point card; that is, it is a two-card hand with a value of 21.

6.    boolean isBusted();  A Hand is busted if its value exceeds 21.

7.    String toString();  Return a string describing the contents of the Hand.  The String should consist of the String representation of each Card in the Hand, separated by newline characters.  If the Hand has BlackJack,  append the word "BlackJack" to the String.  If the Hand is a bust, append the word "Bust" to the String.  For example,

"Ten of Clubs\nFive of Clubs\nNine of Hearts\n\nBust", which would appear as

Ten of Clubs
Five of Clubs
Nine of Hearts

Bust

8.    public static void main(String[] args);  Write a main method (for testing purposes) which creates a random deck, deals a hand from it, and displays it. Again, you should delete this method after testing your implementation.

This should complete your implementation.


Part 4: To run the program

The main() method for this program is in class BlackJack. You can compile the full program with the command

javac *.java

After it compiles smoothly you can run it with

java BlackJack.

 

Appendix: The GUI Interface

The jar file for this lab implements a graphical interface for the BlackJack game. It consists of two files:

BlackJackInterface implements a window-based interface, which looks like this:
Lab 6 GUI

The frame contains two text areas (JTextArea) and three buttons (JButton).  The left text area is used to display the player's hand and its value.  The right text area is for the Dealer's hand; during play, only one of the dealer's two cards is visible to the player.

During play, the Play button is disabled.  The Hit and Stand buttons are enabled, allowing the user to choose an avenue of play.

At the end of a game, the GUI looks like this:

Lab 6 GUI

The left text area displays the player's hand; the right text area displays the Dealer's hand.  The text areas also display the value of each hand.  The outcome of the game (Win, Lose, or Push) is displayed in the player's text area.

The Play button is enabled when the program is started and when a game is completed, and allows the user to start a new game.  The Hit and Stand buttons are disabled while the Play button is active, and Play is disabled during a game, when Hit and Stand are enabled..



File BlackJack.java makes use of the interface to play the game. Class BlackJack maintains the deck of cards and hands for the player and dealer. It contains callback procedures for the buttons. The callback for the Play button just initializes the two hands and displays them. The callback for the Hit button adds a card to the player's hand, checks to see if the player has exceeded 21, and updates the display. The callback for the Stand button repeatedly adds cards to the dealer's hand until it exceeds the player's hand or busts. At this point it determines the winner of the game and updates the display. Finally, it reactivates the Play button to see if the user wants to play another game.

Some enhancements

Here are some enhancements to the game that you can try to implement:

1.    Allow for more players.  You can set up an array of players, rather than just having a single player.  You'll need to modify the user interface, either by adding additional text areas, or by giving each player its own frame.

2.    Betting.  Allow players to bet on the game.  When the game is started, give the player a stake, say $100.  Then allow the player to bet a portion of that stake on the next game.  If the player loses the game, he loses his bet.  If the player wins the game, he gets his bet back, along with winnings equal to the bet amount.  If the game is a push, the player gets the bet back.


 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: