Skip to main content

Problem Statement

Design a flexible deck of cards system that supports generic card games and can be extended for specific games like BlackJack.

Constraints and Assumptions

  • Design a generic deck, then extend for BlackJack
  • Standard 52-card deck (2-10, Jack, Queen, King, Ace) with 4 suits
  • Inputs are valid (no validation needed)
  • Deck fits in memory

Design Overview

The implementation uses inheritance and abstraction to create a flexible card game system:
  1. Suit: Enum for card suits
  2. Card: Abstract base class for all cards
  3. BlackJackCard: Concrete card implementation for BlackJack
  4. Hand: Generic hand of cards
  5. BlackJackHand: BlackJack-specific hand with scoring
  6. Deck: Card deck with dealing and shuffling
This design uses the Template Method pattern, where the base Card class defines the structure, and game-specific classes like BlackJackCard implement the details. This allows easy extension to other card games.

Implementation

Suit Enumeration

Card Abstract Class

Base class for all cards with abstract value property:

BlackJackCard Class

Implements BlackJack-specific card logic:

Hand Classes

Generic hand and BlackJack-specific implementation:

Deck Class

Manages the collection of cards:

Key Design Patterns

Template Method Pattern

The abstract Card class defines the structure, with concrete implementations providing game-specific details:

Strategy Pattern

Different scoring strategies for different games:
  • Generic Hand: Simple sum of card values
  • BlackJack Hand: Complex scoring with Ace flexibility (1 or 11)

Inheritance Hierarchy

BlackJack-Specific Features

Ace Handling

Aces can be worth 1 or 11 in BlackJack. The BlackJackHand class calculates all possible scores:
  • Multiple Aces: Each ace can independently be 1 or 11
  • Optimal score: Choose the best score ≤ 21, or the lowest score > 21 if busted

Face Card Values

Jack, Queen, and King are all worth 10 points:

Complexity Analysis

BlackJack scoring has exponential complexity relative to the number of Aces because it must consider all possible value combinations (1 or 11 for each Ace). However, since a hand typically has at most 4 Aces, this remains practical.

Design Considerations

Advantages

  • Extensible: Easy to add new card games by extending base classes
  • Reusable: Generic classes (Card, Hand, Deck) work for multiple games
  • Type safety: Abstract properties enforce implementation of game-specific logic
  • Clear separation: Game rules in specialized classes, not in base classes

Extension to Other Games

This design easily extends to other card games:
  1. Poker: Create PokerCard and PokerHand with hand ranking logic
  2. Uno: Create UnoCard with colors and special cards
  3. Bridge: Add bidding and trick-taking logic

Potential Improvements

  1. Card factory: Create cards using a factory pattern
  2. Deck initialization: Auto-populate deck with 52 standard cards
  3. Multiple decks: Support games that use multiple decks
  4. Card images: Add visual representation of cards
  5. Game state: Track game progression, bets, winners
  6. Player management: Add player classes with hands and chips
  7. Shuffle algorithm: Implement cryptographically secure shuffling for online play