Object position ranking
I am making a small quiz app, but I have several problems.
I randomly ask questions from NSMutableArray using arc4random (), then I populate the view with three buttons, one containing the correct answer and the other 2 containing two incorrect answers
what I need to do is randomize the X coordinate (position) of the 3 buttons in the view, this is the code I am using, but this creates problems as it does not work as expected and the application is frequently reset when the action is called:
NSBundle *bundle02 = [NSBundle mainBundle];
NSString *textFilePath02 = [bundle02 pathForResource:@"possiblePositions" ofType:@"txt"];
NSString *fileContents02 = [NSString stringWithContentsOfFile:textFilePath02 encoding:NSUTF8StringEncoding error:nil];
arrayPossiblePositions = [[NSMutableArray alloc] initWithArray:[fileContents02 componentsSeparatedByString:@"\n"]];
int length02 = [arrayPossiblePositions count];
int chosen02 = arc4random() % length02;
[arrayPossiblePositions removeObjectAtIndex:chosen02];
int chosen04 = arc4random() % length02;
[arrayPossiblePositions removeObjectAtIndex:chosen04];
int chosen05 = arc4random() % length02;
if ([questionString isEqualToString:@"question1"]) {
buttonCorrect = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize = CGRectMake(chosen02, 80, 130, 130);
buttonCorrect.frame = newSize;
[buttonCorrect setImage:[UIImage imageNamed:@"kncpf.png"] forState:UIControlStateNormal];
[buttonCorrect addTarget:self action:@selector(answerCorrect) forControlEvents:UIControlEventTouchUpInside];
[main addSubview:buttonCorrect];
buttonUncorrect01 = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize02 = CGRectMake(chosen04, 80, 130, 130);
buttonUncorrect01.frame = newSize02;
[buttonUncorrect01 setImage:[UIImage imageNamed:@"kncpf02.png"] forState:UIControlStateNormal];
[buttonUncorrect01 addTarget:self action:@selector(answerUncorrect) forControlEvents:UIControlEventTouchUpInside];
[main addSubview:buttonUncorrect01];
buttonUncorrect02 = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize034578 = CGRectMake(chosen05, 80, 130, 130);
buttonUncorrect02.frame = newSize034578;
[buttonUncorrect02 setImage:[UIImage imageNamed:@"kncpf034578.png"] forState:UIControlStateNormal];
[buttonUncorrect02 addTarget:self action:@selector(answerUncorrect) forControlEvents:UIControlEventTouchUpInside];
[main addSubview:buttonUncorrect02];
}
Can you suggest that I do something different because I am really going crazy?
Thanks in advance for your answers, David
a source to share
I really needed to do something like this, but instead of moving images around, I decided to do this instead:
- Create three buttons where you want them to appear (given locations).
- Assign images to each button arbitrarily (by randomizing NSMutableArray with NSStrings of image names).
- Instead of assigning @selector (answerCorrect) and @selector (answerUncorrect) assign @selector (checkIfCorrect :)
- Define checkIfCorrect as such:
- (void) checkIfCorrect: (id) sender {UIImage * buttonImage = sender.image;
if (buttonImage == [UIImage imageNamed: @ "kncpf.png"]) {[self answerCorrect]; } else {[self answerIncorrect]; }
}
CHANGE THE CODE WHICH I RECOMMEND:
Also, you call
int length02 = [arrayPossiblePositions count];
int chosen02 = arc4random() % length02;
[arrayPossiblePositions removeObjectAtIndex:chosen02];
int chosen04 = arc4random() % length02;
[arrayPossiblePositions removeObjectAtIndex:chosen04];
int chosen05 = arc4random() % length02;
Note that length02 remains unchanged, but arrayPossiblePositions is resized. This is probably the first reason your code is failing: you are trying to remove an index from an array that is outside the array!
I haven't tested but should work: (don't forget to define checkanswer () as I mentioned above)
NSMutableArray *imagesArray = [NSMutableArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil];
int count1 = [imagesArray count];
int index1 = arc4random() % count1;
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize = CGRectMake(30, 80, 130, 130);
button1.frame = newSize;
[button1 setImage:[UIImage imageNamed:[imagesArray objectAtIndex:index1]] forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
[imagesArray removeObjectAtIndex:index1];
int count2 = [imagesArray count];
int index2 = arc4random() % count2;
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize = CGRectMake(160, 80, 130, 130);
button2.frame = newSize;
[button2 setImage:[UIImage imageNamed:[imagesArray objectAtIndex:index2]] forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
[imagesArray removeObjectAtIndex:index2];
int count3 = [imagesArray count];
int index3 = arc4random() % count3;
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize = CGRectMake(290, 80, 130, 130);
button3.frame = newSize;
[button3 setImage:[UIImage imageNamed:[imagesArray objectAtIndex:index3]] forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button3];
a source to share
I think arc4random () needs upper bounds. Not sure if using a module is correct here, or rather a semantic error, not sure how an arc reacts when you try to perform an operation like this without setting its upper bounds first. I'm not sure, try subbing on a hard value instead of your length02 and see if you get the expected behavior then lean back.
a source to share