jeudi 25 février 2021

Random Quiz App without duplication in Swift [duplicate]

I try to make quiz app in Swift and want to display the quiz randomly without repeating .

I think the first thing I have to do is to make quiz random and the second is to display without duplication .

But I haven't been able to do the first thing . I'm very beginner with programming . Please help my code !

This code below is to make quiz random .


【Question.swift】  

import Foundation

struct Question {
    let text: String
    let answers: [String]
    let rightAnswer: String
    init(q: String, a: [String], correctAnswer: String) {
        text = q
        answers = a
        rightAnswer = correctAnswer
    }
}


【QuizBrain.swift】 

import Foundation

struct QuizBrain {

var randomNumber = Int.random(in: 0..<11) 
var questionNumber = 0

let quiz = [
        Question(q: "Which is C ?", a: ["B", "C", "D"], correctAnswer: "C"),
        Question(q: "Which is F?", a: ["F", "G", "H"], correctAnswer: "F"),
        Question(q: "Which is J ?", a: ["H", "I", "J"], correctAnswer: "J"),
        Question(q: "Which is L ?", a: ["L", "M", "N"], correctAnswer: "L"),
        Question(q: "Which is P ?", a: ["P", "Q", "R"], correctAnswer: "P"),
]


func randomQuiz() ->  Question {
        let random = quiz.shuffled() 
        let randomQuiz = random[randomNumber] 
        return randomQuiz
    }

mutating func nextQuestion() {
        if questionNumber + 1  < quiz.count {
            questionNumber += 1
        } else {
            questionNumber = 0
        }
    }

mutating func getText() -> String {
        return randomQuiz().text
}

mutating func getAnswers() -> [String] {
        return randomQuiz().answers
}

mutating func checkBeginnerAnswer(userAnswer: String) -> Bool {
        if userAnswer == beginnerQuiz[questionNumber].rightAnswer {
            return true
        } else {
            return false
        }
    }
}


【VocabularyController.swift】 

import UIKit
import AVFoundation

class VocabularyController: UIViewController {

    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var choice1: UIButton!
    @IBOutlet weak var choice2: UIButton!
    @IBOutlet weak var choice3: UIButton!

    var quizBrain = QuizBrain() 
    override func viewDidLoad() {
        super.viewDidLoad()
        updateUI()
    }

  @IBAction func answerButtonPressed(_ sender: UIButton) {
        let userAnswer = sender.currentTitle!
        let userGotItRight = quizBrain.checkAnswer(userAnswer: userAnswer)

        quizBrain.nextQuestion() 

        Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(updateUI), userInfo: nil, repeats: false)
  }

@objc func updateUI() {
        questionLabel.text = quizBrain.getText()   
        let answerChoices = quizBrain.getAnswers() 

        choice1.setTitle(answerChoices[0], for: .normal) 
        choice2.setTitle(answerChoices[1], for: .normal)
        choice3.setTitle(answerChoices[2], for: .normal) 
   }
}



There is no error with this code . And the quiz is randomized. But I can't display the question with the answer which I want to display .

For example when the q in Question is "Which is F? , the a in Question should be ["F", "G", "H"] . But sometimes the a is ["P", "Q", "R"] or sometimes ["L", "M", "N"]... The answers don't correspond the text which be corresponded .

So please tell me how I can display the quiz randomly and correctly . And I would like to know how I can display it without duplication in this code !

Thank you in advance !




Aucun commentaire:

Enregistrer un commentaire