comparison GeoQuiz/Controllers/CountryGameController.swift @ 30:eb23effeede7

add DatasetView
author Dennis C. M. <dennis@denniscm.com>
date Thu, 10 Nov 2022 11:51:52 +0100
parents f5a2c2dab208
children 6d574bd1644f
comparison
equal deleted inserted replaced
29:f5a2c2dab208 30:eb23effeede7
43 // Sound effects 43 // Sound effects
44 @Published var player: AVAudioPlayer? 44 @Published var player: AVAudioPlayer?
45 45
46 init() { 46 init() {
47 let data: CountryModel = Bundle.main.decode("countries.json") 47 let data: CountryModel = Bundle.main.decode("countries.json")
48 self.data = data.countries 48 let shuffledCountries = data.countries.shuffled().prefix(100)
49
50 var countries = [String: T]()
51 for shuffledCountry in shuffledCountries {
52 countries[shuffledCountry.key] = shuffledCountry.value
53 }
54
55 self.data = countries
49 56
50 let user = UserController() 57 let user = UserController()
51 userLives = user.data.numberOfLives 58 userLives = user.data.numberOfLives
52 59
53 if let userData = UserDefaults.standard.data(forKey: "UserData") { 60 if let userData = UserDefaults.standard.data(forKey: "UserData") {
78 85
79 // Get correct answer 86 // Get correct answer
80 let randomCountryKeys = data.keys.shuffled() 87 let randomCountryKeys = data.keys.shuffled()
81 88
82 let correctCountryKey = randomCountryKeys.first(where: { 89 let correctCountryKey = randomCountryKeys.first(where: {
83 !userChoices.keys.contains($0) && 90 !userChoices.keys.contains($0) && // Avoid duplicated countries
84 !dataAsked.keys.contains($0) 91 !dataAsked.keys.contains($0) // Avoid countries already asked
85
86 }) 92 })
87 93
88 // Unwrap correct answer 94 // Unwrap correct answer
89 if let correctCountryKey = correctCountryKey { 95 if let correctCountryKey = correctCountryKey {
90 let correctCountryValue = data[correctCountryKey]! 96 let correctCountryValue = data[correctCountryKey]!