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