# HG changeset patch # User Dennis C. M. # Date 1668071578 -3600 # Node ID f51b70c2cccc44cbb2a61687ccddbb9cdb915cd9 # Parent 3f4b366d476d7d79e18b66e105777aaea6c27f10 randomize country selection diff -r 3f4b366d476d -r f51b70c2cccc GeoQuiz/Models/Controllers/CountryGameController.swift --- 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")