Swift-based Solitaire Prime game

Swift-based Solitaire Prime game

Evelyn

Evelyn

October 1, 2022

read

#Xcode #Game #swift

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

  1. Select the top card from the deck and place it face-up on the table.
  2. The sum is now the value of that card (Ace = 1, 2 = 2, 3 = 3, …, Jack = 10, Queen = 10, King = 10).
  3. If the sum is prime, discard the pile and begin again with the next card.
  4. If the sum is not prime, add the next card from the deck to the pile.
  5. Repeat steps 2-4 until a prime sum is achieved or all cards have been used

Game Features

homepage

game_features.png
When you launch SolitairePrime, you’ll be greeted with the Welcome screen. This is your starting point, where you’ll find a menu of options to guide you through the game.

Flowchart

homepage

flowchart.png
This flowchart outlines the game’s main features and how they interact. It’s a visual guide to help you understand the game’s flow and how each feature works together.

Print Screens and Code

1. New Deck

new deck

new_deck.png
The ‘New Deck’ menu lets you start a fresh game with a brand-new shuffled deck. It’s your go-to option to kick off a new round of Solitaire Prime. Once you’re ready, just pick your next move and dive into the game.

SolitairePrime.swift
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

display deck

display_deck.png
The “Display Deck” menu lets you see all the cards in the deck, giving you a peek at how things are arranged.

3. Shuffle Deck

shuffle deck

shuffle_deck.png
The “Shuffle Deck” menu randomizes the deck, giving the cards a complete mix-up. Once shuffled, you can check out the new order by choosing the “Display Deck” option (Option 2) to see the fresh, randomized deck in action.

4. Play Solitaire Prime

play solitaire prime

play_solitaire_prime_1.png
Figure 4.5 shows the “Play Solitaire Prime” menu, where the real action happens! Track your prime piles as you play—stack those primes and aim for victory. If the last card forms a prime pile, you win and see your total prime pile count.

play solitaire prime
play_solitaire_prime_2.png
If the last card doesn’t make a prime pile, you’ll see the “Loser” outcome, marking the end of your game. Better luck next time!

SolitairePrime.swift
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

exit

exit.png
The “Exit Solitaire Prime” menu, giving you the option to leave the game and head back to the main menu or close the app completely.

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

XcodeXcode
SwiftSwift