DayByDay/Quiz/OrderSources.swift
79 lines · 3742 bytes
1//
2// OrderSources.swift
3// DayByDay
4//
5
6nonisolated extension MonthOfYear {
7 /// Same months as `Season.current`.
8 var season: Season {
9 switch self {
10 case .march, .april, .may: .spring
11 case .june, .july, .august: .summer
12 case .september, .october, .november: .fall
13 case .december, .january, .february: .winter
14 }
15 }
16}
17
18/// "What day comes after Monday?" The answer is the next item, wrapping
19/// from the last to the first. The asked item is never a choice.
20nonisolated struct ComesNext: QuestionSource {
21 let items: [QuizItem]
22 let noun: String
23
24 init(items: [QuizItem], noun: String) {
25 precondition(items.count >= 3)
26 self.items = items
27 self.noun = noun
28 }
29
30 func next(correctCount: Int, excluding previous: QuizItem?, using rng: inout some RandomNumberGenerator) -> Question {
31 let starts = items.indices.filter { items[($0 + 1) % items.count] != previous }
32 let i = starts.randomElement(using: &rng)!
33 let asked = items[i]
34 let answer = items[(i + 1) % items.count]
35 let count = min(Round.choiceCount(correctCount: correctCount), items.count - 1)
36 let others = items.filter { $0 != asked && $0 != answer }.shuffled(using: &rng).prefix(count - 1)
37 var facts: [QuizItem: String] = [:]
38 for item in others { facts[item] = "\(answer.name) comes after \(asked.name)." }
39 return Question(prompt: "What \(noun) comes after \(asked.name)?",
40 choices: ([answer] + others).shuffled(using: &rng),
41 answer: answer, facts: facts)
42 }
43}
44
45/// "Which season is July in?"
46nonisolated struct WhichSeason: QuestionSource {
47 func next(correctCount: Int, excluding previous: QuizItem?, using rng: inout some RandomNumberGenerator) -> Question {
48 let seasons = Category.seasons.quizItems
49 let answer = seasons.filter { $0 != previous }.randomElement(using: &rng)!
50 let season = answer.value(Season.self)!
51 let month = MonthOfYear.allCases.filter { $0.season == season }.randomElement(using: &rng)!
52 let count = Round.choiceCount(correctCount: correctCount)
53 let others = seasons.filter { $0 != answer }.shuffled(using: &rng).prefix(count - 1)
54 var facts: [QuizItem: String] = [:]
55 for item in others { facts[item] = "\(month.name) is in \(answer.name)." }
56 return Question(prompt: "Which season is \(month.name) in?",
57 choices: ([answer] + others).shuffled(using: &rng),
58 answer: answer, facts: facts)
59 }
60}
61
62/// "Which one has the most?" Choices are piles of 1 to 10; the answer is the largest.
63nonisolated struct Most: QuestionSource {
64 func next(correctCount: Int, excluding previous: QuizItem?, using rng: inout some RandomNumberGenerator) -> Question {
65 let numbers = Category.numbers.quizItems
66 let count = Round.choiceCount(correctCount: correctCount)
67 // The answer needs count - 1 smaller numbers below it.
68 let answer = numbers.filter { $0.value(LearnNumber.self)!.numeral >= count && $0 != previous }
69 .randomElement(using: &rng)!
70 let top = answer.value(LearnNumber.self)!.numeral
71 let others = numbers.filter { $0.value(LearnNumber.self)!.numeral < top }
72 .shuffled(using: &rng).prefix(count - 1)
73 var facts: [QuizItem: String] = [:]
74 for item in others { facts[item] = "That one has \(item.value(LearnNumber.self)!.numeral)." }
75 return Question(prompt: "Which one has the most?",
76 choices: ([answer] + others).shuffled(using: &rng),
77 answer: answer, facts: facts)
78 }
79}