Mercurial > public > geoquiz
diff GeoQuiz/Controllers/CountryGameController.swift @ 33:6d574bd1644f
refactor controllers
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Sat, 12 Nov 2022 11:18:30 +0100 |
parents | eb23effeede7 |
children | 6ec51a4ca897 |
line wrap: on
line diff
--- a/GeoQuiz/Controllers/CountryGameController.swift Fri Nov 11 09:05:41 2022 +0100 +++ b/GeoQuiz/Controllers/CountryGameController.swift Sat Nov 12 11:18:30 2022 +0100 @@ -16,7 +16,6 @@ var data: [String: T] var dataAsked = [String: T]() - // Data @Published var correctAnswer = ( key: String(), value: T(flag: String(), currency: String(), population: Int(), capital: String()) @@ -45,15 +44,17 @@ init() { let data: CountryModel = Bundle.main.decode("countries.json") - let shuffledCountries = data.countries.shuffled().prefix(100) + let shuffledCountries = data.countries.shuffled().prefix(5) + var countries = [String: T]() - var countries = [String: T]() for shuffledCountry in shuffledCountries { countries[shuffledCountry.key] = shuffledCountry.value } self.data = countries + print(countries) + let user = UserController() userLives = user.data.numberOfLives @@ -63,47 +64,6 @@ } } - askQuestion { - selector() - } + ask() } } - -extension CountryGameController { - func selector() { - - // Get random choices - var userChoices = [String: T]() - - while userChoices.count < 2 { - if let choice = data.randomElement() { - userChoices[choice.key] = choice.value - } else { - fatalError("Couldn't get a random value from data") - } - } - - // Get correct answer - let randomCountryKeys = data.keys.shuffled() - - let correctCountryKey = randomCountryKeys.first(where: { - !userChoices.keys.contains($0) && // Avoid duplicated countries - !dataAsked.keys.contains($0) // Avoid countries already asked - }) - - // 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") - } - - self.userChoices = userChoices - } -}