App 16 — 24 Apps You Can Make Today

Rock, Paper, Scissors

Play rock, paper, scissors against the phone — enum-based game logic, random computer moves.

  • ios
↓ Download Playgrounds File

Three buttons. One random opponent. Win, lose, or draw. Rock, Paper, Scissors is a small game with surprisingly clean code — especially once you model the choices as an enum.

What you'll build

Three buttons for your move (rock, paper, scissors), a display of the computer's move, and a result: win, lose, or draw. The computer picks randomly every time.

Key concepts

enum for moves — Represent the three choices as cases. Enums are perfect when there's a fixed set of options.

enum Move: CaseIterable {
    case rock, paper, scissors

    var emoji: String {
        switch self {
        case .rock:     return "🪨"
        case .paper:    return "📄"
        case .scissors: return "✂️"
        }
    }
}

CaseIterable — Conforming to CaseIterable gives you Move.allCases, an array of all cases. Use it to generate the buttons and to pick a random computer move.

let computerMove = Move.allCases.randomElement()!

Game logic as a function — Write a function that takes two moves and returns the result. Keep it separate from the view.

func result(player: Move, computer: Move) -> String {
    if player == computer { return "Draw" }
    switch (player, computer) {
    case (.rock, .scissors), (.paper, .rock), (.scissors, .paper):
        return "You win!"
    default:
        return "Computer wins."
    }
}

Logic belongs in functions

The view's job is to display and receive input — not to run game logic. Putting result() in a separate function makes it easy to test, easy to read, and easy to change.

Download and run it

Open the Playgrounds file from GitHub, hit Run, and play.

← All apps in this book