Dice Foundry Exercises and Challenges

Chapter 7 Exercises — Conditional Logic and Modifiers

These exercises build on the Chapter 7 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-07

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 8:

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 07: Save practice work"
git status --short
git switch main

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

Challenge 1 — Rate the total

Goal: practice switch with range cases, including a one-sided range, and conditional display.

Requirements:

  • Change only ContentView.swift.
  • Write a function that takes an Int and returns a String:
  • 0 or less → "Ouch"
  • 1 to 5 → "Low"
  • 6 to 14 → "Solid"
  • 15 or more → "High"
  • Use a switch, not an if chain.
  • Show the rating of the roll's total in the result area, below the existing content.

Acceptance criteria:

  • The project builds with no errors or warnings.
  • Before the first roll, no rating is shown.
  • 1D4 with modifier -20 always shows "Ouch". 10D20 with modifier +20 always shows "High".
  • Rolling 1D6 with modifier 0 shows "Low" or "Solid" depending on the die (1 to 5 is "Low", 6 is "Solid").
  • Changing any control after a roll does not change the rating.

Hint 1

Which value should you rate: subtotal, total, or something built from modifier? Remember the main lesson of the chapter: the result area must only read values that describe the roll that happened. Only one of those names qualifies.

Hint 2

You need four cases. Three can be written as ranges you already know: a closed range such as 1...5, and so on. For "0 or less", use a one-sided range that has no lower end. You wrote 1... (no upper end) in formattedModifier; the mirror image has the dots on the left. Either ...0 or ..<1 works.

Hint 3

Model the function on formattedModifier: func rating(for value: Int) -> String { switch value { case ...: return "..." ... default: return "High" } }. The last group (15 and up) can be default, which also satisfies the exhaustiveness rule. Then add a Text(rating(for: total)) as the last view inside the else block of if results.isEmpty.

Challenge 2 — Natural 20 and natural 1

Goal: combine conditions with &&, nest if statements in a view, and apply the snapshot idea yourself.

Requirements:

  • Change only ContentView.swift.
  • When the last roll was exactly one D20 and its result was 20, show "Natural 20!" (green). If it was 1, show "Natural 1" (red).
  • Show nothing for any other roll: two or more dice, a different die type, or a D20 showing 2 to 19.
  • The message must describe the roll that happened, not the current die selection.

Acceptance criteria:

  • The project builds with no errors or warnings.
  • Rolling 1D20 repeatedly eventually shows each message (on average once every 20 rolls each).
  • Rolling 1D4 that shows 1 shows no message.
  • After a "Natural 20!" roll, selecting D6 without rolling leaves the message on screen.
  • After a 1D12 roll that shows 1, selecting D20 without rolling does not make "Natural 1" appear.

Hint 1

You need to know which die was rolled. Is that stored anywhere right now? selectedDie describes the next roll, not the last one. Look at how the chapter solved the same problem for the modifier.

Hint 2

Add another snapshot property next to rolledModifier, holding a DieType, and assign it in performRoll() at the same time as the other snapshot values. Then the condition you need has two parts that must both be true: the roll contained exactly one value, and the rolled die was .d20.

Hint 3

The structure is an outer if with &&, and inside it an if / else if that checks results[0]. Because the outer condition already checked the count, it is safe to read index 0 inside. Use .foregroundStyle(.green) and .foregroundStyle(.red) for the colors.

Practice Exercise A — Predict the output

What does this code print? Work it out on paper first, then run it in a #Playground block (or the swift REPL) to check.

Example — not part of the Dice Foundry project
let values = [-25, -1, 0, 1, 20, 21]
for value in values {
    var label = ""
    switch value {
    case ..<(-20):
        label = "too low"
    case -20...(-1):
        label = "penalty"
    case 0:
        label = "none"
    case 1...20:
        label = "bonus"
    default:
        label = "too high"
    }
    print(value, label)
}

Practice Exercise B — Spot the bug

A teammate wrote this notation function. It compiles. Find every input for which it produces the wrong text, explain why, and fix it.

Broken code — for diagnosis practice
func notation(quantity: Int, dieType: DieType, modifier: Int) -> String {
    let dice = "\(quantity)\(dieType.name)"
    if modifier >= 0 {
        return "\(dice) + \(modifier)"
    } else {
        return "\(dice) - \(modifier)"
    }
}

Practice Exercise C — Rewrite with if

Rewrite formattedModifier using if / else if / else instead of switch, producing identical output. Then rewrite notation's sign logic as a switch on modifier. Which version of each do you find easier to read, and why?

Practice Exercise D — Short-circuit thinking

For each expression, say whether it can crash when results is an empty array, and why.

  1. !results.isEmpty && results[0] == 20
  2. results[0] == 20 && !results.isEmpty
  3. results.isEmpty || results[0] > 3
  4. results.count == 1 && results[0] == 1