Knowledge Guide
HomeOO & Low-Level DesignOO Design Problems

Design Blackjack and a Deck of Cards

Let's design a game of Blackjack.

Blackjack is the most widely played casino game in the world. It falls under the category of comparing-card games and is usually played between several players and a dealer. Each player, in turn, competes against the dealer, but players do not play against each other. In Blackjack, all players and the dealer try to build a hand that totals 21 points without going over. The hand closest to 21 wins.

Image
Image

System Requirements

Blackjack is played with one or more standard 52-card decks. The standard deck has 13 ranks in 4 suits.

Background

Points calculation

Blackjack has different point values for each of the cards:

Gameplay

  1. The player places an initial bet.
  2. The player and dealer are each dealt a pair of cards.
  3. Both of the player’s cards are face up, the dealer has one card up and one card down.
  4. If the dealer’s card is an ace, the player is offered insurance.

Initially, the player has a number of choices:

If the player’s hand is over 21, their bet is resolved immediately as a loss. If the player’s hand is 21 or less, it will be compared to the dealer’s hand for resolution.

Dealer has an Ace. If the dealer’s up card is an ace, the player is offered an insurance bet. This is an additional proposition that pays 2:1 if the dealer’s hand is exactly 21. If this insurance bet wins, it will, in effect, cancel the loss of the initial bet. After offering insurance to the player, the dealer will check their hole card and resolve the insurance bets. If the hole card is a 10-point card, the dealer has blackjack, the card is revealed, and insurance bets are paid. If the hole card is not a 10-point card, the insurance bets are lost, but the card is not revealed.

Split Hands. When dealt two cards of the same rank, the player can split the cards to create two hands. This requires an additional bet on the new hand. The dealer will deal an additional card to each new hand, and the hands are played independently. Generally, the typical scenario described above applies to each of these hands.

Bets

Use case diagram

We have two main Actors in our system:

Typical Blackjack Game Use cases

Here are the top use cases of the Blackjack game:

Image
Image

Class diagram

Here are the main classes of our Blackjack game:

Image
Image
Image
Image

Activity diagrams

Blackjack hit or stand: Here are the set of steps to play blackjack with hit or stand:

Image
Image

Code

Enums: Here are the required enums:

java
public enum SUIT {
  HEART, SPADE, CLUB, DIAMOND
}

Card: The following class encapsulates a playing card:

java
public class Card {
    private SUIT suit;
    private int faceValue;

    public SUIT getSuit() {
        return suit;
    }

    public int getFaceValue() {
        return faceValue;
    }

    Card(SUIT suit, int faceValue) {
        this.suit = suit;
        this.faceValue = faceValue;
    }
}

BlackjackCard: BlackjackCard extends from Card class to represent a blackjack card:

java
public class BlackjackCard extends Card {
    private int gameValue;

    public int getGameValue() {
        return gameValue;
    }

    public BlackjackCard(SUIT suit, int faceValue) {
        super(suit, faceValue);
        this.gameValue = faceValue;
        if (this.gameValue > 10) {
            this.gameValue = 10;
        }
    }
}

Deck and Shoe: Shoe contains cards from multiple decks:

java
public class Deck {
    private List<BlackjackCard> cards;
    private Date creationDate;

    public Deck() {
        this.creationDate = new Date();
        this.cards = new ArrayList<BlackjackCard>();
        for (int value = 1; value <= 13; value++) {
            for (SUIT suit : SUIT.values()) {
                this.cards.add(new BlackjackCard(suit, value));
            }
        }
    }

    public List<BlackjackCard> getCards() {
        return cards;
    }

    public class Shoe {
        private List<BlackjackCard> cards;
        private int numberOfDecks;

        private void createShoe() {
            this.cards = new ArrayList<BlackjackCard>();
            for (int decks = 0; decks < numberOfDecks; decks++) {
                cards.add(new Deck().getCards());
            }
        }

        public Shoe(int numberOfDecks) {
            this.numberOfDecks = numberOfDecks;
            createShoe();
            shuffle();
        }

        public void shuffle() {
            int cardCount = cards.size();
            Random r = new Random();
            for (int i = 0; i < cardCount; i++) {
                int index = r.nextInt(cardCount - i - 1);
                swap(i, index);
            }
        }

        public void swap(int i, int j) {
            BlackjackCard temp = cards[i];
            cards[i] = cards[j];
            cards[j] = temp;
        }

        // Get the next card from the shoe
        public BlackjackCard dealCard() {
            if (cards.size() == 0) {
                createShoe();
            }
            return cards.remove(0);
        }
    }
}

Hand: Hand class encapsulates a blackjack hand which can contain multiple cards:

java
public class Hand {
    private ArrayList<BlackjackCard> cards;

    private List<Integer> getScores() {
        List<Integer> totals = new ArrayList();
        total.add(0);

        for (BlackjackCard card : cards) {
            List<Integer> newTotals = new ArrayList();
            for (int score : totals) {
                newTotals.add(card.faceValue() + score);
                if (card.faceValue() == 1) {
                    newTotals.add(11 + score);
                }
            }
            totals = newTotals;
        }
        return totals;
    }

    public Hand(BlackjackCard c1, BlackjackCard c2) {
        this.cards = new ArrayList<BlackjackCard>();
        this.cards.add(c1);
        this.cards.add(c2);
    }

    public void addCard(BlackjackCard card) {
        cards.add(card);
    }

    // get highest score which is less than or equal to 21
    public int resolveScore() {
        List<Integer> scores = getScores();
        int bestScore = 0;
        for (int score : scores) {
            if (score <= 21 && score > bestScore) {
                bestScore = score;
            }
        }
        return bestScore;
    }
}

Player: Player class extends from BasePlayer:

java
public abstract class BasePlayer {
    private String id;
    private String password;
    private double balance;
    private AccountStatus status;
    private Person person;
    private List<Hand> hands;

    public boolean resetPassword();

    public List<Hand> getHands() {
        return hands;
    }

    public void addHand(Hand hand) {
        return hands.add(hand);
    }

    public void removeHand(Hand hand) {
        hands.remove(hand);
    }
}

public class Player extends BasePlayer {
    private int bet;
    private int totalCash;

    public Player(Hand hand) {
        this.hands = new ArrayList<Hand>();
        this.hands.add(hand);
    }
}

public class Dealer extends BasePlayer {
    public Dealer(String id, String password, double balance, String status, Person person) {
        super(id, password, balance, status, person);
    }
}

Game: This class encapsulates a blackjack game:

java
public class Game {
    private Player player;
    private Dealer dealer;
    private Shoe shoe;
    private final int MAX_NUM_OF_DECKS = 3;

    private void playAction(string action, Hand hand) {
        switch (action) {
            case "hit":
                hit(hand);
                break;
            case "split":
                split(hand);
                break;
            case "stand pat":
                break; // do nothing
            case "stand":
                stand();
                break;
            default:
                print("Wrong input");
        }
    }

    private void hit(Hand hand) {
        hand.addCard(shoe.dealCard());
    }

    private void stand() {
        int dealerScore = dealer.getTotalScore();
        int playerScore = player.getTotalScore();
        List<Hand> hands = player.getHands();
        for (Hand hand : hands) {
            int bestScore = hand.resolveScore();
            if (playerScore == 21) {
                // blackjack, pay 3:2 of the bet
            } else if (playerScore > dealerScore) {
                // pay player equal to the bet
            } else if (playerScore < dealerScore) {
                // collect the bet from the player
            } else { // tie
                // bet goes back to player
            }
        }
    }

    private void split(Hand hand) {
        Cards cards = hand.getCards();
        player.addHand(new Hand(cards[0], shoe.dealCard()));
        player.addHand(new Hand(cards[1], shoe.dealCard()));
        player.removeHand(hand);
    }

    public Game(Player player, Dealer dealer) {
        this.player = player;
        this.dealer = dealeer;
        Shoe shoe = new Shoe(MAX_NUM_OF_DECKS);
    }

    public void start() {
        player.placeBet(getBetFromUI());

        Hand playerHand = new Hand(shoe.dealCard(), shoe.dealCard());
        player.addToHand(playerHand);

        Hand dealerHand = new Hand(shoe.dealCard(), shoe.dealCard());
        dealer.addToHand(dealerHand);

        while (true) {
            List<Hand> hands = player.getHands();
            for (Hand hand : hands) {
                string action = getUserAction(hand);
                playAction(action, hand);
                if (action.equals("stand")) {
                    break;
                }
            }
        }
    }

    public static void main(String args[]) {
        Player player = new Player();
        Dealer dealer = new Dealer();
        Game game = new Game(player, dealer);
        game.start();
    }
}
🤖 Don't fully get this? Learn it with Claude

Stuck on Design Blackjack and a Deck of Cards? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Design Blackjack and a Deck of Cards** (OO & Low-Level Design) and want to truly understand it. Explain Design Blackjack and a Deck of Cards from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Design Blackjack and a Deck of Cards** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Design Blackjack and a Deck of Cards** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Design Blackjack and a Deck of Cards** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes