diff GeoQuiz/Controllers/GameProtocol+Extension.swift @ 34:6ec51a4ca897

fix crash when asking new questions
author Dennis C. M. <dennis@denniscm.com>
date Sat, 12 Nov 2022 14:23:05 +0100
parents 6d574bd1644f
children
line wrap: on
line diff
--- a/GeoQuiz/Controllers/GameProtocol+Extension.swift	Sat Nov 12 11:18:30 2022 +0100
+++ b/GeoQuiz/Controllers/GameProtocol+Extension.swift	Sat Nov 12 14:23:05 2022 +0100
@@ -68,21 +68,16 @@
         
         var userChoices = [String: T]()
         
-        while userChoices.count < 2 {
+        let correctAnswer = data.shuffled().first(where: { !dataAsked.keys.contains($0.key) })!
+        dataAsked[correctAnswer.key] = correctAnswer.value
+        userChoices[correctAnswer.key] = correctAnswer.value
+        
+        while userChoices.count < 3 {
             let choice = data.randomElement()!
             userChoices[choice.key] = choice.value
         }
         
-        let correctKey = data.keys.shuffled().first(where: {
-            !userChoices.keys.contains($0) &&               // Avoid duplicated items
-            !dataAsked.keys.contains($0)                    // Avoid items already asked
-        })!
-        
-        let correctValue = data[correctKey]!
-        
-        userChoices[correctKey] = correctValue
-        dataAsked[correctKey] = correctValue
-        correctAnswer = (key: correctKey, value: correctValue)
+        self.correctAnswer = correctAnswer
         self.userChoices = userChoices
     }
     
@@ -105,10 +100,10 @@
             haptics.error()
             playSound("wrongAnswer")
 
-//            withAnimation(.easeIn(duration: 0.5)) {
-//                livesScaleAmount += 1
-//                userLives -= 1
-//            }
+            withAnimation(.easeIn(duration: 0.5)) {
+                livesScaleAmount += 1
+                userLives -= 1
+            }
             
             wrongAnswers[choice.key] = choice.value