
This is my code, The correct answer randomizes its position in the number of buttons. however, the current code seems to duplicate the 4th answer
It does this for every question and their respective set of answers.
The buttons tags are setup simply, the first one is tagged "1" and the 2nd button is tagged "2" and so on.
If any more info is needed feel free to ask.
I've left the problem code so that others who have similar issues will have a base to see what I did wrong.
Problem Code:
//
// AnimalViewController.swift
// It's Trival
//
// Created by Chris Levely on 12/18/16.
// Copyright © 2016 ZenithNomad. All rights reserved.
//
import UIKit
class AnimalViewController: UIViewController {
let questions = ["What is the fastest fish in the sea?", "Which animal has the most legs?"]
let answers = [["Sailfish", "Tuna", "Swordfish", "Marlin"], ["Millipede", "Spider", "Ant", "Octopus"]]
var currentQuestion = 0
var rightAnswerPlacement : UInt32 = 0
@IBOutlet weak var Question: UILabel!
@IBAction func AnswerQuestion(_ sender: AnyObject)
{
if (sender.tag == Int(rightAnswerPlacement))
{
print("Right")
}
else
{
print("Wrong")
}
if (currentQuestion != questions.count)
{
newQuestion()
}
}
func newQuestion()
{
Question.text = questions[currentQuestion]
rightAnswerPlacement = arc4random_uniform(4)+1
var button : UIButton = UIButton()
var x = 1
for i in 1...4
{
button = view.viewWithTag(i) as! UIButton
if (i == Int(rightAnswerPlacement))
{
button.setTitle(answers[currentQuestion][0], for: .normal)
}
else
{
button.setTitle(answers[currentQuestion][x], for: .normal)
x = 3
}
}
currentQuestion += 1
}
override func viewDidAppear(_ animated: Bool) {
newQuestion()
}
}
New Fixed Code:
//
// AnimalViewController.swift
// It's Trival
//
// Created by Chris Levely on 12/18/16.
// Copyright © 2016 ZenithNomad. All rights reserved.
//
import UIKit
class AnimalViewController: UIViewController {
let questions = ["What is the fastest fish in the sea?", "Which animal has the most legs?"]
let answers = [["Sailfish", "Tuna", "Swordfish", "Marlin"], ["Millipede", "Spider", "Ant", "Octopus"]]
var currentQuestion = 0
var rightAnswerPlacement : UInt32 = 0
@IBOutlet weak var Question: UILabel!
@IBAction func AnswerQuestion(_ sender: AnyObject)
{
if (sender.tag == Int(rightAnswerPlacement))
{
print("Right")
}
else
{
print("Wrong")
}
if (currentQuestion != questions.count)
{
newQuestion()
}
}
func newQuestion()
{
Question.text = questions[currentQuestion]
rightAnswerPlacement = arc4random_uniform(4)+1
var button : UIButton = UIButton()
var x = 1
for i in 1...4
{
button = view.viewWithTag(i) as! UIButton
if (i == Int(rightAnswerPlacement))
{
button.setTitle(answers[currentQuestion][0], for: .normal)
}
else
{
button.setTitle(answers[currentQuestion][x], for: .normal)
x += 1
}
}
currentQuestion += 1
}
override func viewDidAppear(_ animated: Bool) {
newQuestion()
}
}
It is because you hardcode x = 3, you should randomize x as well.
Here is my approach, I test it using UILabel, but it is the same concept
func newQuestion()
{
questionLabel.text = questions[currentQuestion]
var label : UILabel = UILabel()
var xArray = [0, 1, 2, 3]
var x = Int(arc4random_uniform(UInt32(xArray.count)))
for i in 1...4
{
label = view.viewWithTag(i) as! UILabel
label.text = answers[currentQuestion][xArray[x]]
if answers[currentQuestion][xArray[x]] == answers[currentQuestion][0]
{
print(answers[currentQuestion][xArray[x]])
//this is the answer, do something when user click this button
}
//remove the index x because you don't want to get a same number next time
xArray.remove(at: x)
x = Int(arc4random_uniform(UInt32(xArray.count)))
}
currentQuestion += 1
}

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With