A Mastermind-inspired code-breaking puzzle game built entirely with SwiftUI and SwiftData.
Crack the hidden code by placing pegs and analyzing match feedback after each guess. Choose from color palettes and emoji themes, manage multiple games at once, and track your solve time.
- Classic Mastermind mechanics — guess a secret code made up of colored or emoji pegs, receiving feedback after each attempt
- Match markers — filled circles indicate exact matches (right peg, right position), outlined circles indicate inexact matches (right peg, wrong position)
- Variable difficulty — codes range from 3 to 6 peg slots, with 6 peg choices per theme
- Duplicate prevention — the same guess cannot be submitted twice
- Win detection — the master code is automatically revealed when cracked
9 built-in peg themes across two categories:
| Color Themes | Emoji Themes |
|---|---|
| Warm | Faces |
| Cool | Elements |
| Earth Tones | Vehicles |
| Rainbow | Animals |
| Fruits |
- Multi-game support — create and manage multiple concurrent games from a split-view navigation interface
- Search — filter games by name with case-insensitive search
- Sort options — sort by name, most recent activity, or view only completed games
- Edit games — rename games and customize peg choices (add/remove color or emoji pegs) via a sheet editor with swipe-to-edit
- Delete games — swipe-to-delete or use context menus
- Sample games — bundled JSON files are automatically loaded on first launch to populate the game list
- Tap — select a peg slot in your guess row, then tap a peg from the chooser bar to place it
- Rotation — use a two-finger rotation gesture anywhere on the game screen to cycle through peg choices for the selected slot
- Magnification — pinch on the game list to resize game summaries between compact, regular, and large layouts
- SwiftData — all game state (codes, attempts, themes, timing) is persisted automatically via
@Modelclasses with cascade delete rules - Scene-aware elapsed time — the timer pauses when the app goes to background and resumes when active, with elapsed time saved to the SwiftData store on every save cycle
- JSON export — save any game to a JSON file via the toolbar button
- JSON import — sample games are decoded from bundled
.jsonfiles at first launch - Codable conformance — both
CodeBreakerandCodemodels fully supportCodablefor serialization
- NavigationSplitView — balanced split-view layout with a game list sidebar and detail game view
- Diamond-shaped pegs — custom
Shapeimplementation for a distinctive peg appearance - Animated transitions — guess submissions slide in from the top, match markers fade in after the row lands, and the board fades during restarts
- Live timer — elapsed time displayed in the toolbar using
TimeDataSourcefor continuous updates, freezing at the final time on win - Pause indicator — displays a pause icon when the timer is inactive (app backgrounded)
- Context menus — long-press any game row for quick edit and delete actions
- Color picker — edit color pegs with the system
ColorPicker, supporting hex color storage - Emoji editing — edit emoji pegs inline with a text field, limited to a single character
CodeBreaker/
├── App/
│ └── CodeBreakerApp.swift # @main entry point, ModelContainer setup
├── Models/
│ ├── CodeBreaker.swift # Core game model (@Model), PegTheme, Peg typealias
│ ├── Code.swift # Code model (@Model), Match enum
│ ├── CodeBreaker+Codable.swift # Codable conformance for CodeBreaker and Code
│ └── Kind.swift # Code.Kind enum with string serialization
├── Views/
│ ├── GameSelector.swift # Root NavigationSplitView with search and sort
│ ├── GameList.swift # @Query-powered game list with CRUD operations
│ ├── GameSummary.swift # Compact game preview (3 size variants)
│ ├── GameEditor.swift # Sheet for editing game name and peg choices
│ ├── CodeBreakerView.swift # Main game screen with guess/restart/save toolbar
│ ├── CodeView.swift # Generic peg row with selection highlight
│ ├── PegView.swift # Single peg renderer (color, emoji, or empty)
│ ├── PegChooser.swift # Tappable peg palette bar
│ ├── PegChoicesChooser.swift # Editable peg list for the game editor
│ ├── MatchMarkers.swift # Grid of match indicator circles
│ ├── ElapsedTime.swift # Live/frozen elapsed time display
│ ├── Diamond.swift # Custom diamond Shape for pegs
│ └── SwiftDataPreview.swift # PreviewModifier for in-memory SwiftData previews
├── UI/
│ ├── UIExtension.swift # Animation, transition, and color extensions
│ ├── ElapsedTimeTracker.swift # ViewModifier for scene-aware timer management
│ └── Color+String.swift # Color ↔ name/hex string conversion
└── Sample Games/
├── Elements.json # Pre-built game with element emoji theme
├── Fruits.json # Pre-built game with fruit emoji theme
└── Vehicles.json # Pre-built game with vehicle emoji theme
- iOS 18.0+
- Xcode 16.0+
- Swift 6.0+
- Clone the repository:
git clone https://github.com/macnoblebk/CodeBreaker.git
- Open
CodeBreaker.xcodeprojin Xcode - Build and run on a simulator or device (iPhone or iPad)
Three sample games are loaded automatically on first launch. Tap + to create your own.
- Select a game from the sidebar (or create a new one)
- Tap a slot in the guess row to select it
- Tap a peg from the chooser bar at the bottom (or use the rotation gesture)
- Fill all slots, then tap Guess
- Read the match markers next to each attempt:
- Filled circle = correct peg in the correct position
- Outlined circle = correct peg in the wrong position
- No circle = peg not in the code
- Repeat until you crack the code
This project is provided for educational purposes.


