How to perform multiple 5‑card draws from a shuffled 52‑card deck in Java

1 Answer

0 votes
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
A standard deck of 52 playing cards consists of four suits:
spades (♠), hearts (♥), diamonds (♦), and clubs (♣).

Each suit contains 13 ranks:
Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King.

Cards of spades and clubs are black, while hearts and diamonds are red.

Color is informational only — it does not affect shuffling or random selection.
*/

/**
    This program performs multiple draws of 5 random cards from a 52‑card deck
    WITHOUT duplicates across draws.

    It uses:
    - List<String> for the deck
    - Collections.shuffle for efficient randomization
    - Functions for clean structure

    Algorithm:
    1. Build a full deck (52 cards)
    2. For each draw:
       a. Shuffle the remaining deck
       b. Draw 5 cards
       c. Remove them from the deck
*/

public class CardDraws {

    // Build a full 52-card deck
    public static List<String> buildDeck() {
        String[] ranks = {
            "2","3","4","5","6","7","8","9","10","J","Q","K","A"
        };
        String[] suits = {
            "Clubs", "Diamonds", "Hearts", "Spades"
        };

        List<String> deck = new ArrayList<>(52);

        for (String suit : suits) {
            for (String rank : ranks) {
                deck.add(rank + " of " + suit);
            }
        }

        return deck;
    }

    // Shuffle and draw 5 cards, then remove them
    public static void drawFive(List<String> deck, int drawNumber) {
        Collections.shuffle(deck);

        System.out.println("Draw #" + drawNumber + ":");
        for (int i = 0; i < 5; i++) {
            System.out.println("  " + deck.get(i));
        }
        System.out.println();

        // Remove drawn cards
        for (int i = 0; i < 5; i++) {
            deck.remove(0);
        }
    }

    public static void main(String[] args) {

        // Number of draws
        int draws = 3;

        // Build the deck
        List<String> deck = buildDeck();

        // Perform multiple draws (no duplicates)
        for (int i = 1; i <= draws; i++) {
            if (deck.size() < 5) {
                System.out.println("Not enough cards left for draw #" + i + ".");
                break;
            }
            drawFive(deck, i);
        }
    }
}


/*
run:

Draw #1:
  4 of Clubs
  Q of Hearts
  2 of Spades
  9 of Spades
  5 of Clubs

Draw #2:
  3 of Diamonds
  3 of Hearts
  J of Clubs
  J of Hearts
  2 of Hearts

Draw #3:
  6 of Spades
  8 of Hearts
  7 of Hearts
  Q of Diamonds
  10 of Clubs

*/

 



answered 14 hours ago by avibootz

Related questions

...