*Xcode Project Entry 2* F1 Quiz - part 1

It's Christmas Eve and time to code! To think, a year ago, I was plodding along with some eBook, blindly convincing myself that I was on a steady road to coding greatness. Really, it was from June where I made the decision to start again, humbling as that may seem. It would have been comforting to have completed endless 'beginner' courses but branching out and applying the theory is the key thing, which brings us to this entry. Yes, my second Xcode OWN project! I thought about doing an educational one, as there is certainly a lot of scope and relevance for that, being a teacher and all. However, making a quiz makes sense, for various reasons. 

First off, I have made this before through Angela's excellent tutorial. No offence to Sandra, but hers is not even close to the elegance and effectiveness of Angela's project. The one thing to take from Sandra is the stack views. Definitely something I will use from now on. This is one of the great things about learning from a myriad of 'experts' - you glean something from each of them, sometimes without even realising it. Also, I like the idea of a quiz as it's something that is accessible and simple in its ultimate goal. Finally, I can tweak and change the content - update and change wherever need be. 

Start Time - 12:30

Now, before commencing, it's a VERY good idea (thank you, Ray, for that one!) is a 'to-do' list so I know what is required. Let's break this down.....

View (what is seen on the app)

  • Question number
  • Score
  • Questions with corresponding answers (labels needed)
  • Buttons to choose answers (4 different answers?)
  • Alerts to show responses
  • Variety for responses to show right/wrong answer
  • Image (F1 related)

Code/Model


  • Bank of questions with answers
  • Determining if answer is right/wrong
  • Tracking score
  • Tracking question number
  • Types of relevant 'false' answers (will come to this)
  • Arrays of relevant answers
  • Categories of a) drivers, b) years, c) circuits, d) cars/teams
OK that will do for now. Let's get the main bit set up!






















Cool! Now, it's 13:30 but have been watching Bodyguard at the same time so not really representative.

Anyway, stack views used for top two labels, two rows of answer buttons and bottom row of labels!

Now to do constraints...

Done!

OK, here's what I've got in the View Controller:

import UIKit

class ViewController: UIViewController {
    
    //Global variables
    
    var question = QuestionBank()
    
    var questionNumber = 0
    
    var score = 0
    
    
    //Methods
    
    func nextQuestion() {
        
        questionNumber += 1
    }
    
    func updateScore() {
        
    }
    
    
    
    //Labels
    
    @IBOutlet weak var displayInfoLabel: UILabel!
    
    @IBOutlet weak var questionLabel: UILabel!
    
    @IBOutlet weak var questionNumberLabel: UILabel!
    
    @IBOutlet weak var scoreLabel: UILabel!
    
    //Answer buttons
    
    @IBAction func chooseAnswer1(_ sender: Any) {
    }
    
    @IBAction func chooseAnswer2(_ sender: Any) {
    }
    
    @IBAction func chooseAnswer3(_ sender: Any) {
    }
    
    @IBAction func chooseAnswer4(_ sender: Any) {
    }
    

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }


}



In the question Bank (separate Swift file). 

import Foundation

enum QuestionType {
    case driver, numbers, team
}

struct QuestionBank {
    
    var driverQuestions: [String: String] = ["Who has won 7 Driver's championships?": "Michael Schumacher", "Who won the 2018 Driver's Championship?": "Lewis Hamilton", "Who was the first driver to win 5 World Driver's Championships?": "Juan Manuel Fangio", "Who won two consecutive World Driver's Championships in 1998 to 1999?": "Mika Hakkinen"]
    
    var numbersQuestions: [String: Int] = ["In which year did Damon Hill win his one and only World Driver's Championship?": 1996, "How many races did Jackie Stewart win out of the 99 he competed in?": 27, "How many World Driver's Championships did Alain Prost win?": 4, "How old was Max Verstappen when he won his first F1 race?": 18]
    
    var teamQuestions: [String: String] = ["Which Formula one team has won the most races of all time?": "Ferrari", "WHich team won consecutive World Driver's and Constructor's Championships between 2010 and 2013?": "Red Bull", "Which team did Jim Clark win both of his World Championships with?": "Lotus"]
}

So I've got all this! Done in an hour and a half (though with Mum and Dad in the living room in the UK, with various things on TV!). 

Anyway, the challenge is now on! All the setup is done, which is a good place to stop!

Next time, this is what I will focus on:

*A way of accessing the question bank and displaying any question at random
*Having the correct answer on one of the four buttons
*Having the three other buttons as 'fake' answers but still relevant!

This is going to be hard. And if I can't figure it out I will use ideas from Angela's app, THEN look it up. If not, then the project will stay in limbo (worse case scenario!). 

Finish Time 14:15 (1 hour 45 minutes, though interrupted, in total)

So a great session. Using the stack views is definitely worth it (thanks Sandra!). I've just realised I never put in an image anywhere so I may need to rejig the whole screen view. The main thing will be getting COMMUNICATION between the code and the buttons/labels. 

But hey, people spend thousands and thousands of pounds and hours on apps. If I can make one in a few hours with no money then that's pretty amazing!

Comments

Popular posts from this blog

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)