comparison GeoQuiz/Logic/GuessTheCapital.swift @ 0:413e2d21333e

first commit
author Dennis C. M. <dennis@denniscm.com>
date Tue, 20 Sep 2022 08:13:26 +0200
parents
children 4dbe0cd9dadc
comparison
equal deleted inserted replaced
-1:000000000000 0:413e2d21333e
1 //
2 // GuessTheCapital.swift
3 // GeoQuiz
4 //
5 // Created by Dennis Concepción Martín on 18/9/22.
6 //
7
8 import Foundation
9
10 class GuessTheCapital: Game, ObservableObject {
11
12 struct Country: Hashable {
13 let capitalName: String
14 let flagSymbol: String
15 }
16
17 let countries: [String: Country]
18 var countriesAsked = [String: Country]()
19
20 @Published var userScore = 0
21 @Published var userLives = 3
22 @Published var questionCounter = 0
23 @Published var alertTitle = ""
24 @Published var alertMessage = ""
25 @Published var showingBuyLivesView = false
26 @Published var showingNoLivesAlert = false
27 @Published var showingWrongAnswerAlert = false
28 @Published var showingEndGameAlert = false
29
30 @Published var userChoices = [String: Country]()
31 @Published var countryNameAsked = ""
32
33 init() {
34 let flags: CountryFlags = load("CountryFlags.json")
35 let capitals: CountryCapitals = load("CountryCapitals.json")
36
37 var countries = [String: Country]()
38
39 for country in capitals.countries {
40 let countryName = country.key
41 let capitalName = country.value
42
43 if let flagSymbol = flags.countries[countryName] {
44 countries[country.key] = Country(capitalName: capitalName, flagSymbol: flagSymbol)
45 } else {
46 fatalError()
47 }
48 }
49
50 self.countries = countries
51 askQuestion()
52 }
53
54 func askQuestion() {
55 guard questionCounter < countries.count else {
56 self.alertTitle = "Amazing!"
57 self.alertMessage = "You've completed the game."
58 self.showingEndGameAlert = true
59
60 return
61 }
62
63 var userChoices = [String: Country]()
64
65 while userChoices.count < 2 {
66 if let country = countries.randomElement() {
67 userChoices[country.key] = country.value
68 } else {
69 fatalError("Couldn't get a random country")
70 }
71 }
72
73 let countryAsked = countries.first(where: {
74 !userChoices.keys.contains($0.key) &&
75 !countriesAsked.keys.contains($0.key)
76 })
77
78 if let countryAsked = countryAsked {
79 userChoices[countryAsked.key] = countryAsked.value
80 self.countriesAsked[countryAsked.key] = countryAsked.value
81 self.countryNameAsked = countryAsked.key
82 } else {
83 fatalError("Couldn't get countryAsked")
84 }
85
86 self.userChoices = userChoices
87 self.questionCounter += 1
88 }
89
90 func answered(userChoice userCapitalNameGuess: String) {
91 guard let correctCountry = countries[countryNameAsked] else {
92 fatalError("Couln't find \(countryNameAsked) in countries dictionary")
93 }
94
95 guard userLives > 0 else {
96 self.alertTitle = "Not enough lives!"
97 self.alertMessage = "Please buy more lives to keep playing"
98 self.showingNoLivesAlert = true
99
100 return
101 }
102
103 if correctCountry.capitalName == userCapitalNameGuess {
104 hapticSuccess()
105 self.userScore += 1
106 askQuestion()
107 } else {
108 hapticError()
109 self.userLives -= 1
110 self.alertTitle = "Wrong!"
111 self.alertMessage = "The capital of \(countryNameAsked) is \(correctCountry.capitalName). You have \(userLives) lives left"
112 self.showingWrongAnswerAlert = true
113 }
114 }
115 }