Mercurial > public > geoquiz
view GeoQuiz/Logic/CountryGame.swift @ 8:e09959b4e4a8
fix bugs
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Thu, 06 Oct 2022 11:14:34 +0200 |
parents | d945e52b0704 |
children | 3540c7efc216 |
line wrap: on
line source
// // CountryGame.swift // GeoQuiz // // Created by Dennis Concepción Martín on 20/9/22. // import Foundation import AVFAudio class CountryGame: Game, ObservableObject { // Define type of generics typealias T = CountryModel.CountryData var data: [String: T] var dataAsked = [String: T]() // Data @Published var correctAnswer = ( key: String(), value: T(flag: String(), currency: String(), population: Int(), capital: String()) ) // User @Published var userChoices = [String: T]() @Published var userScore = 0 @Published var userLives = 3 @Published var correctAnswers = [String: T]() @Published var wrongAnswers = [String: T]() // Alerts @Published var alertTitle = String() @Published var alertMessage = String() @Published var showingGameOverAlert = false @Published var showingEndGameAlert = false @Published var showingWrongAnswerAlert = false @Published var showingExitGameAlert = false // Animations @Published var scoreScaleAmount = 1.0 @Published var livesScaleAmount = 1.0 // Sound effects @Published var player: AVAudioPlayer? init() { let data: CountryModel = load("countries.json") self.data = data.countries askQuestion { selector() } } } extension CountryGame { 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 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 }) // Unwrap optional if let correctAnswer = correctAnswer { userChoices[correctAnswer.key] = correctAnswer.value dataAsked[correctAnswer.key] = correctAnswer.value self.correctAnswer = correctAnswer } else { fatalError("Couldn't unwrap optional value") } self.userChoices = userChoices } }