*Xcode Project Entry 3* Times Tables Quiz - Part 4
11 days! 11 days?! That is a seriously poor effort. It may be a reflection on the fact that I have been doing more exercise lately - gym, tennis and yoga but even then...not good at all! It's nothing to worry about though! Another factor is that last time, I failed to get a times table generator working. So that's what I need to achieve first. I will spend half an hour or so trying to figure it out, if not then I will have to find one online, which is no shame! Anyway, let's just move on and accept that a week and a half is gone but from now on we're getting the time back in!
Start Time - 13:44
So like I said, let's revisit that times tables generator...
Ok, I've got the random numbers and what the answer should be returned as - in separate functions.
I've also connected all of the outlets -
Start Time - 13:44
So like I said, let's revisit that times tables generator...
Ok, I've got the random numbers and what the answer should be returned as - in separate functions.
I've also connected all of the outlets -
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var timerDisplay: UILabel!
@IBAction func answerBox(_ sender: Any) {
}
@IBOutlet weak var questionNumberLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
I need to get the timer working. Let's use the code from HowManyLetters...
Right - update after nearly an hour!
I've changed the whole view as the other one just wanted working with the background. That's something I will check out and figure out another time.
At the moment, nothing is happening with the timer or with the submit answer. So quite a ways to go!
Here's what I need to do -
Need to get the submit answer linked up so that it actually checks the answer.
Update at 15:53!
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textFieldDisplay.delegate = self
newQuestion()
runTimer()
messageLabel.text = "Good luck!"
}
//OUTLETS
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var timerDisplay: UILabel!
@IBOutlet weak var questionNumberLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var messageLabel: UILabel!
//FUNCTION FOR TEXT FIELD
@IBOutlet weak var textFieldDisplay: UITextField!
@IBAction func submitAnswerButton(_ sender: Any) {
isAnswerCorrect()
newQuestion()
}
@IBAction func newGameButton(_ sender: Any) {
newGame()
resetTimer()
}
func hideKeyboard() {
textFieldDisplay.resignFirstResponder()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textFieldDisplay.resignFirstResponder()
return true
}
var score = 0
var question = 0
//TIMER
var seconds = 30
var timer = Timer()
var timeIsRunning = false
func runTimer() {
timeIsRunning = true
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(ViewController.updateTimer)), userInfo: nil, repeats: true)
}
@objc func updateTimer() {
seconds -= 1
timerDisplay.text = String(seconds)
if seconds == 0 {
pauseTimer()
let alert = UIAlertController(title: "Game over!", message: "Your score was \(score)", preferredStyle: .alert)
let action = UIAlertAction(title: "Click here to dismiss. Then select 'NEW GAME'!", style: .default, handler: {
action in self.submitAnswerButton(action)
})
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
}
func pauseTimer() {
timer.invalidate()
timeIsRunning = false
}
func resetTimer() {
if timeIsRunning == false {
runTimer()
seconds = 30
timerDisplay.text = String(seconds)
} else if timeIsRunning == true {
pauseTimer()
runTimer()
seconds = 30
timerDisplay.text =
String(seconds)
}
}
//RANDOM NUMBERS AND MULTIPLIER
var randomNumber1 = 0
var randomNumber2 = 0
func isAnswerCorrect() {
if timeIsRunning == true {
let answer = randomNumber1 * randomNumber2
if Int(textFieldDisplay.text!) == answer {
score += 1
messageLabel.text = "Correct!"
}
else {
messageLabel.text = "Incorrect! Right answer: \(answer)"
}
}
}
func resetTextBox() {
textFieldDisplay.text = ""
}
func newQuestion() {
randomNumber1 = Int.random(in: 1...12)
randomNumber2 = Int.random(in: 1...12)
questionLabel.text = ("\(randomNumber1) x \(randomNumber2)")
updateScores()
resetTextBox()
}
func updateScores() {
question += 1
questionNumberLabel.text = "Question: \(question)"
scoreLabel.text = "Score: \(score)"
}
func newGame() {
question = 0
score = 0
messageLabel.text = "Good luck!"
scoreLabel.text = "Score: 0"
questionNumberLabel.text = "Question: 1"
resetTextBox()
newQuestion()
resetTimer()
}
}
So a lot of code and it's pretty much working! The alerts don't work when the times is up but the key thing is that I got the actual times tables bit working. A good effort and nearly a completed app!
Finish Time - 15:53 (2 hours 9 minutes)
So a big session and a rewarding one albeit with lots of little frustrations. I need one more entry on this to complete it - fix the timer issue and get all the layout sorted again! Done for now!
Comments
Post a Comment