Development Documentation
Architecture Overview
This document outlines the architecture of the card game application, which follows the Model-View-Controller (MVC) architectural pattern with a clear separation between System and Player components.
Project Structure
- index.html
- rules.html
Directorystyle
- base.css
- card.css
Directoryscript
- main.js # Application entry point
- card.js # Card factory for creating card components
- rules.js # Rules page functionality
Directorycontrollers
- GameController.js # Main game controller
- CardController.js # Card component controller
Directorymodels
- SystemModel.js # System logic and state
- PlayerModel.js # Player logic and state
- CardModel.js # Card data model
Directoryviews
- SystemView.js # System UI representation
- PlayerView.js # Player UI representation
- CardView.js # Card UI component
Architectural Patterns
MVC Architecture
The application follows the Model-View-Controller pattern:
-
Models: Responsible for data management and business logic
SystemModel.js
: Manages deck, game rules, and calculationsPlayerModel.js
: Manages player and dealer hands and statesCardModel.js
: Represents a card’s data (suit, rank, state)
-
Views: Handle UI rendering and user interaction
SystemView.js
: Renders system components (deck, counters, admin controls)PlayerView.js
: Renders player components (hands, action buttons)CardView.js
: Custom element for rendering individual cards
-
Controllers: Coordinate models and views
GameController.js
: Main controller orchestrating game flowCardController.js
: Controls individual card behavior
Factory Pattern
The CardFactory
in card.js
implements the factory pattern to create cards:
- Creates card components with proper MVC structure
- Registers custom elements
- Provides convenience methods for card creation
Initialization Flow
- The application starts in
main.js
when the DOM is loaded GameController
is instantiated, which:- Creates models (SystemModel, PlayerModel)
- Creates views (SystemView, PlayerView)
- Initializes the game by creating and shuffling a deck
- Binds event handlers
Game Flow
-
Game Initialization:
- Deck is created with 52 cards (4 suits × 13 ranks)
- Cards are arranged in a stack
- UI counters are updated
-
New Game:
- Hands are cleared
- Initial cards are dealt (2 to player, 2 to dealer with one face down)
- Blackjack checks are performed
-
Player Turn:
- Player can Hit (take another card) or Stand (end turn)
- Bust condition is checked after each Hit
-
Dealer Turn:
- Dealer reveals face-down card
- Dealer draws cards according to rules (hits until 17+)
- Bust condition is checked after each draw
-
End Game:
- Winner is determined
- Results are displayed
- Game can be reset or a new game started
Component Interaction
GameController
orchestrates all game components- Card interactions are managed through
CardController
- Models communicate state changes to controllers
- Controllers update views based on model changes
- Views trigger events that controllers handle
UI Components
- System Components: Deck, card counter, admin controls, game controls
- Player Components: Player hand, dealer hand, action buttons
- Card Components: Individual cards with flip animation
Event Handling
The application uses a centralized event handling approach:
- Events are bound in the
GameController
- Views expose methods to bind handlers
- Controllers process events and update models/views accordingly
Custom Elements
The application uses Web Components:
CardView
is registered as a custom element (card-element
)- This allows encapsulation of card behavior and styling
Future Development
For future development, consider:
- Adding more game variants
- Implementing player accounts and statistics
- Adding multiplayer functionality
- Enhancing animations and visual effects
- Implementing advanced betting mechanics