Reputation: 59
I have this code:
while (!found) {
char letterGuess=(char)([[[wordList letterRank:lengthOfWord]objectAtIndex:yescount]intValue]+97);
NSString *stringLetter=[NSString stringWithFormat:@"%c",letterGuess];
if ([prevGuess count]<=0) {
alreadyGuessed=NO;
}else {
if ([prevGuess containsObject:stringLetter]) {
alreadyGuessed=YES;
yescount++;
}else{
alreadyGuessed=NO;
}
}
if (!alreadyGuessed) {
[prevGuess addObject:stringLetter];
[self drawYesNo];
NSLog(@"%c",letterGuess);
}
if (userAnswer) {
}else {
[self noGuess:letterGuess];
}
if ([wordList count]<=1) {
NSLog(@"word found");
found=YES;
}
}
basically, it takes a letter from a sorted array, checks it against another array containing all the previous letters that were entered, and if it is not in that array, it will call [self drawYesNo], which basically sets up and draws a yes and a no UIButton, and based on what the user presses, changes the 'userAnswer' variable.
My problem is that this loop executes so quickly that the objectAtIndex quickly exceeds the bounds of the array, and throws an error. I need some way to pause during the [drawYesNo] method, and allow the user to actually make a decision.
I know there are answers to similar questions on StackOverflow, but I just can't make heads or tails of them. Can someone please explain this for a very new OO programmer?
Upvotes: 0
Views: 383
Reputation: 104082
I can't really figure out all the logic of your code, but it sounds like you need to refactor it into different methods. Instead of having the code in a while loop, you should have one method that does most (all? I can't tell) of what you posted (without the while loop). You call this once when your app starts, then after the user clicks a button -- in the button's action method you change the value of userAnswer, and then call that method again. This keeps going until some condition is met in your button method that would cause it to stop.
Upvotes: 1