0
|
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
|
3
|
10 class GuessTheCapital: ObservableObject {
|
0
|
11
|
3
|
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 scoreScaleAmount = 1.0
|
|
26 // @Published var livesScaleAmount = 1.0
|
|
27 // @Published var showingBuyLivesView = false
|
|
28 // @Published var showingNoLivesAlert = false
|
|
29 // @Published var showingWrongAnswerAlert = false
|
|
30 // @Published var showingEndGameAlert = false
|
|
31 //
|
|
32 // @Published var userChoices = [String: Country]()
|
|
33 // @Published var countryNameAsked = ""
|
|
34 //
|
|
35 // init() {
|
|
36 // let flags: CountryFlags = load("CountryFlags.json")
|
|
37 // let capitals: CountryCapitals = load("CountryCapitals.json")
|
|
38 //
|
|
39 // var countries = [String: Country]()
|
|
40 //
|
|
41 // for country in capitals.countries {
|
|
42 // let countryName = country.key
|
|
43 // let capitalName = country.value
|
|
44 //
|
|
45 // if let flagSymbol = flags.countries[countryName] {
|
|
46 // countries[country.key] = Country(capitalName: capitalName, flagSymbol: flagSymbol)
|
|
47 // } else {
|
|
48 // fatalError()
|
|
49 // }
|
|
50 // }
|
|
51 //
|
|
52 // self.countries = countries
|
|
53 // askQuestion()
|
|
54 // }
|
|
55 //
|
|
56 // func askQuestion() {
|
|
57 // guard questionCounter < countries.count else {
|
|
58 // self.alertTitle = "Amazing!"
|
|
59 // self.alertMessage = "You've completed the game."
|
|
60 // self.showingEndGameAlert = true
|
|
61 //
|
|
62 // return
|
|
63 // }
|
|
64 //
|
|
65 // var userChoices = [String: Country]()
|
|
66 //
|
|
67 // while userChoices.count < 2 {
|
|
68 // if let country = countries.randomElement() {
|
|
69 // userChoices[country.key] = country.value
|
|
70 // } else {
|
|
71 // fatalError("Couldn't get a random country")
|
|
72 // }
|
|
73 // }
|
|
74 //
|
|
75 // let countryAsked = countries.first(where: {
|
|
76 // !userChoices.keys.contains($0.key) &&
|
|
77 // !countriesAsked.keys.contains($0.key)
|
|
78 // })
|
|
79 //
|
|
80 // if let countryAsked = countryAsked {
|
|
81 // userChoices[countryAsked.key] = countryAsked.value
|
|
82 // self.countriesAsked[countryAsked.key] = countryAsked.value
|
|
83 // self.countryNameAsked = countryAsked.key
|
|
84 // } else {
|
|
85 // fatalError("Couldn't get countryAsked")
|
|
86 // }
|
|
87 //
|
|
88 // self.userChoices = userChoices
|
|
89 // self.questionCounter += 1
|
|
90 // }
|
|
91 //
|
|
92 // func answered(userChoice userCapitalNameGuess: String) {
|
|
93 // guard let correctCountry = countries[countryNameAsked] else {
|
|
94 // fatalError("Couln't find \(countryNameAsked) in countries dictionary")
|
|
95 // }
|
|
96 //
|
|
97 // guard userLives > 0 else {
|
|
98 // self.alertTitle = "Not enough lives!"
|
|
99 // self.alertMessage = "Please buy more lives to keep playing"
|
|
100 // self.showingNoLivesAlert = true
|
|
101 //
|
|
102 // return
|
|
103 // }
|
|
104 //
|
|
105 // if correctCountry.capitalName == userCapitalNameGuess {
|
|
106 // hapticSuccess()
|
|
107 // self.userScore += 1
|
|
108 // askQuestion()
|
|
109 // } else {
|
|
110 // hapticError()
|
|
111 // self.userLives -= 1
|
|
112 // self.alertTitle = "Wrong!"
|
|
113 // self.alertMessage = "The capital of \(countryNameAsked) is \(correctCountry.capitalName). You have \(userLives) lives left"
|
|
114 // self.showingWrongAnswerAlert = true
|
|
115 // }
|
|
116 // }
|
0
|
117 }
|