Angela Yu Course Part 7 (up to lesson 90)

Before I go into the next part of Angela's course, I've been experimenting with the 'Yahtzee' app! It's actually gone much better than expected - I've managed to put together this:



The dice roll when they are clicked, you click on the number buttons (1, 2 etc.) to assign that score...it took a lot of logic! One of the things I'm most proud of is the diceFunction, which combines several functions and bits of code together!

unc diceFunction(diceNumber: Int) {
        
        func isDiceEqual(_: Int) -> Int {
            
            var count = 0
            
            let diceIndexArray = [randomDiceIndex1,randomDiceIndex2, randomDiceIndex3, randomDiceIndex4]
            
            for x in diceIndexArray {
                
                if x == diceNumber - 1 {
                    
                    count += 1
                }
                
            }
            
            return count * diceNumber
            
        }
    
        let diceText = isDiceEqual(diceNumber)
        
        if isDiceEqual(diceNumber) >= 1 {
            
            
            diceMessage = "\(diceText)"
            
        } else {
            
            diceMessage = "Try another dice number!"
        }
        
        

    }
I'm sure there were easier ways of doing it but it works! Then the code required for each of the number buttons is pretty simple:

   @IBAction func oneButton(_ sender: UIButton) {
        diceFunction(diceNumber: 1)
        
        scoreDiceOne.text = diceMessage
        
    }

There it is! It's not perfect - I need to add in a 'total' label, which got messed up before. Yahtzee also has FIVE dice...forgot that! I could also add in labels for three of a kind, full house...but I also need a limit in number of goes, or when all of the text boxes are full of scores....So I've decided to leave it for now. It's the furthest I've ever gone with my own project - much further than F1 facts!

Back to Angela's course, I've literally got half an hour now to get some coding done!

Quizzler App

Modal View Controller (MVC). This is something that Pasan mentioned and explained before. My understanding is that this is the link between what you see (view) the model (the code that is separate) and the controller (the built in code). Probably got some of that wrong!

This app looks awesome! This will be VERY easily adaptable for future apps. Would be very easy to make a Buffy app, an F1 quiz...lots that can be done here!



This is a good way of locking the orientation - it will always be portrait!

Dictionaries - on the plist - there is a key-value pair.

Score label - putting in a  default value of the largest value that the label will ever show up. Just to stop the score from getting truncated.

All labels and constraints in - makes sense to save time! Always break the connections then make changes to labels if necessary.



Creating a Data Model

OK, so I was kind of right! Creating a separate Swift file with all the code. I think the idea is to keep the code separate from all of the buttons/labels/objects etc. where possible.

You should usually have Foundation and UIKit imported - these 'tools' are fairly lightweight and necessary!

Properties - variables/constants - associated with a class.

Methods - these are a function associated with a class

    
    let questionText: String
    let answer: Bool
    
    init(text: String, correctAnswer: Bool) {
        questionText = text
        answer = correctAnswer
    }
     

}


OK, that's all we have time for - for today! Creating the class and object was familiar and hopefully it will clear up some of syntax and initialisation issues that I encountered before). Thinking about it a class would be good for the dice in the Yahtzee game, and possibly for the number buttons and score labels. But that's fine - there are plenty of ways to skin a cat! So next time, there will be more about Object Oriented Programming, which I'm sure will be useful!


Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

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