开发者

Problem with a simple game's logic, I have hit a wall, and don't know how to get over it...SOS!

Ok, so I picked up this iOS app开发者_运维知识库 dev project for something to work on on the side, and it is materializing into something that is uglier than I was anticipating. I have a simple simple game that I have built where I present a bit of text, and the user identifies what it is, by selecting one of four buttons.

Now, I reckon a pickerview here would be a lot easier, but for design purposes, I am going to try and get my way through it doing it programmatically with buttons. So let me take a second to walk you through where I am.

When the user gets to the game screen, I have passed in two different arrays, and a number. The first array contains 25 items of text, the second array contains 25 items of answers (that correspond with the first array as far as index goes), and then the number is the number of rounds the user wishes to play, with 25 being the max.

Once the view loads, I take the number of rounds they have selected, and send it off to a method that returns an array of that number size, shuffled using the arc4random() %roundNumber method that I use as a means to access the index of the array I pass in.

This works really well. I can effectively grab at random from the source array, and after I present it, I remove the index from the index array so as to prevent duplication, and life is good.

THE PROBLEM: The buttons. I don't know why, but I am having a real problem getting the buttons to populate randomly, and contain the correct answer as well. The main problem that I am experiencing is the fact that I can't figure out how to randomly assign a value to the button. Meaning, the buttons have names, button1, button2, button3, and button4. And because I am programmatically setting their titles, and I need to "hardcode" the value of the correct answer into a button, it turns out that the right answer button is the same every time.

This really just boils down to pure thinking and logic, and quite frankly at this point I am running low. I am using a counter variable to access and keep track of which spot in the index is the correct answer, but I just can't figure out the rest of the puzzle.

Below is what I have come up with...but I'm not too proud of it. I have just been retrying different things and now all I have is a mess:

    for(int i = 0; i < 4; i++){

            //digits is an array of size 4 that is populated and shuffled in a similar manner to above

            switch ([[digits objectAtIndex:i] intValue]) {
                case 0:
                    [button1 setTitle:[NSString stringWithFormat:@"%@",[self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]]forState: UIControlStateNormal];

                    NSLog(@"1 %@", [self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]);
                    break;
                case 1:
                    [button2 setTitle:[NSString stringWithFormat:@"%@",[self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]]forState: UIControlStateNormal];

                    NSLog(@"2 %@", [self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]);
                    break;
                case 2:
                    [button3 setTitle:[NSString stringWithFormat:@"%@",[self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]]forState: UIControlStateNormal];

                    NSLog(@" %@", [self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]);
                    break;
                case 3:
                                           //note this is where I hard code the correct value
                    [button4 setTitle:[self.refList objectAtIndex:counter] forState: UIControlStateNormal];

                    NSLog(@"4 %@", [self.refList objectAtIndex:[[arr1 objectAtIndex:i] intValue]]);
                    break;
                default:
                    break;
            }

    }

I really appreciate any help on this, and I look forward to having this mystery revealed.

Thanks!


Step 1 - create four buttons, set frames and background colors appropriately. Set tags on the four buttons 0, 1, 2, 3. Set all buttons to one action for touch up inside.

Step 2 - generate a random number from 0 to 3. Use that value as an index into the array to select a button to hold the correct answer. Set the correct answer as title. Set incorrect answers on the remaining buttons. (you might use NSSet's setWithArray to get all the buttons as a set - after removing the chosen one from the set, use allObjects to get an array of the remaining buttons)

Step 3 - A button is pressed - what is its tag? If it matches the random number, you have a winner.

Every UIView and subclass has a tag member. Setting the tag is easy:

myButton.tag = 0;

checking it is just as easy:

if(unknownButton.tag == randomNumber)
  // winner

You get all the buttons to trigger one action method so that's where the tag is checked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜