Mercurial > public > geoquiz
changeset 28:f51b70c2cccc
randomize country selection
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Thu, 10 Nov 2022 10:12:58 +0100 |
parents | 3f4b366d476d |
children | f5a2c2dab208 |
files | GeoQuiz/Models/Controllers/CountryGameController.swift |
diffstat | 1 files changed, 15 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/GeoQuiz/Models/Controllers/CountryGameController.swift Thu Nov 10 09:26:48 2022 +0100 +++ b/GeoQuiz/Models/Controllers/CountryGameController.swift Thu Nov 10 10:12:58 2022 +0100 @@ -76,16 +76,23 @@ } } - // Get question asked (correct answer) - let correctAnswer = data.first(where: { - !userChoices.keys.contains($0.key) && // Avoid duplicated countries - !dataAsked.keys.contains($0.key) // Avoid countries already asked + // Get correct answer + let randomCountryKeys = data.keys.shuffled() + + let correctCountryKey = randomCountryKeys.first(where: { + !userChoices.keys.contains($0) && + !dataAsked.keys.contains($0) + }) - // Unwrap optional - if let correctAnswer = correctAnswer { - userChoices[correctAnswer.key] = correctAnswer.value - dataAsked[correctAnswer.key] = correctAnswer.value + // Unwrap correct answer + if let correctCountryKey = correctCountryKey { + let correctCountryValue = data[correctCountryKey]! + + userChoices[correctCountryKey] = correctCountryValue + dataAsked[correctCountryKey] = correctCountryValue + + let correctAnswer = (key: correctCountryKey, value: correctCountryValue) self.correctAnswer = correctAnswer } else { fatalError("Couldn't unwrap optional value")