Card War: Displaying the Cards & Score
Build Interactive Card Game UI with Swift Programming
Core Development Components
UIImageView Implementation
Create dynamic card display containers that hold and manage card images throughout gameplay. Essential for visual card representation.
Programmatic Constraints
Set precise positioning and sizing for UI elements using code instead of Storyboard. Provides dynamic layout control.
Game State Management
Handle scoring, card drawing, and game restart functionality. Critical for maintaining proper game flow and user experience.
Development Workflow Overview
Create Card Containers
Build UIImageViews to hold the card images for both players during gameplay rounds
Position Elements
Set programmatic constraints to properly size and position cards relative to existing UI elements
Implement Game Logic
Add scoring system, card reveal functionality, and game restart capabilities
Complete previous exercises 5A-5C before proceeding. Ensure Main.storyboard, Data Model.swift, and ViewController.swift are open in separate tabs for efficient development workflow.
To deepen the element of surprise that makes games of chance so exciting, we're assigning the image of the card back, NOT the front image of the actual card that was drawn.
The cardsImageViews array maintains handles to all generated cards, enabling proper cleanup when the game resets. The IV suffix stands for Image View, following iOS naming conventions.
Card Sizing Calculations
Programmatic Constraint Process
Analyze Storyboard Constraints
Examine existing Deck button constraints to match dimensions and positioning
Calculate Frame Dimensions
Use CGRect with mathematical calculations based on view frame proportions
Position Using Center Points
Set center.X and center.Y coordinates relative to view center with multipliers
When the card object was initialized in the data model, it fetched its own image. The data model interacts with the View Controller to display information visually in the UI.
Score Update Implementation
Compare Card Values
Use if statements to determine which player has the higher value card
Increment Winner Score
Add 1 point to the winning player's score variable using += operator
Update UI Automatically
didSet property observer watches score variables and updates label outlets accordingly
Game Restart Requirements
Provides fresh gameplay experience with randomized card distribution
Tracks number of cards drawn and enables proper game state management
Provides clean slate for new game session
Prevent crashes when all 26 cards have been drawn
Key Takeaways

in our UI to the restartGame function we're about to implement. Clean up the restartButton function to call our restart logic efficiently: