This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.m
242 lines (201 loc) · 8.03 KB
/
Controller.m
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// Controller.m
// Quizzer
//
// Created by xcode on 2014-03-02.
// Copyright (c) 2014 xcode. All rights reserved.
//
#import "Controller.h"
@implementation Controller
@synthesize buttons;
NSMutableArray* quizArray = nil;
int currIndex = 0;
int selectedDifficulty = 3;
int questionNumber = 0;
int guesses = 0;
bool rareCharacters = false;
bool currentAnswerIsWrong = false;
int wrongAnswers = 0;
const int NUMBER_OF_QUESIONS = 10;
-(IBAction)answerSelected:(UIButton*)sender {
NSString* answer = [sender currentTitle];
if (!quizArray) {
quizArray = [self makeQuiz];
}
if([self checkAnswer:quizArray curr_index:currIndex answer:answer]) {
[feedback setTextColor:[UIColor greenColor]];
[feedback setText:@" You got it!"];
if (questionNumber < NUMBER_OF_QUESIONS) {
currIndex = [self populateQuiz:quizArray];
[questions setText:[NSString stringWithFormat:@"Question %d out of %d", ++questionNumber, NUMBER_OF_QUESIONS]];
}
currentAnswerIsWrong = false;
} else if (!questionNumber) {
[questions setText:[NSString stringWithFormat:@"Question %d out of %d", ++questionNumber, NUMBER_OF_QUESIONS]];
currIndex = [self populateQuiz:quizArray];
} else {
[feedback setTextColor:[UIColor redColor]];
[feedback setText:@"Wrong! Try again!"];
if (!currentAnswerIsWrong) {
currentAnswerIsWrong = true;
wrongAnswers++;
}
}
if (questionNumber > NUMBER_OF_QUESIONS && !currentAnswerIsWrong) {
[self displayStats];
questionNumber = 0;
quizArray = nil;
guesses = 0;
currIndex = 0;
}
guesses++;
}
- (void)displayStats {
double percent = ((NUMBER_OF_QUESIONS - wrongAnswers) / NUMBER_OF_QUESIONS) * 100;
NSString *result = [NSString stringWithFormat:@"%d guesses, %.2lf%%", guesses, percent];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Results"
message:result
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
-(NSMutableArray*)makeQuiz {
NSMutableArray *innerArray = [[NSMutableArray alloc] initWithCapacity:2];
NSMutableArray *outerArray = [[NSMutableArray alloc] init];
[innerArray addObject:[UIImage imageNamed:@"blue_bomb.png"]];
[innerArray addObject:@"Blue Bomb"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"fire_chomp.png"]];
[innerArray addObject:@"Fire Chomp"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"goomba.png"]];
[innerArray addObject:@"Goomba"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"lady_bow.png"]];
[innerArray addObject:@"Lady Bow"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"nibbles.png"]];
[innerArray addObject:@"Nibbles"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"sombrero_guy.png"]];
[innerArray addObject:@"Shy Guy"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"starlow.png"]];
[innerArray addObject:@"Starlow"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"sylvia.png"]];
[innerArray addObject:@"Sylvia"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"toadette.png"]];
[innerArray addObject:@"Toadette"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"vivian.png"]];
[innerArray addObject:@"Vivian"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"watt.png"]];
[innerArray addObject:@"Watt"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"bomb_fish.png"]];
[innerArray addObject:@"Bomb Fish"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"bombette.png"]];
[innerArray addObject:@"Bombette"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"ember.png"]];
[innerArray addObject:@"Ember"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"jerry_the_bob_omb.png"]];
[innerArray addObject:@"Jerry the Bomb"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"kersti.png"]];
[innerArray addObject:@"Kersti"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"kirby.png"]];
[innerArray addObject:@"Kirby"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"misstar.png"]];
[innerArray addObject:@"Miss Star"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
[innerArray addObject:[UIImage imageNamed:@"time_bob_omb.png"]];
[innerArray addObject:@"Time Bomb"];
[outerArray addObject:[innerArray copy]];
[innerArray removeAllObjects];
return outerArray;
}
-(int)populateQuiz:(NSMutableArray*)questionsArray {
int index = arc4random() % [questionsArray count];
[imageView setImage:[[questionsArray objectAtIndex:index] objectAtIndex:0]];
NSMutableArray* answers = [[NSMutableArray alloc] initWithObjects:[[questionsArray objectAtIndex:index] objectAtIndex:1], nil];
int count = 1;
for (NSMutableArray* question in questionsArray) {
if (count >= selectedDifficulty)
break;
if (![[question objectAtIndex:1] isEqual:[answers objectAtIndex:0]]) {
count++;
[answers addObject:[question objectAtIndex:1]];
}
}
[self shuffle:answers];
for (int position = 0; position < [buttons count]; position++) {
if (position < selectedDifficulty) {
[[buttons objectAtIndex:position] setHidden:false];
[[buttons objectAtIndex:position] setTitle:[answers objectAtIndex:position] forState: UIControlStateNormal];
} else {
[[buttons objectAtIndex:position] setHidden:true];
}
}
return index;
}
-(BOOL)checkAnswer:(NSMutableArray*)questionsArray curr_index:(int)index answer:(NSString*)user_answer {
if ([[[questionsArray objectAtIndex:index] objectAtIndex:1] isEqual:user_answer]) {
[questionsArray removeObjectAtIndex:index];
return true;
}
return false;
}
- (void)shuffle:(NSMutableArray*)questionsArray {
NSUInteger count = [questionsArray count];
for (NSUInteger i = 0; i < count; ++i) {
NSInteger nElements = count - i;
NSInteger n = arc4random_uniform(nElements) + i;
[questionsArray exchangeObjectAtIndex:i withObjectAtIndex:n];
}
}
-(IBAction)difficultySelected:(UISegmentedControl*)sender {
switch(sender.selectedSegmentIndex) {
default:
case 0:
selectedDifficulty = 3;
break;
case 1:
selectedDifficulty = 6;
break;
case 2:
selectedDifficulty = 9;
break;
}
}
-(IBAction)gameModeSelected:(UISegmentedControl*)sender {
rareCharacters = [sender selectedSegmentIndex];
}
@end