Graphic by Keith Ohlfs |
Problem Set 8, Part B
|
[CS111 Home Page] [Syllabus] [Lecture Notes] [Assignments] [Programs] [Documentation] [Software Installation] [FAQ] [CS Dept.] [CWIS]
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:
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
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:
Since you will treat all aces as having the
value 11, you should always call cardPoints using true as the
second argument.