通过遍历字典显示随机问题

Displaying random questions by looping through a dictionary

我通过尝试制作问答游戏开始学习 swift。但我坚持尝试显示字典中的随机问题。我使用字典的原因是因为我正在做的测验是基于具有不同符号的化学元素。

所以根据选择的难度,我创建了四个不同的词典:

//Defining the dictionaries per difficulty
var easy: [String: String] = [
    "H": "Waterstof",
    "He": "Helium",
    "Li": "Lithium",
    "B": "Boor",
    "C": "Koolstof",
    "O": "Zuurstof",
    "Ne": "Neon",
    "Al": "Aliminium",
    "Si": "Silicium",
    "K": "Kalium",
    "Fe": "Ijzer",
    "Ni": "Nikkel",
    "Zn": "Zink",
    "Cd": "Cadmium",
    "I": "Jood"
]
var normal: [String: String] = [
    "N": "Stikstof",
    "F": "Fluor",
    "Na": "Natrium",
    "Mg": "Magnesium",
    "P": "Fosfor",
    "CI": "Chloor",
    "Ar": "Argon",
    "S": "Zwavel",
    "Ca": "Calcium",
    "Cu": "Koper",
    "Au": "Goud",
    "Br": "Broom",
    "Ag": "Zilver",
    "Pt": "Platina",
    "Ba": "Barium"
]

等等..(困难,极端)

这是我的 viewDidLoad() 方法:

override func viewDidLoad() {

    switch (self.selectedDifficultyByUser){
        case "Makkelijk":// Means 'easy' in dutch
            //Assigning the easy dictionary to a temporary dictionary
            self.temp = self.easy
        case "Normaal":
            //Assigning the normal dictionary to a temporary dictionary
            self.temp = self.normal
        case "Moeilijk":
            //Assigning the hard dictionary to a temporary dictionary
            self.temp = self.moeilijk
        case "Extreem":
            //Assigning the extreme dictionary to a temporary dictionary
            self.temp = self.extreem
        default:
            println("Something went wrong")

    }

    super.viewDidLoad()
    self.answers = [self.btn1, self.btn2, self.btn3, self.btn4]

    pickRandomElement()
    randomizeAnswers()

    let updateTime : Selector = "updateTime"
    timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: updateTime, userInfo: nil, repeats: true)
    startTime = NSDate.timeIntervalSinceReferenceDate()

}

如您所见,我正在将我所有的按钮分配给数组 'answers',我将很快循环它。

然后将调用函数 pickRandomElement,如下所示:

func pickRandomElement() -> () {
    //First here we make sure that the 'answerOptions' Array is cleared,
    //Because it could cause that the correct answer won't be showed.
    answerOptions = [String]()


    if self.selectedDifficultyByUser == "Makkelijk" {
        let index: Int = Int(arc4random_uniform(UInt32(easy.count)))

        let symbol = Array(easy.keys)[index]
        let element = Array(easy.values)[index]

        self.currentQuestion = symbol
        self.correctAnswer = element

        //Assign the correctAnswer to the answerOptions array
        self.answerOptions.append(element)

        //Show the question to the user
        self.questionLabel.text = self.currentQuestion

        //remove the correctanswer from the dictionary, to
        //make sure that the answers won't be duplicated
        self.easy[symbol] = nil
        self.easy[element] = nil

        for (var i = 0; i < 3; i++) {
            let randomIndex: Int = Int(arc4random_uniform(UInt32(easy.count)))

            let optionSymbol = Array(easy.keys)[randomIndex]
            let optionElement = Array(easy.values)[randomIndex]

            self.answerOptions.append(optionElement)

            //Removing 'optionSymbol' and 'optionElement' from the array
            //to prevent duplicated answer
            self.easy[optionElement] = nil
            self.easy[optionSymbol] = nil
        }
        self.easy = self.temp //Filling the 'easy' array with the temporary array

    }
}

我遇到的问题出在上面显示的 for 循环中。我循环三次以选择随机元素并将它们显示给用户。在此过程中,我将从(简单)数组中删除随机选择的元素以防止重复答案。 因为我要从 easy 数组中删除这些元素,所以我将我在开始时创建的临时数组分配回 easy 数组。

如果我这样做,整个easy数组将在三轮左右后为空。 如果我相反的我会得到一个无限循环。

如果我做错了,有人可以指导我正确的方向或帮助我解决这个问题吗?

提前致谢!

已更新

Firo 的解决方案非常适合我。但我知道面临另一个错误。即,pickRandomElement 函数 returns 有时会多次提出问题。我试着制作另一本字典,把提出的问题放在那里,然后检查是否已经有人问过这个问题。然而,随着回答问题数量的增加,这将需要对每个问题进行线性增加的查找,直到达到所有查找。有人知道我该如何解决这个问题吗? –

因此,如果没有你的所有代码,我只能给你一个有根据的猜测,即用你当前的代码处理你的无限循环情况的好方法。

这里防止无限循环的一个简单方法是确认您还有其他问题要问。保留实例值以检查您还想问多少问题。

var remainingSteps = 5

然后当用户回答正确的问题时,在提出下一个问题之前检查它

func userAnsweredQuestionCorrectly() {
    if self.remainingSteps > 0 {
        // Present next question
        // Decrement remaining steps
        self.remainingSteps--
    else {
        // Done asking questions, back to menu (or whatever is next)
    }
}

Firo 的解决方案很棒。 This one might help you with getting a unique question each time to avoid repetition. Let me know here or on codementor 如有任何问题。