@@ -21,6 +21,7 @@ enum Status {
21
21
22
22
class GameViewController : UIViewController {
23
23
24
+ // MARK: Variables
24
25
var words : [ String ] !
25
26
var flags = [ Bool] ( ) // true = done with that word
26
27
var timer : Timer !
@@ -39,6 +40,8 @@ class GameViewController: UIViewController {
39
40
return count % words. count
40
41
}
41
42
43
+
44
+ // MARK: Constants
42
45
let green = UIColor . init ( red: 180 / 255.0 , green: 232 / 255.0 , blue: 112 / 255.0 , alpha: 1.0 )
43
46
let red = UIColor . init ( red: 255 / 255.0 , green: 69 / 255.0 , blue: 69 / 255.0 , alpha: 1.0 )
44
47
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 {
49
52
let motionUpdateInterval = 0.3
50
53
let timerTolerance = 0.05
51
54
52
-
55
+ // MARK: Outlets
53
56
@IBOutlet weak var timerLabel : UILabel !
54
57
@IBOutlet weak var wordLabel : UILabel !
55
58
59
+
60
+ // MARK: UIViewController overrides
56
61
override func viewDidLoad( ) {
57
62
super. viewDidLoad ( )
58
63
@@ -77,6 +82,8 @@ class GameViewController: UIViewController {
77
82
status = . readyToBegin
78
83
}
79
84
85
+ // MARK: Motion
86
+
80
87
func motionUpdate( roll: Double , pitch: Double ) {
81
88
if pitch > - 0.2 && pitch < 0.2 {
82
89
if roll > 1.2 && roll < 1.9 {
@@ -86,54 +93,40 @@ class GameViewController: UIViewController {
86
93
} else if status == . readyToContinue {
87
94
nextWord ( )
88
95
}
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 ( )
99
100
}
100
101
}
101
102
102
103
}
104
+
105
+ // MARK: Actions
103
106
104
107
@IBAction func back( ) {
105
- timer ? . invalidate ( )
108
+ endGame ( )
106
109
dismiss ( animated: false , completion: nil )
107
110
}
108
-
109
- func setupFlags( ) {
110
- for _ in words {
111
- flags. append ( false )
112
- }
113
- }
114
-
111
+
115
112
func startGame( ) {
116
113
status = . getReady
117
114
// start timer, display first word, change status to display
118
115
timerLabel. text = String ( gameTime)
119
116
timer = Timer . scheduledTimer ( timeInterval: 1 , target: self , selector: #selector( GameViewController . timeUpdate) , userInfo: nil , repeats: true )
120
117
timer. tolerance = timerTolerance
121
118
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)
127
121
timerLabel. isHidden = false
122
+ view. backgroundColor = blue
128
123
}
129
124
130
125
func showFirstWord( ) {
131
126
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 )
134
129
view. backgroundColor = UIColor . white
135
- timerLabel. textColor = UIColor . lightGray
136
- timerLabel. text = String ( gameTime)
137
130
}
138
131
139
132
func nextWord( ) {
@@ -142,21 +135,18 @@ class GameViewController: UIViewController {
142
135
count += 1 ;
143
136
print ( index)
144
137
} while ( flags [ index] )
145
- wordLabel. text = words [ index]
146
- wordLabel. textColor = UIColor . black
138
+ configureWord ( text: words [ index] , color: UIColor . black)
147
139
view. backgroundColor = UIColor . white
148
140
}
149
141
150
142
func correct( ) {
151
143
correctSoundPlayer. play ( )
152
- wordLabel. text = " Correct "
153
- wordLabel. textColor = UIColor . white
144
+ configureWord ( text: " Correct " , color: UIColor . white)
154
145
view. backgroundColor = green
155
146
flags [ index] = true
156
147
score += 1
157
148
if score == words. count {
158
- timer. invalidate ( )
159
- status = . end
149
+ endGame ( )
160
150
showResult ( )
161
151
} else {
162
152
status = . readyToContinue
@@ -166,12 +156,28 @@ class GameViewController: UIViewController {
166
156
func pass( ) {
167
157
incorrectSoundPlayer. play ( )
168
158
status = . readyToContinue
169
- wordLabel. text = " Pass "
170
- wordLabel. textColor = UIColor . white
159
+ configureWord ( text: " Pass " , color: UIColor . white)
171
160
view. backgroundColor = red
172
161
}
173
162
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
175
181
func timeUpdate( ) {
176
182
if prepareTime > 0 {
177
183
prepareTime -= 1
@@ -184,24 +190,28 @@ class GameViewController: UIViewController {
184
190
print ( gameTime)
185
191
timerLabel. text = String ( gameTime)
186
192
if gameTime == 0 {
187
- timer. invalidate ( )
188
- status = . end
193
+ endGame ( )
189
194
showResult ( )
190
195
}
191
196
}
192
197
}
193
198
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 )
200
203
}
201
- alert. addAction ( quitAction)
202
- present ( alert, animated: true , completion: nil )
203
204
}
204
205
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
+ }
205
215
206
216
}
207
217
0 commit comments