Skip to content

Commit 95ec664

Browse files
committed
Add comments, shorten the code
1 parent 7291701 commit 95ec664

File tree

2 files changed

+70
-52
lines changed

2 files changed

+70
-52
lines changed

Guess.xcodeproj/project.pbxproj

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,12 @@
6262
51178D531ED0CD9C0004CF59 /* Guess */ = {
6363
isa = PBXGroup;
6464
children = (
65-
51178D541ED0CD9C0004CF59 /* AppDelegate.swift */,
6665
51178D6A1ED0D8190004CF59 /* ArrayExtension.swift */,
6766
51178D661ED0D1400004CF59 /* MenuViewController.swift */,
6867
51178D681ED0D37C0004CF59 /* GameViewController.swift */,
69-
51178D6C1ED20AB10004CF59 /* Sounds */,
7068
51178D581ED0CD9C0004CF59 /* Main.storyboard */,
71-
51178D5B1ED0CD9C0004CF59 /* Assets.xcassets */,
72-
51178D5D1ED0CD9C0004CF59 /* LaunchScreen.storyboard */,
73-
51178D601ED0CD9C0004CF59 /* Info.plist */,
69+
51178D6C1ED20AB10004CF59 /* Sounds */,
70+
51178D711ED22E930004CF59 /* Supporting Files */,
7471
);
7572
path = Guess;
7673
sourceTree = "<group>";
@@ -84,6 +81,17 @@
8481
name = Sounds;
8582
sourceTree = "<group>";
8683
};
84+
51178D711ED22E930004CF59 /* Supporting Files */ = {
85+
isa = PBXGroup;
86+
children = (
87+
51178D541ED0CD9C0004CF59 /* AppDelegate.swift */,
88+
51178D5B1ED0CD9C0004CF59 /* Assets.xcassets */,
89+
51178D5D1ED0CD9C0004CF59 /* LaunchScreen.storyboard */,
90+
51178D601ED0CD9C0004CF59 /* Info.plist */,
91+
);
92+
name = "Supporting Files";
93+
sourceTree = "<group>";
94+
};
8795
/* End PBXGroup section */
8896

8997
/* Begin PBXNativeTarget section */

Guess/GameViewController.swift

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ enum Status {
2121

2222
class GameViewController: UIViewController {
2323

24+
// MARK: Variables
2425
var words: [String]!
2526
var flags = [Bool]() // true = done with that word
2627
var timer: Timer!
@@ -39,6 +40,8 @@ class GameViewController: UIViewController {
3940
return count % words.count
4041
}
4142

43+
44+
// MARK: Constants
4245
let green = UIColor.init(red: 180/255.0, green: 232/255.0, blue: 112/255.0, alpha: 1.0)
4346
let red = UIColor.init(red: 255/255.0, green: 69/255.0, blue: 69/255.0, alpha: 1.0)
4447
let blue = UIColor.init(red: 56/255.0, green: 189/255.0, blue: 255/255.0, alpha: 1.0)
@@ -49,10 +52,12 @@ class GameViewController: UIViewController {
4952
let motionUpdateInterval = 0.3
5053
let timerTolerance = 0.05
5154

52-
55+
// MARK: Outlets
5356
@IBOutlet weak var timerLabel: UILabel!
5457
@IBOutlet weak var wordLabel: UILabel!
5558

59+
60+
// MARK: UIViewController overrides
5661
override func viewDidLoad() {
5762
super.viewDidLoad()
5863

@@ -77,6 +82,8 @@ class GameViewController: UIViewController {
7782
status = .readyToBegin
7883
}
7984

85+
// MARK: Motion
86+
8087
func motionUpdate(roll: Double, pitch: Double) {
8188
if pitch > -0.2 && pitch < 0.2 {
8289
if roll > 1.2 && roll < 1.9 {
@@ -86,54 +93,40 @@ class GameViewController: UIViewController {
8693
} else if status == .readyToContinue {
8794
nextWord()
8895
}
89-
} else if roll > 2.35 && roll < 3.14 {
90-
// Correct
91-
if status == .display {
92-
correct()
93-
}
94-
} else if roll > 0 && roll < 0.78 {
95-
// Pass
96-
if status == .display {
97-
pass()
98-
}
96+
} else if roll > 2.35 && roll < 3.14 && status == .display {
97+
correct()
98+
} else if roll > 0 && roll < 0.78 && status == .display {
99+
pass()
99100
}
100101
}
101102

102103
}
104+
105+
// MARK: Actions
103106

104107
@IBAction func back() {
105-
timer?.invalidate()
108+
endGame()
106109
dismiss(animated: false, completion: nil)
107110
}
108-
109-
func setupFlags() {
110-
for _ in words {
111-
flags.append(false)
112-
}
113-
}
114-
111+
115112
func startGame() {
116113
status = .getReady
117114
// start timer, display first word, change status to display
118115
timerLabel.text = String(gameTime)
119116
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(GameViewController.timeUpdate), userInfo: nil, repeats: true)
120117
timer.tolerance = timerTolerance
121118

122-
wordLabel.text = "GET READY"
123-
wordLabel.textColor = UIColor.white
124-
view.backgroundColor = blue
125-
timerLabel.text = String(prepareTime)
126-
timerLabel.textColor = UIColor.white
119+
configureWord(text: "GET READY", color: UIColor.white)
120+
configureTimer(time: prepareTime, color: UIColor.white)
127121
timerLabel.isHidden = false
122+
view.backgroundColor = blue
128123
}
129124

130125
func showFirstWord() {
131126
status = .display
132-
wordLabel.text = words[index]
133-
wordLabel.textColor = UIColor.black
127+
configureWord(text: words[index], color: UIColor.black)
128+
configureTimer(time: gameTime, color: UIColor.lightGray)
134129
view.backgroundColor = UIColor.white
135-
timerLabel.textColor = UIColor.lightGray
136-
timerLabel.text = String(gameTime)
137130
}
138131

139132
func nextWord() {
@@ -142,21 +135,18 @@ class GameViewController: UIViewController {
142135
count += 1;
143136
print(index)
144137
} while (flags[index])
145-
wordLabel.text = words[index]
146-
wordLabel.textColor = UIColor.black
138+
configureWord(text: words[index], color: UIColor.black)
147139
view.backgroundColor = UIColor.white
148140
}
149141

150142
func correct() {
151143
correctSoundPlayer.play()
152-
wordLabel.text = "Correct"
153-
wordLabel.textColor = UIColor.white
144+
configureWord(text: "Correct", color: UIColor.white)
154145
view.backgroundColor = green
155146
flags[index] = true
156147
score += 1
157148
if score == words.count {
158-
timer.invalidate()
159-
status = .end
149+
endGame()
160150
showResult()
161151
} else {
162152
status = .readyToContinue
@@ -166,12 +156,28 @@ class GameViewController: UIViewController {
166156
func pass() {
167157
incorrectSoundPlayer.play()
168158
status = .readyToContinue
169-
wordLabel.text = "Pass"
170-
wordLabel.textColor = UIColor.white
159+
configureWord(text: "Pass", color: UIColor.white)
171160
view.backgroundColor = red
172161
}
173162

174-
163+
func showResult() {
164+
let title = score == words.count ? "Congratulations!" : "Time's up 😙"
165+
let message = "You scored \(score) points."
166+
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
167+
let quitAction = UIAlertAction(title: "Okay", style: .default) { action in
168+
self.dismiss(animated: false, completion: nil)
169+
}
170+
alert.addAction(quitAction)
171+
present(alert, animated: true, completion: nil)
172+
}
173+
174+
func endGame() {
175+
timer?.invalidate()
176+
motionManager?.stopDeviceMotionUpdates()
177+
status = .end
178+
}
179+
180+
// MARK: Timer callback
175181
func timeUpdate() {
176182
if prepareTime > 0 {
177183
prepareTime -= 1
@@ -184,24 +190,28 @@ class GameViewController: UIViewController {
184190
print(gameTime)
185191
timerLabel.text = String(gameTime)
186192
if gameTime == 0 {
187-
timer.invalidate()
188-
status = .end
193+
endGame()
189194
showResult()
190195
}
191196
}
192197
}
193198

194-
func showResult() {
195-
let title = score == words.count ? "Congratulations!" : "Time's up 😙"
196-
let message = "You scored \(score) points."
197-
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
198-
let quitAction = UIAlertAction(title: "Okay", style: .default) { action in
199-
self.dismiss(animated: false, completion: nil)
199+
// MARK: Other
200+
func setupFlags() {
201+
for _ in words {
202+
flags.append(false)
200203
}
201-
alert.addAction(quitAction)
202-
present(alert, animated: true, completion: nil)
203204
}
204205

206+
func configureWord(text: String, color: UIColor) {
207+
wordLabel.text = text
208+
wordLabel.textColor = color
209+
}
210+
211+
func configureTimer(time: Int, color: UIColor) {
212+
timerLabel.text = String(time)
213+
timerLabel.textColor = color
214+
}
205215

206216
}
207217

0 commit comments

Comments
 (0)