Mercurial > public > geoquiz
comparison GeoQuiz/Models/Controllers/CountryGameController.swift @ 28:f51b70c2cccc
randomize country selection
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Thu, 10 Nov 2022 10:12:58 +0100 |
parents | 425078c01194 |
children |
comparison
equal
deleted
inserted
replaced
27:3f4b366d476d | 28:f51b70c2cccc |
---|---|
74 } else { | 74 } else { |
75 fatalError("Couldn't get a random value from data") | 75 fatalError("Couldn't get a random value from data") |
76 } | 76 } |
77 } | 77 } |
78 | 78 |
79 // Get question asked (correct answer) | 79 // Get correct answer |
80 let correctAnswer = data.first(where: { | 80 let randomCountryKeys = data.keys.shuffled() |
81 !userChoices.keys.contains($0.key) && // Avoid duplicated countries | 81 |
82 !dataAsked.keys.contains($0.key) // Avoid countries already asked | 82 let correctCountryKey = randomCountryKeys.first(where: { |
83 !userChoices.keys.contains($0) && | |
84 !dataAsked.keys.contains($0) | |
85 | |
83 }) | 86 }) |
84 | 87 |
85 // Unwrap optional | 88 // Unwrap correct answer |
86 if let correctAnswer = correctAnswer { | 89 if let correctCountryKey = correctCountryKey { |
87 userChoices[correctAnswer.key] = correctAnswer.value | 90 let correctCountryValue = data[correctCountryKey]! |
88 dataAsked[correctAnswer.key] = correctAnswer.value | 91 |
92 userChoices[correctCountryKey] = correctCountryValue | |
93 dataAsked[correctCountryKey] = correctCountryValue | |
94 | |
95 let correctAnswer = (key: correctCountryKey, value: correctCountryValue) | |
89 self.correctAnswer = correctAnswer | 96 self.correctAnswer = correctAnswer |
90 } else { | 97 } else { |
91 fatalError("Couldn't unwrap optional value") | 98 fatalError("Couldn't unwrap optional value") |
92 } | 99 } |
93 | 100 |