-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.swift
110 lines (84 loc) · 3.79 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// ViewController.swift
// BillOrSteve
//
// Created by James Campagno on 6/8/16.
// Copyright © 2016 Flatiron School. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var counterLabel: UILabel!
@IBOutlet weak var factLabel: UILabel!
@IBOutlet weak var steveButton: UIButton!
@IBOutlet weak var billButton: UIButton!
var billAndSteveFacts: [String : [String]] = [:]
var correctPerson: String = ""
var counter: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
createFacts()
showFact()
}
func showFact() {
let (person, fact) = getRandomFact()
correctPerson = person
factLabel.text = fact
}
@IBAction func steveGuess(_ sender: Any) {
if correctPerson == "Steve Jobs" {
counter += 1
counterLabel.text = String(counter)
}
showFact()
}
@IBAction func billGuess(_ sender: Any) {
if correctPerson == "Bill Gates" {
counter += 1
counterLabel.text = String(counter)
}
showFact()
}
func getRandomFact() -> (String, String) {
let person = randomPerson()
if let facts = billAndSteveFacts[person] {
let fact = produceFact(from: facts, with: person)
return (person, fact)
}
let otherPerson = person == "Bill Gates" ? "Steve Jobs" : "Bill Gates"
if let facts = billAndSteveFacts[otherPerson] {
let fact = produceFact(from: facts, with: otherPerson)
return (otherPerson, fact)
}
billButton.isUserInteractionEnabled = false
steveButton.isUserInteractionEnabled = false
return ("", "GAME OVER")
}
func produceFact(from array: [String], with person: String) -> String {
var copy = array
let index = randomIndex(fromArray: copy)
let fact = copy[index]
copy.remove(at: index)
billAndSteveFacts[person] = copy.isEmpty ? nil : copy
return fact
}
func createFacts() {
let steveFacts = [
"He took a calligraphy course, which he says was instrumental in the future company products' attention to typography and font.", "Shortly after being shooed out of his company, he applied to fly on the Space Shuttle as a civilian astronaut (he was rejected) and even considered starting a computer company in the Soviet Union.", "He actually served as a mentor for Google founders Sergey Brin and Larry Page, even sharing some of his advisers with the Google duo.", "He was a pescetarian, meaning he ate no meat except for fish."
]
let billFacts = [
"He aimed to become a millionaire by the age of 30. However, he became a billionaire at 31.", "He scored 1590 (out of 1600) on his SATs.", "His foundation spends more on global health each year than the United Nation's World Health Organization.", "The private school he attended as a child was one of the only schools in the US with a computer. The first program he ever used was a tic-tac-toe game.", "In 1994, he was asked by a TV interviewer if he could jump over a chair from a standing position. He promptly took the challenge and leapt over the chair like a boss."
]
billAndSteveFacts["Bill Gates"] = billFacts
billAndSteveFacts["Steve Jobs"] = steveFacts
}
}
// MARK: - Helper Methods
extension ViewController {
func randomIndex(fromArray array: [String]) -> Int {
return Int(arc4random_uniform(UInt32(array.count)))
}
func randomPerson() -> String {
let randomNumber = arc4random_uniform(2)
return randomNumber == 0 ? "Steve Jobs" : "Bill Gates"
}
}