Mercurial > public > geoquiz
comparison GeoQuiz/Logic/Game.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 |
comparison
equal
deleted
inserted
replaced
7:d945e52b0704 | 8:e09959b4e4a8 |
---|---|
38 var scoreScaleAmount: Double { get set } | 38 var scoreScaleAmount: Double { get set } |
39 var livesScaleAmount: Double { get set } | 39 var livesScaleAmount: Double { get set } |
40 | 40 |
41 // Sound effects | 41 // Sound effects |
42 var player: AVAudioPlayer? { get set } | 42 var player: AVAudioPlayer? { get set } |
43 | |
44 func selector() | |
43 } | 45 } |
44 | 46 |
45 extension Game { | 47 extension Game { |
46 var questionCounter: Int { | 48 var questionCounter: Int { |
47 dataAsked.count | 49 dataAsked.count |
48 } | 50 } |
49 | 51 |
50 func askQuestion() { | 52 func askQuestion(selector: () -> Void) { |
51 guard questionCounter < data.count else { | 53 guard questionCounter < data.count else { |
52 alertTitle = "⭐️ Congratulations ⭐️" | 54 alertTitle = "⭐️ Congratulations ⭐️" |
53 alertMessage = "You completed the game." | 55 alertMessage = "You completed the game." |
54 showingEndGameAlert = true | 56 showingEndGameAlert = true |
55 | 57 |
56 return | 58 return |
57 } | 59 } |
58 | 60 |
59 // Get random choices | 61 selector() |
60 var userChoices = [String: T]() | |
61 | |
62 while userChoices.count < 2 { | |
63 if let choice = data.randomElement() { | |
64 userChoices[choice.key] = choice.value | |
65 } else { | |
66 fatalError("Couldn't get a random value from data") | |
67 } | |
68 } | |
69 | |
70 // Get question asked (correct answer) | |
71 let correctAnswer = data.first(where: { | |
72 !userChoices.keys.contains($0.key) && !dataAsked.keys.contains($0.key) | |
73 }) | |
74 | |
75 // Unwrap optional | |
76 if let correctAnswer = correctAnswer { | |
77 userChoices[correctAnswer.key] = correctAnswer.value | |
78 dataAsked[correctAnswer.key] = correctAnswer.value | |
79 self.correctAnswer = correctAnswer | |
80 } else { | |
81 fatalError("Couldn't unwrap optional value") | |
82 } | |
83 | |
84 self.userChoices = userChoices | |
85 } | 62 } |
86 | 63 |
87 func answer(_ choice: (key: String, value: T)) { | 64 func answer(_ choice: (key: String, value: T), selector: () -> Void) { |
88 if correctAnswer == choice { | 65 if correctAnswer == choice { |
89 hapticSuccess() | 66 hapticSuccess() |
90 playSound("correctAnswer") | 67 playSound("correctAnswer") |
91 | 68 |
92 withAnimation(.easeIn(duration: 0.5)) { | 69 withAnimation(.easeIn(duration: 0.5)) { |
93 scoreScaleAmount += 1 | 70 scoreScaleAmount += 1 |
94 userScore += 1 | 71 userScore += 1 |
95 } | 72 } |
96 | 73 |
97 correctAnswers[correctAnswer.key] = correctAnswer.value | 74 correctAnswers[correctAnswer.key] = correctAnswer.value |
98 askQuestion() | 75 askQuestion { |
76 selector() | |
77 } | |
99 } else { | 78 } else { |
100 hapticError() | 79 hapticError() |
101 playSound("wrongAnswer") | 80 playSound("wrongAnswer") |
102 | 81 |
103 withAnimation(.easeIn(duration: 0.5)) { | 82 withAnimation(.easeIn(duration: 0.5)) { |
124 livesScaleAmount = 1 | 103 livesScaleAmount = 1 |
125 } | 104 } |
126 } | 105 } |
127 } | 106 } |
128 | 107 |
129 func reset() { | 108 func reset(selector: () -> Void) { |
130 dataAsked = [String: T]() | 109 dataAsked = [String: T]() |
131 userScore = 0 | 110 userScore = 0 |
132 userLives = 3 | 111 userLives = 3 |
133 correctAnswers = [String: T]() | 112 correctAnswers = [String: T]() |
134 wrongAnswers = [String: T]() | 113 wrongAnswers = [String: T]() |
135 askQuestion() | 114 askQuestion { |
115 selector() | |
116 } | |
136 } | 117 } |
137 | 118 |
138 private func playSound(_ filename: String) { | 119 private func playSound(_ filename: String) { |
139 guard let soundFileURL = Bundle.main.url(forResource: filename, withExtension: "wav") else { | 120 guard let soundFileURL = Bundle.main.url(forResource: filename, withExtension: "wav") else { |
140 fatalError("Sound file \(filename) couldn't be found") | 121 fatalError("Sound file \(filename) couldn't be found") |