0

I have a quiz app that generates a random question. at the moment because of the randomization, you can sometimes answer the same question multiple times. I have an an array of questions and I use the arc4Random method to generate a random question on the screen.

how do I make it so that swift recognises that a question is already been on and to skip it? would I have to make an if statement? and if so how would I code that?

var currentQuestion = Int(arc4random_uniform(31) + 1)
var rightAnswerPlacement:UInt32 = 0
var seconds = 30
var timer = Timer()
var score = 0
if (sender.tag == Int(rightAnswerPlacement)) {
    score += 1
    scoreLabel.text = "\(score)"
    seconds = 100
    currentQuestion = Int(arc4random_uniform(32) + 1)
} else {
    timer.invalidate()
    end()
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
jon wilson
  • 54
  • 10
  • Pop each question from your array as you ask it, then you are only left with un-asked questions. Of course, you might consider using a second array for this, so you can reset the quiz, but that depends on your implementation – MadProgrammer Jan 28 '19 at 20:54
  • 1
    Another approach might be to simply shuffle the a copy of the original array (ie randomise the elements them selves), then all you need to do is pop the first element off the array until there are none left - see [How do I shuffle an array in Swift?](https://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift) for more details – MadProgrammer Jan 28 '19 at 20:55
  • Or use a set, `Set`, to keep track of asked questions – Joakim Danielson Jan 28 '19 at 20:55
  • I tried the .shuffled() which worked, however my answers array didn't match the questions. how do I pop each question as I ask it? – jon wilson Jan 28 '19 at 21:04
  • 1
    Don't have seperate questions and answers arrays. Create a struct to store the question with its answers and put instances of that struct into an array – Paulw11 Jan 28 '19 at 22:36

2 Answers2

3
var currentQuestion = Int(arc4random_uniform(36) + 1)
var rightAnswerPlacement:UInt32 = 0
var seconds = 30
var timer = Timer()
var score = 0
var asked = [Int]()

var recordData:String!

@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!

@IBOutlet weak var buttonOne: UIButton!
@IBOutlet weak var buttonTwo: UIButton!
@IBOutlet weak var buttonThree: UIButton!

@IBAction func answerButtons(_ sender: AnyObject) {

    if (sender.tag == Int(rightAnswerPlacement)) {

        score += 1
        scoreLabel.text = "\(score)"
        seconds = 100
        currentQuestion = Int(arc4random_uniform(36) + 1)

       while asked.contains(currentQuestion) {

            currentQuestion = Int(arc4random_uniform(36) + 1)
        }
        asked.append(currentQuestion)
        print(asked)

        if asked.count == questions.count {
            currentQuestion = 0
          return
        }
jon wilson
  • 54
  • 10
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

You could simply shuffle the question indices, and process that array:

let questionCount = 32
let questionIndices = (0..<questionCount).shuffled()
for index in questionIndices {
    let question = questions[index]
    let answer = answers[index]
    // process the question
}

You can trust the shuffled() implementation to be really random.

Cristik
  • 30,989
  • 25
  • 91
  • 127