krz/daybyday

iOS learning games for young children: days, months, seasons, numbers, letters, animals and more. education ios kids swift https://krz.sh/apps/daybyday/

DayByDay/Quiz/MixColors.swift

062549ad26607ce9e973ed2a9a7ef6f853885f83
daybyday/DayByDay/Quiz/MixColors.swift history · blame · raw

27 lines · 1423 bytes

1 symbol in this file
  • next function, line 12
 1//
 2//  MixColors.swift
 3//  DayByDay
 4//
 5
 6/// "What do red and yellow make?" The two paints are shown above the choices.
 7nonisolated struct MixColors: QuestionSource {
 8    static let mixes: [(LearnColor, LearnColor, LearnColor)] = [
 9        (.red, .yellow, .orange), (.blue, .yellow, .green), (.red, .blue, .purple), (.red, .white, .pink),
10    ]
11
12    func next(correctCount: Int, excluding previous: QuizItem?, using rng: inout some RandomNumberGenerator) -> Question {
13        let colors = Category.colors
14        let (a, b, result) = Self.mixes.filter { colors.item($0.2) != previous }.randomElement(using: &rng)!
15        let answer = colors.item(result)
16        let ingredients = [colors.item(a), colors.item(b)].shuffled(using: &rng)
17        let count = Round.choiceCount(correctCount: correctCount)
18        let others = colors.quizItems.filter { $0 != answer && !ingredients.contains($0) }
19            .shuffled(using: &rng).prefix(count - 1)
20        let (first, second) = (ingredients[0].name, ingredients[1].name.lowercased())
21        var facts: [QuizItem: String] = [:]
22        for item in others { facts[item] = "\(first) and \(second) make \(answer.name.lowercased())." }
23        return Question(prompt: "What do \(first.lowercased()) and \(second) make?",
24                        choices: ([answer] + others).shuffled(using: &rng),
25                        answer: answer, facts: facts, shown: ingredients)
26    }
27}