Mercurial > public > geoquiz
comparison GeoQuiz/Logic/GuessTheFlag.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 |
---|---|
4 // | 4 // |
5 // Created by Dennis Concepción Martín on 20/9/22. | 5 // Created by Dennis Concepción Martín on 20/9/22. |
6 // | 6 // |
7 | 7 |
8 import Foundation | 8 import Foundation |
9 import SwiftUI | |
9 | 10 |
10 class GuessTheFlag: Game, ObservableObject { | 11 class GuessTheFlag: Game, ObservableObject { |
11 let countries: [String: String] | 12 |
12 var countriesAsked = [String: String]() | 13 // Define type of generics |
14 var data: [String: String] | |
15 var dataAsked = [String]() | |
13 | 16 |
17 // Data | |
18 @Published var correctAnswer = (key: String(), value: String()) | |
19 | |
20 // User | |
21 @Published var userChoices = [String: String]() | |
14 @Published var userScore = 0 | 22 @Published var userScore = 0 |
15 @Published var userLives = 3 | 23 @Published var userLives = 3 |
16 @Published var questionCounter = 0 | 24 |
17 @Published var alertTitle = "" | 25 // Alerts |
18 @Published var alertMessage = "" | 26 @Published var alertTitle = String() |
27 @Published var alertMessage = String() | |
28 @Published var showingNoLivesAlert = false | |
29 @Published var showingEndGameAlert = false | |
30 @Published var showingWrongAnswerAlert = false | |
31 | |
32 // Animations | |
33 @Published var scoreScaleAmount = 1.0 | |
34 @Published var livesScaleAmount = 1.0 | |
35 | |
36 // Modal views | |
19 @Published var showingBuyLivesView = false | 37 @Published var showingBuyLivesView = false |
20 @Published var showingNoLivesAlert = false | |
21 @Published var showingWrongAnswerAlert = false | |
22 @Published var showingEndGameAlert = false | |
23 | |
24 @Published var userChoices = [String: String]() | |
25 @Published var countryNameAsked = "" | |
26 | 38 |
27 init() { | 39 init() { |
28 let flags: CountryFlags = load("CountryFlags.json") | 40 let flags: CountryFlags = load("CountryFlags.json") |
29 self.countries = flags.countries | 41 data = flags.countries |
30 askQuestion() | 42 askQuestion() |
31 } | 43 } |
32 | |
33 func askQuestion() { | |
34 guard questionCounter < countries.count else { | |
35 self.alertTitle = "Amazing!" | |
36 self.alertMessage = "You've completed the game." | |
37 self.showingEndGameAlert = true | |
38 | |
39 return | |
40 } | |
41 | |
42 var userChoices = [String: String]() | |
43 | |
44 while userChoices.count < 2 { | |
45 if let country = countries.randomElement() { | |
46 userChoices[country.key] = country.value | |
47 } else { | |
48 fatalError("Couldn't get a random country") | |
49 } | |
50 } | |
51 | |
52 let countryAsked = countries.first(where: { | |
53 !userChoices.keys.contains($0.key) && | |
54 !countriesAsked.keys.contains($0.key) | |
55 }) | |
56 | |
57 if let countryAsked = countryAsked { | |
58 userChoices[countryAsked.key] = countryAsked.value | |
59 self.countriesAsked[countryAsked.key] = countryAsked.value | |
60 self.countryNameAsked = countryAsked.key | |
61 } else { | |
62 fatalError("Couldn't get countryAsked") | |
63 } | |
64 | |
65 self.userChoices = userChoices | |
66 self.questionCounter += 1 | |
67 } | |
68 | |
69 func answered(userChoice userFlagSymbolGuess: String) { | |
70 guard let correctFlagSymbolAnswer = countries[countryNameAsked] else { | |
71 fatalError("Couln't find \(countryNameAsked) in countries dictionary") | |
72 } | |
73 | |
74 guard userLives > 0 else { | |
75 self.alertTitle = "Not enough lives!" | |
76 self.alertMessage = "Please buy more lives to keep playing" | |
77 self.showingNoLivesAlert = true | |
78 | |
79 return | |
80 } | |
81 | |
82 if correctFlagSymbolAnswer == userFlagSymbolGuess { | |
83 hapticSuccess() | |
84 self.userScore += 1 | |
85 askQuestion() | |
86 } else { | |
87 hapticError() | |
88 self.userLives -= 1 | |
89 self.alertTitle = "Wrong!" | |
90 self.alertMessage = "That's not the flag of \(countryNameAsked). You have \(userLives) lives left" | |
91 self.showingWrongAnswerAlert = true | |
92 } | |
93 } | |
94 } | 44 } |