lundi 24 août 2015

How not repeat Switch cases? (Swift)

I'm doing a Quiz Game. I booked all the questions and their answers in each "case" of a switch. I want the case to be chosen in random order, but after the person answer the question and push the button "next", the function "randomQuestions" worked again, however I do not want it to be possible to repeat the same case previously used.

func randomQuestions ()
{
    var randomNumber = arc4random_uniform(3)
    while previousNumber == randomNumber
    {
        randomNumber = arc4random_uniform(3)
    }
    previousNumber = randomNumber

    switch(randomNumber)
    {
    case 0:
        textoHideUnhide()
        questionsLabel.text = "What color is the sun?"
        button1.setTitle("Yellow", forState: UIControlState.Normal)
        button2.setTitle("Black", forState: UIControlState.Normal)
        button3.setTitle("Blue", forState: UIControlState.Normal)
        button4.setTitle("White", forState: UIControlState.Normal)
        correctAnswer = "1"
        break
    case 1:
        textoHideUnhide()
        questionsLabel.text = "What color is the moon?"
        button1.setTitle("Red", forState: UIControlState.Normal)
        button2.setTitle("Blue", forState: UIControlState.Normal)
        button3.setTitle("White", forState: UIControlState.Normal)
        button4.setTitle("Orange", forState: UIControlState.Normal)
        correctAnswer = "3"
        break
    case 2:
        textoHideUnhide()
        questionsLabel.text = "What color is the grass?"
        button1.setTitle("White", forState: UIControlState.Normal)
        button2.setTitle("Green", forState: UIControlState.Normal)
        button3.setTitle("Orange", forState: UIControlState.Normal)
        button4.setTitle("Red", forState: UIControlState.Normal)
        correctAnswer = "2"
        break
    default:
        break
}




Aucun commentaire:

Enregistrer un commentaire