DayByDay/Quiz/Game.swift
200 lines · 8114 bytes
5 symbols in this file
1//
2// Game.swift
3// DayByDay
4//
5
6nonisolated enum Game: String, CaseIterable {
7 case findIt, oddOneOut, listen
8 case comesNext, weekTrain, yesterdayTomorrow, yearWheel, whichSeason, counting, most
9 case mixColors, trace, dressUp, sortFood, moves
10
11 var title: String {
12 switch self {
13 case .findIt: "Find it!"
14 case .oddOneOut: "Odd one out"
15 case .listen: "Listen"
16 case .comesNext: "What comes next?"
17 case .weekTrain: "Week train"
18 case .yesterdayTomorrow: "Yesterday and tomorrow"
19 case .yearWheel: "Year wheel"
20 case .whichSeason: "Which season?"
21 case .counting: "Count with me"
22 case .most: "Which has the most?"
23 case .mixColors: "Mix colors"
24 case .trace: "Trace it"
25 case .dressUp: "Dress for the weather"
26 case .sortFood: "Fruit or vegetable?"
27 case .moves: "Move your body"
28 }
29 }
30
31 var symbol: String {
32 switch self {
33 case .findIt: "magnifyingglass"
34 case .oddOneOut: "square.grid.2x2.fill"
35 case .listen: "ear.fill"
36 case .comesNext: "arrow.right.circle.fill"
37 case .weekTrain: "train.side.front.car"
38 case .yesterdayTomorrow: "calendar.day.timeline.left"
39 case .yearWheel: "circle.dashed"
40 case .whichSeason: "leaf.fill"
41 case .counting: "hand.tap.fill"
42 case .most: "chart.bar.fill"
43 case .mixColors: "paintbrush.fill"
44 case .trace: "hand.draw.fill"
45 case .dressUp: "tshirt.fill"
46 case .sortFood: "basket.fill"
47 case .moves: "figure.wave"
48 }
49 }
50
51 /// Played as a `Round` of questions; the rest have their own screens.
52 var usesRound: Bool {
53 switch self {
54 case .findIt, .oddOneOut, .listen, .comesNext, .whichSeason, .most, .mixColors, .dressUp: true
55 case .weekTrain, .yesterdayTomorrow, .yearWheel, .counting, .trace, .sortFood, .moves: false
56 }
57 }
58}
59
60nonisolated extension LearnAnimal {
61 var sound: SoundEffect? {
62 switch self {
63 case .cat: .meow
64 case .dog: .woof
65 case .bird: .tweet
66 case .cow: .moo
67 case .duck: .quack
68 case .sheep: .baa
69 case .pig: .oink
70 default: nil
71 }
72 }
73}
74
75nonisolated extension LearnWeather {
76 var sound: SoundEffect? {
77 switch self {
78 case .rainy: .rain
79 case .windy: .wind
80 case .stormy: .thunder
81 default: nil
82 }
83 }
84}
85
86nonisolated extension Category {
87 /// Games offered in this topic's hub, in order.
88 var games: [Game] {
89 var games: [Game] = [.findIt]
90 if [.shapes, .animals, .food].contains(self) { games.append(.oddOneOut) }
91 if [.numbers, .animals, .alphabet, .weather].contains(self) { games.append(.listen) }
92 switch self {
93 case .days: games += [.comesNext, .weekTrain, .yesterdayTomorrow]
94 case .months: games += [.comesNext, .yearWheel]
95 case .seasons: games.append(.whichSeason)
96 case .numbers: games += [.counting, .most]
97 case .colors: games.append(.mixColors)
98 case .shapes, .alphabet: games.append(.trace)
99 case .weather: games.append(.dressUp)
100 case .food: games.append(.sortFood)
101 case .bodyParts: games.append(.moves)
102 default: break
103 }
104 return games
105 }
106
107 /// `weights` favor answers the child has been missing; only the pick-one
108 /// games use them.
109 func questionSource(for game: Game, weights: [QuizItem: Double] = [:]) -> any QuestionSource {
110 switch game {
111 case .findIt: with(findIt, weights)
112 case .oddOneOut: with(oddOneOut, weights)
113 case .listen: with(listen, weights)
114 case .comesNext: ComesNext(items: quizItems, noun: self == .days ? "day" : "month")
115 case .whichSeason: WhichSeason()
116 case .most: Most()
117 case .mixColors: MixColors()
118 case .dressUp: DressUp()
119 case .weekTrain, .yesterdayTomorrow, .yearWheel, .counting, .trace, .sortFood, .moves:
120 preconditionFailure("\(game) is not a question round")
121 }
122 }
123
124 private var findIt: QuestionPicker {
125 let article = switch self {
126 case .alphabet: "the letter "
127 case .numbers: "the number "
128 case .animals, .food, .shapes, .bodyParts: "the "
129 default: ""
130 }
131 // Common nouns after an article are lower case ("the cow"); names stay as they are.
132 return QuestionPicker(answers: quizItems) {
133 "Can you find \(article)\(article.isEmpty ? $0.name : $0.name.lowercased())?"
134 }
135 }
136
137 private var oddOneOut: OddOneOut {
138 switch self {
139 case .food:
140 OddOneOut(group(LearnFood.allCases.filter(\.isFruit), "Which one is a fruit?") { SortRound.fact(for: $0.value(LearnFood.self)!) },
141 group(LearnFood.allCases.filter { !$0.isFruit }, "Which one is a vegetable?") { SortRound.fact(for: $0.value(LearnFood.self)!) })
142 case .animals:
143 OddOneOut(group(LearnAnimal.allCases.filter(\.canFly), "Which one can fly?") { "\($0.name) can fly." },
144 group(LearnAnimal.allCases.filter { !$0.canFly }, "Which one can't fly?") { "\($0.name) can't fly." })
145 case .shapes:
146 OddOneOut(group(LearnShape.allCases.filter { $0.hasCorners == false }, "Which one is round?") { "\($0.name) is round." },
147 group(LearnShape.allCases.filter { $0.hasCorners == true }, "Which one has corners?") { "\($0.name) has corners." })
148 default:
149 preconditionFailure("No odd one out for \(self)")
150 }
151 }
152
153 private var listen: QuestionPicker {
154 switch self {
155 case .animals:
156 let voiced = items(LearnAnimal.allCases.filter { $0.sound != nil })
157 return QuestionPicker(answers: voiced, prompt: { _ in "Who says this?" },
158 sound: { $0.value(LearnAnimal.self)?.sound })
159 case .weather:
160 return QuestionPicker(answers: items(LearnWeather.allCases.filter { $0.sound != nil }),
161 // Stormy only as an answer: a storm is also rain and wind.
162 distractors: items(LearnWeather.allCases.filter { $0 != .stormy }),
163 prompt: { _ in "What weather sounds like this?" },
164 sound: { $0.value(LearnWeather.self)?.sound })
165 case .numbers:
166 return QuestionPicker(answers: items(LearnNumber.allCases.filter { $0.numeral <= 5 }),
167 prompt: { _ in "How many beeps?" },
168 sound: { $0.value(LearnNumber.self).map { .beeps($0.numeral) } })
169 case .alphabet:
170 // K and Q share C's sound in their example words; X has no starting sound.
171 let letters = items(LearnLetter.allCases.filter { ![.k, .q, .x].contains($0) })
172 return QuestionPicker(answers: letters) {
173 "Which letter does \($0.value(LearnLetter.self)!.exampleWord) start with?"
174 }
175 default:
176 preconditionFailure("No listening game for \(self)")
177 }
178 }
179
180 private func with(_ picker: QuestionPicker, _ weights: [QuizItem: Double]) -> QuestionPicker {
181 var picker = picker
182 picker.weights = weights
183 return picker
184 }
185
186 private func with(_ source: OddOneOut, _ weights: [QuizItem: Double]) -> OddOneOut {
187 var source = source
188 source.weights = weights
189 return source
190 }
191
192 private func items<T: CaseIterable>(_ values: [T]) -> [QuizItem] {
193 values.map { item($0) }
194 }
195
196 private func group<T: CaseIterable>(_ values: [T], _ question: String,
197 fact: @escaping (QuizItem) -> String) -> OddOneOut.Group {
198 OddOneOut.Group(members: items(values), question: question, fact: fact)
199 }
200}