DayByDay/Quiz/DressUp.swift
84 lines · 3131 bytes
1//
2// DressUp.swift
3// DayByDay
4//
5
6/// Things to wear, for "Dress for the weather".
7nonisolated enum Clothing: Int, CaseIterable {
8 case sunglasses, sunHat, tshirt, umbrella, coat, jacket
9
10 var name: String {
11 switch self {
12 case .sunglasses: "Sunglasses"
13 case .sunHat: "Sun hat"
14 case .tshirt: "T-shirt"
15 case .umbrella: "Umbrella"
16 case .coat: "Coat"
17 case .jacket: "Jacket"
18 }
19 }
20
21 var symbol: String {
22 switch self {
23 case .sunglasses: "sunglasses.fill"
24 case .sunHat: "hat.widebrim.fill"
25 case .tshirt: "tshirt.fill"
26 case .umbrella: "umbrella.fill"
27 case .coat: "coat.fill"
28 case .jacket: "jacket.fill"
29 }
30 }
31
32 var suits: Set<LearnWeather> {
33 switch self {
34 case .sunglasses, .sunHat, .tshirt: [.sunny]
35 case .umbrella: [.rainy]
36 // Coat and jacket look alike, so they suit the same weather; both
37 // are fine in the rain, so neither is a wrong answer there.
38 case .coat, .jacket: [.rainy, .snowy, .windy]
39 }
40 }
41
42 var fact: String {
43 switch self {
44 case .sunglasses: "Sunglasses are for sunny days."
45 case .sunHat: "A sun hat is for sunny days."
46 case .tshirt: "A T-shirt is for warm days."
47 case .umbrella: "An umbrella is for rainy days."
48 case .coat: "A coat is for cold days."
49 case .jacket: "A jacket is for cold, windy days."
50 }
51 }
52
53 /// Recorded under the weather topic with a "clothing." id.
54 var item: QuizItem {
55 QuizItem(id: "clothing.\(self)", name: name, category: .weather)
56 }
57
58 static func from(_ item: QuizItem) -> Clothing? {
59 allCases.first { $0.item.id == item.id }
60 }
61}
62
63/// "It's rainy! What should we wear?" Exactly one choice suits the weather.
64nonisolated struct DressUp: QuestionSource {
65 static let weathers: [LearnWeather] = [.sunny, .rainy, .snowy, .windy]
66
67 func next(correctCount: Int, excluding previous: QuizItem?, using rng: inout some RandomNumberGenerator) -> Question {
68 let previousClothing = previous.flatMap(Clothing.from)
69 func fresh(_ w: LearnWeather) -> [Clothing] {
70 Clothing.allCases.filter { $0.suits.contains(w) && $0 != previousClothing }
71 }
72 let weather = Self.weathers.filter { !fresh($0).isEmpty }.randomElement(using: &rng)!
73 let answer = fresh(weather).randomElement(using: &rng)!
74 let count = Round.choiceCount(correctCount: correctCount)
75 let others = Clothing.allCases.filter { !$0.suits.contains(weather) }
76 .shuffled(using: &rng).prefix(count - 1)
77 var facts: [QuizItem: String] = [:]
78 for c in others { facts[c.item] = c.fact }
79 return Question(prompt: "It's \(weather.name.lowercased())! What should we wear?",
80 choices: ([answer] + others).map(\.item).shuffled(using: &rng),
81 answer: answer.item, facts: facts,
82 shown: [Category.weather.item(weather)])
83 }
84}