Dice Foundry Exercises and Challenges

Chapter 4 Exercises — State, Functions, and Random Numbers

These exercises build on the Chapter 4 project. Do them on a separate Git branch so your guided project stays identical to the reference checkpoint:

First commit and tag the guided work using the workbook's Development Checkpoint. Confirm git status --short prints nothing, then create the branch:

git switch -c challenge-04

When you are finished (whether or not you solved everything), commit your challenge edits (including unfinished work), then return to the main line before starting Chapter 5:

Uncommitted edits can follow you across branches. Save them before switching; if nothing changed, skip add and commit. The status command should print nothing:

git add -A
git commit -m "Challenge 04: Save practice work"
git status --short
git switch main

Solutions are in the Chapter 4 Answers document. Try each hint in order and only read the next one when you are stuck.

Challenge 1 — A twenty-sided dice roller

Goal: let the user roll either a six-sided die (D6) or a twenty-sided die (D20).

Requirements:

  • Change only ContentView.swift.
  • Replace the single "Roll Dice" button with two buttons side by side: "Roll D6" and "Roll D20".
  • Both buttons keep the current button styling (title2 font, bordered prominent style, large control size, orange tint).
  • "Roll D6" shows a random number from 1 to 6; "Roll D20" shows a random number from 1 to 20.
  • The caption shows which die was rolled last: "Rolling a D6" or "Rolling a D20". Before any roll it reads "Rolling a D6".
  • Use your existing rollDie(sides:) function for both dice. Do not write a second rolling function.

Acceptance criteria:

  • The project builds with no errors or warnings.
  • 30 taps on "Roll D20" produce only values from 1 to 20, including some values above 6.
  • 30 taps on "Roll D6" produce only values from 1 to 6.
  • After every tap, the caption names the die that produced the number on screen.

Hint 1

Start with the part that needs no new ideas: a second button whose action calls rollDie with a different argument. Your function already accepts any number of sides; that is why sides is a parameter. To put two buttons side by side, think about which stack arranges views horizontally.

Hint 2

The caption must change depending on which button was tapped last. Anything that changes on screen in response to a tap needs to be stored somewhere SwiftUI watches. Add a second state property, next to result, that remembers how many sides the last-rolled die had. Its initial value should make the caption correct before any roll. The caption can then use string interpolation, just as the die display does.

Hint 3

Each button's action now has two jobs: record which die is being rolled in your new state property, and store a roll of that die in result. That is two lines in each closure. For the shared styling, remember that some modifiers flow down to the views inside a container: try moving the existing four button modifiers from the single button to the HStack that contains both buttons, then check the canvas.

Extra Practice

Exercise 1 — Predict the output

What does this code (not part of the project) print?

Example — not part of the Dice Foundry project
func describe(roll: Int, sides: Int) -> String {
    let label = "D\(sides)"
    return "\(label) rolled \(roll)"
}

let first = describe(roll: 3, sides: 6)
let second = describe(roll: 17, sides: 20)
print(first)
print(second)

Exercise 2 — Spot the bug

A student's button compiles, but the die keeps showing 0 no matter how many times they tap. Find the bug and explain why it compiles.

Broken code — for diagnosis practice
            Button("Roll Dice") {
                let result = rollDie(sides: 6)
            }

Exercise 3 — Spot the bug

This version of the roll function compiles and runs. Over 100 rolls, the student notices that the die never shows a 6. Why, and what is the one-character fix?

Broken code — for diagnosis practice
    func rollDie(sides: Int) -> Int {
        let roll = Int.random(in: 1..<sides)
        return roll
    }

Exercise 4 — Labels and names

Rewrite the declaration of rollDie so that callers write rollDie(withSides: 6) but the code inside the function still uses the name sides. Then write the call that the button would use. (Do this on your challenge branch, or just on paper; do not leave it in your main project.)

Exercise 5 — Predict the value

Trace this example (not part of the project) and give the final value of count. Which names are local to the function?

Example — not part of the Dice Foundry project
func addRolls(first: Int, second: Int) -> Int {
    let sum = first + second
    return sum
}

var count = 0
count = addRolls(first: 2, second: 5)
count = addRolls(first: count, second: count)