0

I am kind of in a pickle here since I have tried many things to accomplish this and have failed.

What I want to do: I have a NSString in my app which is the answer to a question to a round in a quiz. My answers are stored in a NSMutableArray. These answers are chosen at random. I have 4 buttons in my app that are options (possible answers) for the round. I don't want to have multiple answers!

What I have tried:

  1. I have tried deleting the answer after storing it in one of the buttons so that it couldn't be chosen again but it ended up causing a crash since I was trying to delete a object in the array while using fast enumeration (for loop).
  2. I have tried just detecting if the button title was equal to to the answer after setting the correct answer to a specific button but that didn't work for some odd reason (no crash). There will still multiple buttons with the same answer

What I need help with: How should I stop the answer from being in multiple buttons so that the quiz isn't showing the obvious answer?

What should I do instead?

Thanks!

Edit1: So I pretty much made a NSArray of my four UIButtons.

I put the answer into a random UIButton like this:

NSInteger chosen = (arc4random() % 4);
UIButton *randButton = (UIButton *)[buttonArray objectAtIndex:chosen];
[randButton setTitle:imageName forState:UIControlStateNormal];

Then I title the other buttons like so, my buttons that do not have the answer have a title of nothing so I do this:

- (void)titleButtons {
    for (UIButton *buttons in buttonArray) {
        if ([[buttons titleForState:UIControlStateNormal] == nil]) {
            UIButton *button = buttons;
            NSString *superheroString = (NSString*)[superheroArray objectAtIndex:(arc4random() % [superheroArray count])];
            [button setTitle:superheroString forState:UIControlStateNormal];
            [self checkTitles:button];
        }
    }

Then the checkTitle method looks like this, this is the method where I try to make sure where 2 buttons do not have the same answer as the imageName (answer):

- (void)checkTitles:(UIButton*)button {
    if ([[button titleForState:UIControlStateNormal] isEqualToString:imageName]) {
        //Duplicate Answer so re-title button
        NSString *newTitle = [superheroArray objectAtIndex:(arc4random() % [superheroArray count])];
        [button setTitle:newTitle forState:UIControlStateNormal];
        //Call same method again to see if new title is still same answer as pic to avoid same answers
        [self checkTitles:button];
    }
}
}
8
  • Maybe you just want to shuffle the array? Commented Dec 11, 2011 at 6:04
  • How is that different from picking a random index then getting the object from that index? Commented Dec 11, 2011 at 6:17
  • You can't guarantee the random index is unique. Commented Dec 11, 2011 at 6:25
  • So shuffling will guarantee it? Commented Dec 11, 2011 at 6:29
  • No, shuffling make you no need to use random indices (assuming that NSMutableArray has length 4 corresponding to the 4 answers you'll show). Commented Dec 11, 2011 at 6:31

2 Answers 2

1

If you have an NSArray with all your answers, and you want 1 correct answer and 3 different wrong answers, you can do the following:

  1. Decide in which button do you want the right answer. (randomize it)
  2. Get a random answer from your array and store the index of that answer in a temporary array.
  3. Get another random answer and make sure you are not picking an answer with the same index as the one you have in your temp array, (and again, store the new index in the temporary array)
Sign up to request clarification or add additional context in comments.

2 Comments

#1 was what I am currently doing which isn't working. So maybe #2 and #3 combined will work for me, ill try it out later on! :)
You should also save on another array the buttons where you already have an answer...
0

Don't compare strings using ==. This compares pointer addresses. Use isEqualToString: instead:

if (stringA == stringB)

Usually won't work (will for some constant strings)

if ([stringA isEqualToString:stringB])

Will always work.

7 Comments

in the second code snippet is that what you mean? Were there any other instances of this issue?
Yes, when you check the button title.
Ok check now, I tried that and 3 of my buttons then don't get titled!
I'm not sure you should have removed the == nil from titleButtons. isEqualToString won't match in that case.
ok i edited it back. But where else now should I do isEqualToString? I don't see any more places where I can do that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.