Just a heads-up, this project is a console app built with Xcode, not a mobile app. We were specifically instructed to develop it as a console application.
Introduction
This Swift-based Solitaire Prime game is a twist on classic solitaire where you’re not just stacking cards, but you’re hunting for prime piles. The goal is to manage the cards wisely and hit those primes to win. You draw cards, sum them up, and hope for prime piles. The menu lets you start a new game, display the deck, shuffle it, play, or exit when you’re done. The code features classes for cards and decks, along with functions to play the game and handle user interaction. Using a regular deck, you’ll try to build piles of cards that add up to a prime number. When the game ends, if your last pile is prime, you win—otherwise, you lose.
Rules
- Select the top card from the deck and place it face-up on the table.
- The sum is now the value of that card (Ace = 1, 2 = 2, 3 = 3, …, Jack = 10, Queen = 10, King = 10).
- If the sum is prime, discard the pile and begin again with the next card.
- If the sum is not prime, add the next card from the deck to the pile.
- Repeat steps 2-4 until a prime sum is achieved or all cards have been used
Game Features
Flowchart
Print Screens and Code
1. New Deck
func refreshDeck() {
cards.removeAll()
let suits = ["♠️", "♥️", "♣️", "♦️"]
let ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
for suit in suits {
for rank in ranks {
cards.append(Card(rank: rank, suit: Character(suit)))
}
}
}
The refreshDeck() function creates a full deck of 52 cards (13 ranks × 4 suits) and stores them in the cards array. It’s your go-to function for resetting or reshuffling the deck.
2. Display Deck
3. Shuffle Deck
4. Play Solitaire Prime

func isPrime(_ number: Int) -> Bool {
if number <= 1 {
return false
}
if number <= 3 {
return true
}
if number % 2 == 0 || number % 3 == 0 {
return false
}
var i = 5
while i * i <= number {
if number % i == 0 || number % (i + 2) == 0 {
return false
}
i += 6
}
return true
}
// Main function to play Solitaire Prime
func playSolitairePrime(deck: Deck) {
var piles = 0
var sum = 0
var lastPileIsPrime = false
while deck.cardsLeft() > 0 {
if let card = deck.deal() {
sum += card.getValue()
print(card.showCard(), terminator: " ")
if isPrime(sum) {
print("Prime Pile!")
piles += 1
lastPileIsPrime = true
sum = 0
} else {
lastPileIsPrime = false
}
}
}
if lastPileIsPrime {
print("Winner! Number of prime piles: \(piles)")
} else {
print("Loser")
}
}
This function deals cards one by one, adding their values to a pile (sum). After each card, it checks if the total is prime. If it is, the pile becomes a “Prime Pile,” and a new one starts. Once all cards are dealt, the game ends—if the last pile was prime, you win and see how many prime piles you made; if not, you lose!
5. Exit
Conclusion
This project was my first dive into building a system from scratch with Swift and Xcode. I learned so much along the way and am pretty proud of how it turned out.
Tech Stack

