Issues with answer checking and out-of-bounds answers in SwiftUI game

I am building a math quiz game in SwiftUI with a liquid swipe interface where the user needs to swipe or tap the correct answer among two choices displayed on the screen. However, I'm facing several issues with the game logic and answer checking. Here are the problems I'm encountering:

  1. The game is generating answers that are out of bounds: Sometimes, when the user swipes or taps the correct side, the game recognizes an entirely different answer that is out of the valid range of answer choices.
  2. Inconsistent answer checking: The answer checking mechanism is not working reliably. There are cases where the correct side is swiped or tapped, but the game incorrectly prints "INCORRECT!" instead of "CORRECT!". In addition, when the recognized answer is out of bounds, it doesn't print anything.

I suspect that the issues lie in the swipe function and the answer checking logic. I have already tried implementing various fixes suggested in online resources, but none of them have resolved the problems.

There are 3 parts to the code. I have included links to the ContentView and any other necessary code:

Could someone please help me identify the issues with the answer checking and out-of-bounds answers in my SwiftUI game? If there are any modifications I need to make in this question, please let me know with a helpful comment. Any suggestions or insights would be greatly appreciated.

Thank you in advance for your assistance!

Answered by Claude31 in 757931022

Nice design.

But, why do you test for correct answer in swipe ? Test should only occur when user taps an answer.

If have added a print and it often shows INCORRECT after Swipe

            if let selectedAnswerIndex = selectedAnswerIndex {
                let selectedAnswer = choiceArray[selectedAnswerIndex]
                print("Test in Swipe", selectedAnswerIndex)
                answerIsCorrect(answer: selectedAnswer)
                self.selectedAnswerIndex = nil
            }

I commented out most this part, only keeping self.selectedAnswerIndex = nil

//            if let selectedAnswerIndex = selectedAnswerIndex {
//                let selectedAnswer = choiceArray[selectedAnswerIndex]
//                answerIsCorrect(answer: selectedAnswer)
                self.selectedAnswerIndex = nil
//            }

Now it seems to work correctly:

  • no answers out of bound (in fact I never observed this even with the code in Swipe)
  • No more inconsistent answer checking (there were before commenting out)
Accepted Answer

Nice design.

But, why do you test for correct answer in swipe ? Test should only occur when user taps an answer.

If have added a print and it often shows INCORRECT after Swipe

            if let selectedAnswerIndex = selectedAnswerIndex {
                let selectedAnswer = choiceArray[selectedAnswerIndex]
                print("Test in Swipe", selectedAnswerIndex)
                answerIsCorrect(answer: selectedAnswer)
                self.selectedAnswerIndex = nil
            }

I commented out most this part, only keeping self.selectedAnswerIndex = nil

//            if let selectedAnswerIndex = selectedAnswerIndex {
//                let selectedAnswer = choiceArray[selectedAnswerIndex]
//                answerIsCorrect(answer: selectedAnswer)
                self.selectedAnswerIndex = nil
//            }

Now it seems to work correctly:

  • no answers out of bound (in fact I never observed this even with the code in Swipe)
  • No more inconsistent answer checking (there were before commenting out)
Issues with answer checking and out-of-bounds answers in SwiftUI game
 
 
Q