Chapter 5 Exercises — Enumerations and Dice Selection
These exercises build on the Chapter 5 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-05
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 6:
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 05: Save practice work"
git status --short
git switch main
Solutions are in the Chapter 5 Answers document. Try each hint in order and only read the next one when you are stuck.
Challenge 1 — Show the average roll
Goal: show the player what a single roll of the selected die averages out to.
Requirements:
- Add a computed property to
DieTypethat returns the average result of one roll as aDouble. - Compute it from the die's number of sides. Do not type six averages by hand.
- Show it in
ContentViewbelow the "Rolling a ..." caption, in small secondary text, for example "Average roll: 3.5".
Acceptance criteria:
- The project builds with no errors or warnings.
- With D6 selected the text reads "Average roll: 3.5"; with D20 selected, "Average roll: 10.5"; with D4, "Average roll: 2.5".
- The text updates immediately when you tap a different die, without rolling.
DieType.swiftstill imports only Foundation.
Hint 1
For a fair die numbered 1 to N, the average of one roll is the average of the lowest and highest faces. Work it out on paper for a D6 first: lowest 1, highest 6.
Hint 2
The formula is (1 + sides) divided by 2. Remember from Chapter 2 that dividing two Int values gives an Int and throws away the fraction: 7 / 2 is 3, not 3.5. Convert to Double before dividing. The new property is a computed property, declared just like sides and name, but with type Double.
Hint 3
The property body is a single expression that converts sides + 1 to a Double and divides by 2. In ContentView, add a Text that interpolates selectedDie. followed by your property's name, then apply .font(.footnote) and .foregroundStyle(.secondary). Place it directly after the caption's .foregroundStyle(.secondary) line.
Challenge 2 — A bolder picker
Goal: make the selected die stand out more, and change the picker's shape.
Requirements:
- Lay out the six die buttons as three rows of two instead of two rows of three.
- The selected die's label is larger and heavier than the others.
- Keep the existing orange/gray tint.
- Use the ternary operator for the new styling.
Acceptance criteria:
- The project builds with no errors or warnings.
- Six equal-width buttons in three rows of two, in the order D4 D6 / D8 D10 / D12 D20.
- Only the selected die's label is larger and heavier; the change follows your taps.
- Rolling still uses the selected die.
Hint 1
Two separate changes are needed. The number of columns is decided entirely by one property you wrote in Step 10. The label's appearance is decided by modifiers on the Text inside the label: closure.
Hint 2
Remove one GridItem from columns. For the label, you already have one ternary that picks a Color. You can write the same kind of expression to pick a font (for example .title2 versus .headline) and another to pick a font weight (for example .heavy versus .regular). Each ternary must produce the same type in both branches.
Hint 3
Replace .font(.headline) with .font( followed by the same dieType == selectedDie ? ... : ... condition you used for the tint, choosing between two fonts. Then add a .fontWeight(...) line with the same condition choosing between two weights. Keep .frame(maxWidth: .infinity) as the last modifier on the label. Do not try to use a ternary to switch between .bordered and .borderedProminent: those are different types and will not compile.
Extra Practice
These have no hints. Answers are in the Chapter 5 Answers document.
Exercise A — Predict the output
What does this playground print? Write your answer before running it.
#Playground("Exercise A") {
let first = DieType.d12
let second = DieType(rawValue: 12)
let third = DieType(rawValue: 3) ?? .d4
print(first.name, first.sides)
print(second == first)
print(third.name)
print(DieType.allCases.first == .d4)
}
Exercise B — Spot the bug
A developer wanted to highlight the selected die, but every button stays gray no matter what you tap. Find the bug.
ForEach(DieType.allCases) { dieType in
Button {
let selectedDie = dieType
} label: {
Text(dieType.name)
.font(.headline)
.frame(maxWidth: .infinity)
}
.buttonStyle(.bordered)
.tint(dieType == selectedDie ? .orange : .gray)
}
Exercise C — Spot the bug
This enum does not compile. Find two problems.
enum DieType: Int, CaseIterable, Identifiable {
case d4 = 4
case d6 = 6
case 8 = 8
case d10 = 6
var sides: Int {
rawValue
}
}
Exercise D — Small modification
Change the "Rolling a D20" caption so it reads "Rolling a D20 (1 to 20)" for whatever die is selected. Use only properties DieType already has. Do it on your challenge branch.