Skip to content

Instantly share code, notes, and snippets.

@JosephTLyons
Last active July 19, 2019 07:06
Show Gist options
  • Save JosephTLyons/b5db5b173740a05598c68b8993a4855a to your computer and use it in GitHub Desktop.
Save JosephTLyons/b5db5b173740a05598c68b8993a4855a to your computer and use it in GitHub Desktop.
Angela Yu Section 10: Coding Challenge #3 - Control Flow - Build a Story App Life Lifeline (Solution)
//
// ViewController.swift
// Destini
//
// Created by Philipp Muellauer on 01/09/2015.
// Copyright (c) 2015 London App Brewery. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
// Our strings
let story1 = "Your car has blown a tire on a winding road in the middle of nowhere with no cell phone reception. You decide to hitchhike. A rusty pickup truck rumbles to a stop next to you. A man with a wide brimmed hat with soulless eyes opens the passenger door for you and asks: \"Need a ride, boy?\"."
let answer1a = "I\'ll hop in. Thanks for the help!"
let answer1b = "Better ask him if he\'s a murderer first."
let story2 = "He nods slowly, unphased by the question."
let answer2a = "At least he\'s honest. I\'ll climb in."
let answer2b = "Wait, I know how to change a tire."
let story3 = "As you begin to drive, the stranger starts talking about his relationship with his mother. He gets angrier and angrier by the minute. He asks you to open the glovebox. Inside you find a bloody knife, two severed fingers, and a cassette tape of Elton John. He reaches for the glove box."
let answer3a = "I love Elton John! Hand him the cassette tape."
let answer3b = "It\'s him or me! You take the knife and stab him."
let story4 = "What? Such a cop out! Did you know traffic accidents are the second leading cause of accidental death for most adult age groups?"
let story5 = "As you smash through the guardrail and careen towards the jagged rocks below you reflect on the dubious wisdom of stabbing someone while they are driving a car you are in."
let story6 = "You bond with the murderer while crooning verses of \"Can you feel the love tonight\". He drops you off at the next town. Before you go he asks you if you know any good places to dump bodies. You reply: \"Try the pier.\""
// UI Elements linked to the storyboard
@IBOutlet weak var topButton: UIButton! // Has TAG = 1
@IBOutlet weak var bottomButton: UIButton! // Has TAG = 2
@IBOutlet weak var storyTextView: UILabel!
var storyNumber: Int = 1
override func viewDidLoad() {
super.viewDidLoad()
updateStory(story1, answer1a, answer1b, false)
}
// User presses one of the buttons
@IBAction func buttonPressed(_ sender: UIButton) {
if (sender.tag == 1) {
if (storyNumber == 1) {
updateStory(story3, answer3a, answer3b, false)
storyNumber = 3
}
else if (storyNumber == 2) {
updateStory(story3, answer3a, answer3b, false)
storyNumber = 3
}
else if (storyNumber == 3) {
updateStory(story6, "", "", true)
storyNumber = 6
}
}
else if (sender.tag == 2) {
if (storyNumber == 1) {
updateStory(story2, answer2a, answer2b, false)
storyNumber = 2
}
else if (storyNumber == 2) {
updateStory(story4, "", "", true)
storyNumber = 4
}
else if (storyNumber == 3) {
updateStory(story5, "", "", true)
storyNumber = 5
}
}
}
func updateStory(_ story: String,
_ topButtonText: String,
_ bottomButtonText: String,
_ hideButtons: Bool) {
storyTextView.text = story
topButton.setTitle(topButtonText, for: .normal)
bottomButton.setTitle(bottomButtonText, for: .normal)
topButton.isHidden = hideButtons
bottomButton.isHidden = hideButtons
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment