Graphic by Keith Ohlfs
CS111, Wellesley College, Spring 2000

Problem Set 8, Part B

Due: Friday, April 28 at 4:00 p.m.

[CS111 Home Page] [Syllabus] [Lecture Notes] [Assignments] [Programs] [Documentation] [Software Installation] [FAQ] [CS Dept.] [CWIS]

Reading

Link to Extra Credit Problems


Task 3: BlackJack [4 pts]

Blackjack (also known as twenty-one) is a popular casino card game. There are many versions of the game; here we consider one of the simplest variations.

The goal of the game is to acquire a hand of cards whose point value is as large as possible without exceeding 21 points. The point value of a hand is the sum of the point values of the individual cards, where:

For example,

Since the goal of the game is to get as close to 21 points without going over, the canonical value of a hand with multiple point values is the largest value less than or equal to 21, if such a value exists, or the smallest value greater than 21, if none of the possible values is less than 21. Some examples of canonical values:

Note that adding a card to a hand with aces may decrease its canonical value by changing the point value of an ace from 11 to 1.

For the remainder of this problem, we will simplify matters by assuming that aces always have 11 as their point value. For example, we will treat [AC AD AH] as having a point value of 33.

In our simple version of the game, there are two participants: the player and the house (representing the casino). Each is dealt two cards from a fresh deck. Both of the player's cards are dealt face up, but one of the house's cards is dealt face down and so is a secret unknown to the player. Play proceeds in two stages:

  1. The player can elect to hit (be dealt another card which is added to her hand) as many times as she wants until she decides to hold (keep her hand) or she busts (gets a hand with point value greater than 21). If the player busts, the game is over, and the house wins. If the player holds, then play continues with the next stage.
  2. The house turns over the face down card and proceeds to hit until the value of the house's hand is 17 or greater. If the house busts, the player wins the game. If the final value of the house's hand is between 17 and 21, inclusive, then the participant with the higher point value wins the game. If the point values of the house and player are equal, the house wins the game.

In this problem, you are provided with a simple graphical user interface (GUI) to a Blackjack game and are asked to implement the rules of the game. For starters, you should try the working version of BlackJack which can be found in the Test folder of the ps8_programs folder. Use AppletViewer to run the Blackjack.html applet, and play several games.

The interface is rather crude. For example, cards are represented as text rather than as icons, and the layout of the game leaves much to be desired. But the interface does the job, and is not the focus of this assignment. We will study how to implement user interfaces in Java in the next few lectures. For now you should treat the interface as a black box. Note that in addition to showing the cards in a hand, the interface also shows the number of points.

In order to play a game, we need to have a working Hand of cards. An implementation of VectorHand has been provided. The class file is in the project folder, but the source file is not (since that would give away the answer to Task 2). Refer to the Hand contract for how to use VectorHand. We also need to have a Deck of cards. A Deck is a Hand (i.e. an implementation of Deck will extend an implementation of Hand) that starts off with all 52 cards. With a Deck, one can shuffle it, and deal cards. The contract for Deck has been provided below. An implementation of Deck has been provided (again, as a class file, but not the source file).


Deck Contract

public Deck ()
Returns a deck of 52 cards in sorted order from lo to high.

public void shuffle();
Randomize the order of cards in a deck.

public Card deal();
Remove and return the last card of the deck.


The BlackJack folder contains a number of compiled classes (.class files) implementing the Card, Hand, and Deck Contracts described in this problem set. It also contains the source code (.java files) for the user interface (BlackjackGUI.java) and the game rules (BlackjackGame.java). The only file you will be working with is BlackjackGame.java. You may also want to skim BlackjackGUI.java to see how it calls the methods in BlackjackGame.java. But note that it is not necessary to understand the details of how the user interface is built.

BlackjackGame.java contains a skeleton for the implementation of the BlackjackGame class. Instances of this class represent the state of a simple game of Blackjack, as described above. Abstractly, the game has the following state variables:

In this problem, your goal is to implement the following contract for the BlackjackGame class. You will do this by declaring appropriate instance variables and by fleshing out the code for the skeleton methods provided in BlackjackGame.java


Contract for the BlackjackGame class:

Public Constructor Method:

public BlackjackGame ()
Creates a new game in which both the player and the house have been dealt two cards from a fresh deck. The same deck should be used throughout the remainder of the game.

Public Instance Methods:

public Hand getPlayer()
Returns a copy of the current hand of the player in the game. Side effects to the returned hand should not affect the players hand in the game.

public Hand getHouse()
Returns a copy of the current hand of the house in the game. Side effects to the returned hand should not affect the players hand in the game.

public void playerHits()
Adds a new card to the player's hand.

public void housePlays()
Deals cards to the house until the house hand is worth 17 or more points.

Public Class Methods:

public static boolean isBusted (Hand h)
Returns true if the hand h is worth more than 21 points, and false otherwise.

public static int handPoints (Hand h)
Returns the number of points in the hand h. For simplicity, count all aces as 11. (On the next assignment, we will see how to count aces as both 1 and 11.)

Notes:


Link to Extra Credit Problems