让测验应用程序不重复随机问题

时间:2015-05-11 03:41:51

标签: xcode swift random arc4random

我知道有一些带有shuffle的解决方案,或类似的东西,但我不知道我是否可以在我的代码中应用它们。你能帮我么?如何让问题不重复?

func RandomQuestions(){

    var RandomNumber = arc4random() % 28
    RandomNumber += 1

    switch(RandomNumber){
    case 1:
        QuestionLabel.text = "Who is the Numer One Best DJ According to DJ Mag 2014?"
        Button1.setTitle("Tiësto", forState: UIControlState.Normal)
        Button2.setTitle("Avicii", forState: UIControlState.Normal)
        Button3.setTitle("Hardwell", forState: UIControlState.Normal)
        Button4.setTitle("Dimitri Vegas & Like Mike", forState: UIControlState.Normal)
        CorrectAnwser = "3"
        break
    case 2:
        QuestionLabel.text = "Who is the Only DJ that played in an Olimpic Games?"
        Button1.setTitle("Avicii", forState: UIControlState.Normal)
        Button2.setTitle("Tiësto", forState: UIControlState.Normal)
        Button3.setTitle("Armin Van Buuren", forState: UIControlState.Normal)
        Button4.setTitle("Calvin Harris", forState: UIControlState.Normal)
        CorrectAnwser = "2"
        break
    }

    /* ...more questions...* /

}

5 个答案:

答案 0 :(得分:4)

您的代码需要重新格式化,保持这种方式(您建议的)每次需要添加时,最终会添加巨大的一堆代码新问题。请考虑这样做:

class Question{
 let question : String
 let optionA: String
 let optionB: String
 let optionC: String
 let optionD: String
 let correctAnswer: Int

 //add initializers here.
}

class Question{
 let question : String
 let options: [String]
 let correctAnswer: Int

 //add initializers here.
}

然后

class QuestionRepository{
 private var questions: [Question]

 /// use this to load questions to be asked only once per game, this way you will end up having the order in which you will ask questions and there will be no repetitions.
 var readyToAskQuestions : [Question] {
    let shuffledQuestions = shuffle(questions)
    return shuffledQuestions
 }      

 init()
  {
    //build your harcoded 'questions' variable here 
  }

 convenience init(url: String)
 {
   //or load your questions from a file, NSUserDefaults, database sql file, from a webservice on the internet, etc.. 
 }
} 
  

此处所述的随机播放功能可以是sorting arrays in swift

中所述的任何答案

答案 1 :(得分:3)

解决这个问题的最糟糕的方法是继续生成随机数字"跟踪"已经使用过的数字。对于100个问题的数组,您必须平均滚动最后一个随机数100次以获得最后一个可用问题。

执行此类操作的最简单方法是在使用数组之前对数组进行洗牌,然后按顺序迭代它。

你可以在这个答案中看到Fisher-Yates shuffle的实现...... How do I shuffle an array in Swift?

然后它只是迭代数组的情况......

for question in shuffledArray {
    // ask question
}

您甚至可以为它创建一个生成器,以便您获得next问题,等等。

此外,您通过使用开关并打开随机值,以错误的方式进行操作。

创建一个包含属性的Question对象... questionText,option1,option2,correctAnswer ... etc ...

您甚至可以创建一个选项数组,并将correctAnswerIndex与它一起存储。

然后将所有Question对象粘贴在一个数组中。更好的是,将它们放在plist文件中,以便您可以在运行时读取它们。这样管理起来要容易得多。

答案 2 :(得分:2)

我的answer到另一个帖子:

var someArray = Array(1...28)

let index = Int(arc4random_uniform(UInt32(someArray.count)))
let randomNumber = someArray[index]

someArray.removeAtIndex(index)

然后你可以将randomNumber传递给你的函数:

func randomQuestions(question: Int) {
    switch(question) {
    case 1: // Question1
    case 2: // Question2
}

答案 3 :(得分:1)

尝试让arc4random()不重复并不是查看此问题的正确方法。你想避免两次提出同样的问题,所以你需要以某种方式跟踪你已经问过的问题。我建议使用一个数组,并且可能给每个问题提供某种类型的ID,然后将每个问题ID存储在数组中,然后删除它们或将它们移动到一个新的问题"他们被问到阵列。

答案 4 :(得分:-3)

我会添加一个变量,将已经提出的问题保存到视图控制器:

var askedQuestions = [Int]()

然后,在您的功能中,您可以执行以下操作:

func RandomQuestions(){

    var randomNumber:Int = (Int)(arc4random() % 28 + 1)

    while find(askedQuestions, randomNumber) != nil && askedQuestions.count < 28 {
        randomNumber = (Int)(arc4random() % 28 + 1)
    }

    if askedQuestions.count > 28 {
        return
    }

    askedQuestions.append(randomNumber)

    switch(randomNumber){
    case 1:
        QuestionLabel.text = "Who is the Numer One Best DJ According to DJ Mag 2014?"
        Button1.setTitle("Tiësto", forState: UIControlState.Normal)
        Button2.setTitle("Avicii", forState: UIControlState.Normal)
        Button3.setTitle("Hardwell", forState: UIControlState.Normal)
        Button4.setTitle("Dimitri Vegas & Like Mike", forState: UIControlState.Normal)
        CorrectAnwser = "3"
        break
    case 2:
        QuestionLabel.text = "Who is the Only DJ that played in an Olimpic Games?"
        Button1.setTitle("Avicii", forState: UIControlState.Normal)
        Button2.setTitle("Tiësto", forState: UIControlState.Normal)
        Button3.setTitle("Armin Van Buuren", forState: UIControlState.Normal)
        Button4.setTitle("Calvin Harris", forState: UIControlState.Normal)
        CorrectAnwser = "2"
        break
    }

    /* ...more questions...*/

}

另请注意,当您用完所有28个问题时,您必须注意所发生的事情。 :)

PS:最好不要使用像RandomNumber这样的大写变量名。请改用randomNumber。这是一个很好的做法。 :)