Back to 111 Main Page

Mobius strip

Assignment 13

Task

Get more practice with ArrayLists and breaking problems down into classes by modeling a deck of playing cards.

Steps

Your finished program should create a full deck of standard playing cards. That is, 52 cards, Ace through King, in four suits: ♦ ♥ ♠ ♣. A new deck should be in shuffled order. No card should be duplicated within a deck. Use an ArrayList to hold your cards.

Your program should then allow the user to draw as many cards as she'd like from this deck. Each card drawn should be displayed one per line.

After each draw, your program should state how many cards remain in the deck.

If the next draw would exceed the number of cards left the current deck, a new shuffled deck should be created so that drawing may continue normally. The program should indicate when a new deck has been "opened"/created.

Sample Output

This is your digital deck of playing cards!
A new deck has already been opened and shuffled for you.

[52 cards left in deck] How many cards to draw (0 to quit)? 1

10 of Spades

[51 cards left in deck] How many cards to draw (0 to quit)? 7

Ace of Spades
Queen of Diamonds
8 of Clubs
Queen of Clubs
2 of Clubs
3 of Diamonds
3 of Clubs

[44 cards left in deck] How many cards to draw (0 to quit)? 2

10 of Diamonds
King of Spades

[42 cards left in deck] How many cards to draw (0 to quit)? 45

King of Clubs
6 of Hearts
3 of Hearts
Ace of Hearts
5 of Diamonds
6 of Diamonds
King of Diamonds
4 of Clubs
10 of Clubs
6 of Spades
9 of Spades
2 of Spades
2 of Hearts
5 of Spades
8 of Spades
6 of Clubs
4 of Spades
7 of Clubs
King of Hearts
9 of Diamonds
7 of Spades
3 of Spades
7 of Hearts
7 of Diamonds
Queen of Hearts
Jack of Diamonds
Jack of Spades
9 of Hearts
4 of Diamonds
Queen of Spades
Jack of Clubs
Jack of Hearts
9 of Clubs
8 of Diamonds
4 of Hearts
10 of Hearts
5 of Clubs
Ace of Diamonds
8 of Hearts
5 of Hearts
2 of Diamonds
Ace of Clubs
[--Opened a new deck--]
3 of Diamonds
6 of Diamonds
3 of Spades

[49 cards left in deck] How many cards to draw (0 to quit)? 0

Design

We discussed in lab different ways one might go about implementing this program. You may implement it however you like as long as it meets the requirements above.

One option is to use three classes with the following details:

PlayingCard
Holds a suit (int? char? String??) and a value (int?)
  • Constructor -- Makes a new card
  • toString() -- Returns a String of this card.
Deck
Contains the ArrayList<PlayingCard>.
  • Constructor -- Loads the ArrayList with 52 cards and then calls shuffle
  • shuffle() -- Shuffles this deck
  • draw() -- Draws (and returns) one card
  • getSize() -- Returns how many cards are left in this deck
Assignment13.java
Acts like the dealer
  • main(String[] args) -- Creates a deck and then asks the user how many cards to draw. Draw this many cards, but if, before any draw, the list is empty, replace the empty deck and then continues drawing.

We also discussed how, for this particular program, all we need for a PlayerCard is its String representation. So we could skip this class and just use an ArrayList<String>. Similarly, we could move the behavior of the Deck class into Assignment13.java as well, and just do everything in main. However, at least creating and shuffling a new deck should probably be in its own method, since we have to do it multiple times.

Shuffling

If you need a good shuffling algorithm, check out Fisher-Yates. Here's my version of it in a shuffle method in my Deck class:

  /**
   * Shuffles the cards in this deck using the Fisher-Yates algorithm.
   */
  public void shuffle() {
    PlayingCard temp;
    int tempIndex;
    for (int i = cards.size() - 1; i >= 0; i--){
      tempIndex = (int) (Math.random() * (i + 1));
      if (tempIndex != i){ //swap if different elements
        temp = cards.get(i);
        cards.set(i, cards.get(tempIndex));
        cards.set(tempIndex, temp);
      }
    }
  }

What to submit

Attach all the classes needed to run your program to an email. The class containing your main method should be named Assignment13.java.

FAQs

I understand how to use two for loops to generate the cards. But how do we then translate certain values to Strings? For example, 1 to "Ace", 13 to "King", 2 to "Hearts".
You can use a series of conditionals (or a switch) to determine how to translate your int values to a String. Consider the following:
	String card = "";
	//translate card value
	if (value == 1) {
	  card += "Ace";
	} else if (...) {
	  ...
	}else {
	  card += value;  //for 2 - 10
	}

	//translate suit
	if (suit == 1) {
	  card += " of Diamonds";
	}else if (suit == 2) {
	  card += " of Hearts";
	}else ...

Where you do this is up to you, however. If you're using a PlayingCard class, you could store the suit and value as ints, and then do this translation in the toString() method. Otherwise, in the constructor, you could translate from int parameters to String instance variables.

If you're not using a PlayingCard class, you could just form the String directly in the nested for loops that generate the 52 cards.

My program crashes if I try to draw a lot of cards.
I'd recommend your draw method takes no paramaters and draws only one card at a time. Then, in the UI, you'll be able to check the deck size before drawing. If there are no cards left, you can replace the empty deck with a new deck before drawing the next card.

Grading

Out of 10 points:

1 - Submission and Coding Standards
Follows required submission policies and coding standards.
1 - Compiles
1 - ArrayList
You use an ArrayList to hold your cards (however those might be represented).
3 - Deck
A new deck contains 1 each of the 52 standard playing cards. Each new deck is in different, random/shuffled order.
1 - Cards remaining
Your program clearly states how many cards are left in the current deck.
2 - Drawing
Your program allows the user to draw the given number of cards from the current deck.
1 - New deck
If the user tries to draw more cards than are in the current deck, the rest of the current deck is drawn, a new deck is created, and drawing continues until the total requested number of cards has been drawn.


~ztomasze Index : TA Details: ICS111: Assignment 13
http://www2.hawaii.edu/~ztomasze
Last Edited: 22 May 2008
©2008 by Z. Tomaszewski.