import java.util.Random; /** * CS 231 — Project 1 * Deck * * Purpose: * Represents the deck used by the Blackjack game. A Deck stores Card objects * in the ArrayList class completed in Lab 1 and can build, deal, and shuffle * those cards. */ public class Deck { // TODO: Declare a private ArrayList instance variable. // This ArrayList stores the cards currently remaining in the deck. /** * Constructs a complete, shuffled 52-card deck. */ public Deck() { // TODO: // 1. Create the ArrayList that will hold the Cards. // 2. Call build() to create and shuffle the complete deck. } /** * Builds a standard 52-card deck, replacing any deck currently stored, * and then shuffles the completed deck. * * The deck contains: * - four cards of each value 2 through 9 * - sixteen cards with value 10 * - four cards with value 11 */ public void build() { // TODO: Replace the current deck with a new empty ArrayList. // // TODO: Add the correct 52 Card objects. // // Think about how loops can avoid writing 52 separate add statements. // Once the complete Deck has been built, shuffle it. // This call is provided because the work of this task is implementing // shuffle(), not deciding when a newly built Deck should be shuffled. shuffle(); } /** * Returns the number of cards currently remaining in the deck. * * @return the number of cards in the deck */ public int size() { // TODO: Return the number of Cards stored in the ArrayList. return 0; } /** * Returns and removes the top card of the deck. * * The top card is the Card at index 0. * * @return the top card */ public Card deal() { // TODO: Use the ArrayList operation that both removes and returns // the element at an index. return null; } /** * Shuffles the cards currently in the deck. * * The shuffle must rearrange the existing Cards without adding Cards, * losing Cards, or changing their values. */ public void shuffle() { // TODO: Create a new, empty ArrayList that will hold // the Cards in shuffled order. // // TODO: Create a Random object. // // While Cards remain in the Deck's current ArrayList: // // 1. Generate a random valid index: // // int index = rand.nextInt(cards.size()); // // Replace "cards" with the name of your Deck's ArrayList variable. // // 2. Remove the Card at that index from the current ArrayList. // Remember: remove(index) both removes AND returns the Card. // // 3. Add the returned Card to the new ArrayList. // // Notice that size() becomes smaller after each removal, so the next // random index is selected only from Cards that remain. // // TODO: When the original ArrayList is empty, make the Deck's // instance variable refer to the new shuffled ArrayList. } /** * Returns a String representation of the cards currently in the deck. * * @return a String representation of the deck */ public String toString() { // TODO: Return a useful String representation of the deck. // // Hint: Before writing another loop, consider whether the ArrayList // holding the Cards already knows how to represent itself as a String. return ""; } }