DayByDay/Quiz/SequencePuzzle.swift
78 lines · 3339 bytes
7 symbols in this file
1//
2// SequencePuzzle.swift
3// DayByDay
4//
5
6/// Cards placed one at a time into ordered slots. Tapping or dragging a card
7/// places it in the next slot; a wrong card waits for `resume()`.
8nonisolated struct SequencePuzzle {
9 struct Step {
10 let answer: QuizItem
11 let prompt: String
12 /// Said after a wrong card, before "Try again!".
13 let correction: String
14 }
15
16 let steps: [Step]
17 /// The cards on offer, shuffled once.
18 let pool: [QuizItem]
19 /// Labels above the slots; nil for the train.
20 let slotLabels: [String]?
21 /// Said once every slot is filled.
22 let summary: String
23 private(set) var placed: [QuizItem] = []
24 private(set) var isShowingCorrection = false
25
26 var current: Step? { placed.count < steps.count ? steps[placed.count] : nil }
27 var isSolved: Bool { current == nil }
28
29 mutating func place(_ item: QuizItem) -> AnswerOutcome {
30 guard let current, !isShowingCorrection, !placed.contains(item) else { return .ignored }
31 guard item == current.answer else {
32 isShowingCorrection = true
33 return .incorrect(correctName: current.answer.name)
34 }
35 placed.append(item)
36 return .correct
37 }
38
39 mutating func resume() {
40 isShowingCorrection = false
41 }
42
43 func correction() -> String {
44 guard let current else { return "" }
45 return "\(current.correction) Try again!"
46 }
47
48 /// Sunday to Saturday, from all seven cards.
49 static func weekTrain(using rng: inout some RandomNumberGenerator) -> SequencePuzzle {
50 let days = DayOfWeek.allCases.map { Category.days.item($0) }
51 let steps = days.indices.map { i in
52 i == 0
53 ? Step(answer: days[0], prompt: "Which day comes first?", correction: "\(days[0].name) comes first.")
54 : Step(answer: days[i], prompt: "What comes after \(days[i - 1].name)?",
55 correction: "\(days[i].name) comes next.")
56 }
57 return SequencePuzzle(steps: steps, pool: days.shuffled(using: &rng), slotLabels: nil,
58 summary: days.map(\.name).joined(separator: ", ") + "!")
59 }
60
61 /// Yesterday, today and tomorrow, from those three cards plus one other day.
62 static func yesterdayTomorrow(today: DayOfWeek, using rng: inout some RandomNumberGenerator) -> SequencePuzzle {
63 func day(_ offset: Int) -> QuizItem {
64 Category.days.item(DayOfWeek(rawValue: (today.rawValue + offset + 7) % 7)!)
65 }
66 let (yesterday, now, tomorrow) = (day(-1), day(0), day(1))
67 let extra = (2...5).map(day).randomElement(using: &rng)!
68 let steps = [
69 Step(answer: yesterday, prompt: "Which day was yesterday?", correction: "Yesterday was \(yesterday.name)."),
70 Step(answer: now, prompt: "Which day is today?", correction: "Today is \(now.name)."),
71 Step(answer: tomorrow, prompt: "Which day will it be tomorrow?", correction: "Tomorrow will be \(tomorrow.name)."),
72 ]
73 return SequencePuzzle(
74 steps: steps, pool: [yesterday, now, tomorrow, extra].shuffled(using: &rng),
75 slotLabels: ["Yesterday", "Today", "Tomorrow"],
76 summary: "Yesterday was \(yesterday.name), today is \(now.name), and tomorrow will be \(tomorrow.name)!")
77 }
78}