CS 111: Fundamentals of Programming 1

Lab 8

Goals

To work with

Blackjack

For our purposes, the game of Blackjack will be played as follows. The game is played with a regular deck of playing cards. Face cards (Jacks, Queens, Kings) count as 10 points, an Ace can count as either 1 or 11, and the other cards count the same as the number on the card. The object of each game is to get as close to 21 points as possible without going over.

We consider a single player playing against the dealer. The dealer first deals two cards to the other player and two cards to herself. The player's two cards are showing, and one of the dealer's cards is showing. At this point if either has 21 (Blackjack), the hand is over. The one with 21 wins the hand - if both have 21, the hand is a tie.

If neither player has Blackjack after the deal, the player may choose to Hit as many times as he wishes. When he Hits, the dealer deals him another card. If the player's total exceeds 21, he busts and loses the hand. If the player gets 21, he wins the hand.

At any time, if the player has not exceeded 21, he may choose to Stand. In this case, the dealer deals cards to herself. If she has less than 17 points, she must Hit. If she has 17 or more points she must Stand. If she busts, she loses. Otherwise, the one with the most points wins.

In this project, you will make use of the following classes that are provided:

Card: When a card is created, it is given a point value in the range of 1-10. The 10's are more likely since they would represent 10's, Jacks, Queens and Kings. However, the current implementation does not keep track of previous cards created; so it is possible to get more than four fives, for example. New cards will be created by the dealer. You will need to display cards, but you will not need to manipulate point values of individual cards.

Properties: There are no public properties available to you.

Methods: The only method that you should use from the Card class is a toString method. To display a card in a text area, say, you could do something like

playerArea.append(myCard + "\n"); \\ or

playerArea.append (myCard.toString());

Player: An instance of this class represents a Blackjack player. It is created with a point total of 0. Assuming myPlayer has been declared with type Player,

myPlayer = new Player();

establishes myPlayer with a point total of 0.

Properties: No public properties

Methods:

There is a mutator, called setTotal, to give a card to the player. This method has header:

public void setTotal(Card aCard)
// Pre: aCard is an instance of Card class (has point value)
// Post: the point value of aCard has been applied correctly to total for player

For example, to give myCard to myPlayer

myPlayer.setTotal(myCard);

There is a public accessor method, getTotal.

public int getTotal()
// Post: returns the total points the player currently has

currentTotal = myPlayer.getTotal();

Dealer: The Dealer class is a subclass of Player; so a dealer is also a player.

Properties: No public properties

Methods:

In addition to the Player methods, there is a public accessor method to get a card from the dealer.

public Card getCard()
// Post: returns a newly created Card.

So, for example, to deal a card to the player might look something like this

myCard = myDealer.getCard();
myPlayer.setTotal(myCard);

Important Note: You will be writing the interface class to play the game of Blackjack. You are to make use of these classes provided. You should not attempt to determine point values of cards. You only need to know total points of the player and the dealer, and these you get from the player and dealer - don't keep running totals. You have no need for random number generators in this project.

Important Note: Remember the honor code rules - review syllabus if needed.

Important Note: Develop this work in small pieces and test as you go. Try to think of methods that you could write that would decompose your programming into meaningful components.

Step 1 - Copy the Lab8 folder from the course folder to your directory.

This contains a Blackjack project which you are to complete and a Sample project with a working version.

Step 2. Develop a BlackjackInterface class

This is your GUI class with a main method to execute the game. Among the features are

Note: With the sample program, notice how the appropriate buttons are enabled at any point during execution. This actually simplifies the programming, because I don't have to concern myself with issues such as: What if user takes Stand when no cards have been dealt, etc. To do this, I am making use of a JButton method called setEnabled. For example to disable the Hit button,

hitButton.setEnabled(false);

Step 3. Turn in your work.

When you have completed all work on this lab, you should turn in a print out of your final program. Be sure to adhere closely to specifications given. Also adhere to points of programming style. Points will be deducted for poor choice of variable names, poor indentation and placement of braces, lack of pre and post conditions, etc.

You should also copy your Lab8 folder into your turnin folder.

Due by class time next Monday.