Angela Yu Course Part 10 (up to lesson 112)

It's unlikely that I'll be able to do any Swift tomorrow as I'm going to Eastbourne today, for the weekend. So I can give half an hour now, maximum!

Coding Challenge - Destini App!

Choose your own adventure books! I used to ADORE these. Angela explains that these have had some sort of revival through the Lifeline games. It's very binary - yes/no mostly. I have my own experience of this based on WDC...


Now the challenge is to complete the game! Having followed the first few instructions, which I found a bit confusing it must be said, I was about to surrender and see the answer. Then I managed to stop myself - I CAN do this, or at least have a good go! Even if I don't get the correct answer, it will be good practice to put what I know into practice. This is my weakness, which I keep coming back to - that when some problem solving is required, I fold far too quickly. So here is what I need to do:


Let's break this down...

I need to use the If statement. The storyIndex variable will be the value that is passed into this. 
Each time I will need to update - storyTextView, topButton and bottomButton (tags for the latter 2). 

&& and | | will come in handy by the looks of it. 

So I need to actually create a storyIndex first. This could mean an array....Ah no the storyIndex was the Int value I created but haven't used yet. OK, so this is going to be handy for knowing where we are in the story. Right, I'm going to have a go!

Well I've tried a couple of things and it's not working :(

I think I have the main idea for the if statements, but it keeps going to story6, when I've put in adding the storyIndex variable. 

I've given it a go and got some of it - managed to hide the buttons when story6 comes along, but I can't seem to crack this aspect of it - looking at answers now....

YES! Before seeing exactly what Angela did, I realised something - I was doing a SEPARATE if statement, not one as part of else if! That should make a difference. Let's complete it now!!!!

OK so I got there but it was not easy! The If/else thing through me and complicated things. 

I want to have a look at the code again to make it more logical. Here is what I currently have:

@IBAction func buttonPressed(_ sender: UIButton) {
    
        // TODO Step 4: Write an IF-Statement to update the views
        
        if sender.tag == 1 && storyIndex == 1 {
            
            storyTextView.text = story3
            
            topButton.setTitle(answer3a, for: .normal)
            
            bottomButton.setTitle(answer3b, for: .normal)
            
            storyIndex = 3
            
             print(storyIndex)
            
            
        } else if sender.tag == 2 && storyIndex == 1 {
            
            storyTextView.text = story2
            
            topButton.setTitle(answer2a, for: .normal)
            
            bottomButton.setTitle(answer2b, for: .normal)
            
            storyIndex = 2

            print(storyIndex)
            
        } else if sender.tag == 1 && storyIndex == 3 {
            
            storyTextView.text = story6
            
            topButton.isHidden = true
            
            bottomButton.isHidden = true
            
            storyIndex = 6
            

            
        } else if sender.tag == 2 && storyIndex == 2 {
            
            storyTextView.text = story3
            
            topButton.setTitle(answer3a, for: .normal)
            
            bottomButton.setTitle(answer3b, for: .normal)
            
            storyIndex = 3
            
        
            
        } else if sender.tag == 2 && storyIndex == 3 {
            
            storyTextView.text = story5
            
            topButton.isHidden = true
            
            bottomButton.isHidden = true
            
           
            
            storyIndex = 5
            
        
    } else if sender.tag == 1 && storyIndex == 3 {
    
    storyTextView.text = story4
    
    topButton.isHidden = true
    
    bottomButton.isHidden = true
    

            
            storyIndex = 4
            
    
    }

I think it makes more sense to sort by storyIndex, then by tag! So the best way was to look at each possible outcome and do it sequentially. Makes good sense and am SOOOO pleased that I did not just see the answer!

@IBAction func restartButton(_ sender: UIButton) {
        
        storyIndex = 1
        
        storyTextView.text = story1
        
        topButton.isHidden = false
        
        bottomButton.isHidden = false
        
        topButton.setTitle(answer1a, for: .normal)
        
        bottomButton.setTitle(answer1b, for: .normal)
        
        restartView.isHidden = true
        
    }

I needed SOME help with the idea of a restart button but figured most of out out myself. Angela actually made this as a restart function, which makes sense, but to be honest this works fine for now!

So, a good experience all in all. Ultimately, I did the right thing by not just coding along with Angela and sorting out the logic side of it myself. I'm sure there are simpler ways of coding the whole if statement bit, but to be honest, it's fine! Feeling pleased that I've ben able to problem solve, my main weakness and area to work on!

Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

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