*Xcode Project Entry 2* F1 Quiz - part 3
Well last time was frustrating but, interestingly, I cracked it just after my entry! That was hugely rewarding, even though MOST of the work was from Angela's project. Anyway, I adapted well and understand each aspect to it. Yesterday, I had another go, changing the views first of all, including background colour etc. I found some cool things, like the font colour changing when the answer was correct or not. Anyway, I had left it on a real cliffhanger when I headed out to Wimbledon that evening. The huge breakthrough was creating a randomator function, so that the questions were not in a set order. This is clearly a game changer!
However, I couldn't quite crack it! The answers weren't matching up to the questions...a bit like what I found initially in the last entry. Anyway, I sorted it! I did several things basically and here is the final code...
Start Time - 21:14
However, I couldn't quite crack it! The answers weren't matching up to the questions...a bit like what I found initially in the last entry. Anyway, I sorted it! I did several things basically and here is the final code...
Start Time - 21:14
import UIKit
class ViewController: UIViewController {
//Global variables
var question = QuestionBank()
var questionNumber = 0
var pickedAnswer = false
var score = 0
var randomNumber = 0
func randomator() {
randomNumber = Int.random(in: 0...9)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
updateScores()
}
//Labels
@IBOutlet weak var displayInfoLabel: UILabel!
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var questionNumberLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
//Answer labels
@IBAction func chooseAnswer(_ sender: UIButton) {
if sender.tag == 1 {
pickedAnswer = true
}
else if sender.tag == 2 {
pickedAnswer = false
}
questionNumber += 1
checkAnswer()
updateScores()
nextQuestion()
}
//Methods
func updateScores() {
questionNumberLabel.text! = "Question \(questionNumber + 1) out of 10"
scoreLabel.text! = "Score: \(score)"
questionLabel.text = question.list[randomNumber].questionText
randomator()
}
func nextQuestion() {
if questionNumber <= 9 {
questionLabel.text = question.list[randomNumber].questionText
}
else {
startOver()
let alert = UIAlertController(title: "Game over!", message: "Your score was \(score) out of 10!", preferredStyle: .alert)
let action = UIAlertAction(title: "Click here to dismiss. Then have another game!", style: .default, handler: {
action in self.startOver()
})
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
}
func checkAnswer() {
let correctAnswer = question.list[randomNumber].answer
if correctAnswer == pickedAnswer {
displayInfoLabel.text! = "Well done! That is the correct answer!"
displayInfoLabel.textColor = UIColor(displayP3Red: 0, green: 144/255, blue: 81/255, alpha: 1)
score += 1
} else {
displayInfoLabel.text! = "Unlucky! That was not correct!"
displayInfoLabel.textColor = .red
}
}
func startOver() {
questionNumber = 0
score = 0
displayInfoLabel.text = "How many questions can you answer correctly out of 10?"
displayInfoLabel.textColor = UIColor.init(displayP3Red: 1/255, green: 25/255, blue: 147/255, alpha: 1)
updateScores()
}
}
And the view....
So a couple of final things -
1. Facts - need many, many more!
2. App icons - these will NOT update. So I have to leave 2 for now.
Right so let's get 1. sorted then we're done for now! Adding facts...I will add these chronologically, using a web search rather than my own memory! Then I can add in other ones. The key thing will be then to update the code....
Finish Time - 22:19 (1 hour 15 minutes)
Most of that was adding more facts to be fair! But now I have 50 facts! I want to get to 100 questions. Then I will leave the app! That is plenty.
Comments
Post a Comment