通过循环字典显示随机问题

时间:2015-06-04 12:50:23

标签: ios swift dictionary random xcode6

我开始通过尝试制作一个问答游戏来学习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()

}

正如你所看到的那样,我正在将所有按钮分配给数组'答案',我将很快将其循环播放。

然后将调用函数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数组在三轮左右后将为空。 如果我相反,我会得到一个infinte循环。

如果我以错误的方式做这件事或帮助我摆脱这个问题,有人可以把我放在正确的方向吗?

提前致谢!

已更新

Firo的解决方案对我来说非常合适。但我知道面对另一个错误。也就是说,pickRandomElement函数有时会多次返回被询问的问题。我试着制作另一本字典并将问题放在那里并检查是否已经问过这个问题。但是,随着回答的问题数量的增加,这将需要对所提出的每个问题进行线性增加的查找量,直到达到所有查询。有人知道我怎么能解决这个问题吗? -

2 个答案:

答案 0 :(得分:1)

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

在此处阻止无限循环的一种简单方法是验证您是否还有其他想要问的问题。保留一个实例值来检查您还想问多少问题。

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)
    }
}

答案 1 :(得分:0)

Firo的解决方案非常棒。 This每次都可以帮助您获得一个独特的问题,以避免重复。如果您有任何疑问,请在此处或codementor告诉我。