## CS 101 (Spring 1999) Lab 6: Java Jack

Lab Assigned Design Due
(Mondays 2 PM)
Lab Due
(Fridays 2 PM)
19 Feb 8 Mar 19 Mar

### Overview:

This lab is loosely based on the card game called Black Jack. A player tries to draw cards until the player's hand is worth as close to 21 points as possible, without going over. A numbered card, such as the 3 of Clubs, is worth its stated value, such as 3 points. A face card is wroth 10 points. An Ace is worth 1 or 11 points in Black Jack. For Java Jack, an Ace is worth 11 points if the player's hand has fewer than 3 cards; otherwise, the Ace is worth only one point.

You will implement both a customer and a dealer, that differ only in terms of how they decide to draw more cards. The dealer decides automatically based on the dealer's hand; the customer is under your control.

### Goals:

By the end of this lab, you should
• Understand how to design and implement linked-list (recursive) structures
• Be able to write methods to manipulate linked lists
• Be even more comfortable using recursion and iteration
• Understand a simple encoding scheme for playing cards
• Use == to test if two objects references are to the same object
This lab contains two parts, design and implementation, due on the dates shown above by 2 PM.

The design will be discussed Monday in class. In Lab, your graded design will be returned. No other handouts or code will be issued. So, think carefully about your design!

### Before starting:

• Read over this entire document before you start.
• Study the linked-list examples in class and in the notes.
• Run the sample solution.
Zip includes: The `MousePad` and `DirectionVector` are from Lab 2 . Use of `HandViz` is optional and is worth extra credit if you use it to show the hands visually.

Remember to type the following lines at the top of any files that use the terminal or canvas classes.

```import cs101.terminal.*;
import cs101.canvas.*;
```

### Particulars

`Card.java`

Design and implement the class `Card.java` that represents a playing card. Conceptually, a playing card has a

• suit, which could be Hearts, Diamonds, Clubs, or Spades
• rank, which could be Ace, 2, 3, ..., 9, Jack, Queen, King
However, from an implementation standpoint, we will encode a card using an integer `id`, where `id` is in the inclusive range 0...51. Let us assume that
• The first 13 numbers (0...12) are the Clubs in order Ace, 2, ..., King.
• The next 13 numbers (13...25) are the Diamonds in order Ace, 2, ..., King.
• The next 13 numbers (26...38) are the Hearts in order Ace, 2, ..., King.
• The last 13 numbers (39...51) are the Spades in order Ace, 2, ..., King.
Thus, the following code
```  Card c1 = new Card(15);
```
creates a card with `id` 15, which represents the 3 of Diamonds.

Within the `Card` class, we will need the following methods.

• We need a method to return the suit (Clubs, Diamonds, Hearts, Spades) of a card.
• We need a method to return the rank (Ace, 2, .., King) of a card.
• We need a method to return the point value of the card. The treatment of an Ace makes this a bit tricky.
• We need the method `int getId()` to return the id (0...51) of a card.
• The `toString()` of a `Card` should be defined in terms of the card's suit and rank, though you might include the card's internal `id`.
• We need a static method in `Card` for testing the class. Actually, this is provided for you. The method constructs each of the possible 52 cards and prints out the card using its `toString()`.

`CardList.java`

This object represents a linked-list of cards. Each card in the list is currently shown or not shown. Pattern your design of this class after the examples you have seen in class and in the notes (i.e., the wrap-and-link idea). For this class, you will need the following methods (include their API but not their implementation in your design):

• A method `Card getFirst()` that returns the list's first `Card`.
• A method `CardList getRest()` that returns the rest of the list's cards.
• A method `boolean isShown()` that tells if the list element's card (same card as `getFirst()`) should be shown or not.
• A method `void setShown()` that makes all cards in the list shown.
• A recursive method `cardinality` (pardon the pun) that takes no parameters and returns the number of cards in the list.
• A recursive method `duplicate` that takes no parameters and returns a new `CardList` that begins a list with the same `Card`s but not the same `CardList` references (this will be explained in class).
• An iterative method `reverse` that takes no parameters and returns a new linked-list that contains this list's cards but in the reverse order.
• A method `points` that can be iterative or recursive (your choice). This method takes no parameters and computes the point total for the list.
• Two versions of a card-finding method:
• A method `CardList findCard(Card)` that returns the first `CardList` that contains the specified `Card`. If none can be found, then null is returned.
• A method `CardList findCard(int i)` that returns the ith element in the list, if the list as enough elements to do that; otherwise, null is returned
• Two versions of a card exchanger:
• A method `exchange(Card)` swaps this `CardList`'s `Card` with the specified `Card`, if the specified `Card` is in the list.
• A method `exchange(CardList)` that swaps this `CardList`'s `Card` with the specified `CardList`'s `Card`.
Save yourself time: think about how you can write a method by calling other methods you have already written.

`Deck.java`

This class is provided to you for this lab. However, it won't work properly until your `CardList` and `Card` classes are implemented. This class has the following methods.

`Deck()`
This constructor makes a new deck, with the cards in order.
`void shuffle()`
This method shuffles the (remaining) cards
`Card draw()`
This method removes and returns a card from the deck.

`Customer` and `Dealer`

Each of these classes must provide the following methods

• `boolean wantsCard()` returns true if another card is desired.
• `void takeCard(Card, boolean)` accepts a new card into the player's hand. The `boolean` parameter specifies if the card should be shown.
• `void takeCard(Card)` is the same as above, but the card is definitely shown. See how cards are dealt in `Startup.java` to see how these constructors are used.
• `boolean busted()` returns true if the player's hand contains over 21 points.
• There are accessors in these classes, as given, that need to be filled in.

### What to turn in:

Design (due Monday)
1. Complete a design cover sheet.
2. Define your `Card` class.
• Give its API and describe what the methods do.
• How do you get a card's suit and rank?
• How are these represented?
• How do you get a card's point value?
• Remember to keep instance variables as private as possible.
3. The methods in the API for `CardList` are given. For your design, do the following.
• Define the instance variables -- their names and purpose.
• Draw an example of a 3-card `CardList`.
• Where you can get away with it, explain how you will implement a method in terms of other methods in this class.
• Sketch a loop that iterates through a given `CardList`.
4. In terms of the methods you define for `CardList`, provide code for the `Dealer`'s `wantCard` method, assuming
• If the dealer has 16 or fewer points, then another card must be taken.
• Otherwise, no more cards can be taken.
5. Explain how the `Customer` class (as given) decides whether to want more cards or not.
Implementation (due Friday)
1. Complete a code cover sheet.
2. Provide a transcript from self-test of `CardList` and `Dealer`. Your testing must include coverage of all methods in `CardList`.
3. Provide a transcript of 10 games played between the dealer and the customer.
4. Provide printouts of any files you created or modified for this assignment.
5. Be prepared to demo in lab.

### Suggested implementation approach:

1. Get `Card` working.
2. Get enough of `CardList` working so that `Deck` can make a deck. Test this.
3. Complete more of `CardList` so that `Deck` can shuffle the deck (only one version of `exchange` is needed). Test this.
4. Complete the rest of `CardList`.
5. Test `CardList` thoroughly.
6. Implement the `Dealer`. Do not implement the `Customer` yet, as you will find that the two have much in common. It will be faster to get `Dealer` working fully, and then copy `Dealer` into `Customer` and modify ``` Customer``` as needed.
7. Rig the `Customer` so that no cards are drawn, and then run the program so the `Dealer` plays. Verify that the `Dealer` behaves correctly.
8. Copy and paste all code (except `wantsCard`) from the `Dealer` into the `Customer`.