Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ios beginner

macrumors newbie
Original poster
May 5, 2011
5
0
Hello,

I'm teaching myself to do basic Ios programming. I'm working on a project in a book like this:

There is a collection of pictures on one wheel, and then the names for the pictures on the second wheel. The game is to match the picture with the name. It's using the slot-machine like "Picker View" device. The picture set and the name set are each in their own array. Then there is a compare method set up to see if they match.

The problem is that the "randomization" method is very obvious. 0 on wheel-one matches with 9 on wheel-two, 1 with 8, 2 with 7, etc. A person just has to see that the wheels go in opposite directions on the Picker slot machine wheels.


How can I mix the stuff up so it's not so obvious? I don't need it to be different each time. And I don't need it to be done "programmatically" using a sophisticated math equation. Which ever way is simplest to code from a new-to-programming standpoint. It just needs to be scrambled up enough so a person couldn't guess the pattern.

If there is some way to set the arrays up so that they are scrambled more, that would be great. So instead of the pairs being like this 0/9, 1/8, 2/7, 3/6... it would be more like this: 0/4, 1/2, 3/9, 4/3.

That way it's not like "the furthest up on wheel one matches the furthest down one wheel two... the second-furthest up on wheel one matches the second-furthest down on wheel two.

Because that is too obvious.

I would like to read about how to make less-predictable matching patterns on the wheels. Right now, the left-hand wheel displays images, and the right-hand displays text strings. But I could make a set of images for the right-hand wheel instead of using text strings (if that somehow makes it easier to code a better scramble).
 
Last edited:

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
It's called a shuffle algorithm. See for instance:

http://en.wikipedia.org/wiki/Fisher–Yates_shuffle

Fill an NSMutableArray with objects and then shuffle it like this:

Code:
// Fisher-Yates shuffle
for (int i = [aMutableArray count]; i > 1; i--)
    [aMutableArray exchangeObjectAtIndex:i-1 withObjectAtIndex:random()%i];  // introduces modulo bias (see below)
 

ios beginner

macrumors newbie
Original poster
May 5, 2011
5
0
Thanks,

That looks like some advanced stuff that professionals use. But I'm not sure that is what I'm looking for.

Picture this:
There are two wheels on the Picker device on the iphone.

They each have 10 items.

Those items never change. You can turn the phone off and on, and they stay the same.

They don't delete or mutate. All 10 on both wheels always stay there.

What I want to do is map the left wheel to the right wheel in a scrambled way. Not a randomly generated way...a scrambled way. I said the word "random" in the title of the post. But forget the word "random". I mean a non-obvious matching pattern. This pattern does not have to be randomly created.

The match sequence I have right now is this:
0left matches 9right
1left matches 8right
2left matches 7right
3left matches 4right
4left matches 5right
5left matches 4right
6left matches 3right
7left matches 2right
8left matches 1right
9left matches 0right

The way the match algorithm is set up is basically like this: "add up the index array number of the left wheel with that of the right wheel. if it adds up to 9, you have a match".

But I want something more scrambled. Like this:
0left matches 2right
1left matches 5right
2left matches 1right
3left matches 4right
4left matches 7right
5left matches 3right
6left matches 8right
7left matches 9right
8left matches 1right
9left matches 0right

So for example: if the person selected the 5th object on the left wheel (4left), they would have to also select the 8th object on the right wheel (7right) to have it match.
 

Shawnpk

macrumors 6502
Jan 13, 2011
350
0
Los Angeles, CA
Just add them to the arrays in the order you want them to match and pull them out in the same order. Not really sure why you want the same order evey time though.
 

ios beginner

macrumors newbie
Original poster
May 5, 2011
5
0
Thanks for your reply.

I don't know what "pull them out in the same order" means in coding terms. (I'm very new at this!).

I've heard of something called a "switch statement".

Maybe I could create 10 separate switch statements?
 

wlh99

macrumors 6502
Feb 7, 2008
272
0
Thanks,


The way the match algorithm is set up is basically like this: "add up the index array number of the left wheel with that of the right wheel. if it adds up to 9, you have a match".

Why not 10 if statements?
If the index on the left is 0 and on the right is 9 you have a match.
If the index on the left is 1 and on the right is 5 you have a match.
etc.

A way to do that would be to start by setting a BOOL set to false. Then have the ten if statements check for a match and set the BOOL to true if a match is found. Then have an 11th if statement check if the BOOL is true, do whatever needs done for a match.

There are better(I disagree with this phrase) "advanced stuff that professionals use" but I think 10 if statements is what you are looking for.
 

Shawnpk

macrumors 6502
Jan 13, 2011
350
0
Los Angeles, CA
Thanks for your reply.

I don't know what "pull them out in the same order" means in coding terms. (I'm very new at this!).

I've heard of something called a "switch statement".

Maybe I could create 10 separate switch statements?

You will have to write an "if" or "switch" statement, but that won't help you load your arrays. How are you loading your array into the picker?
 

ios beginner

macrumors newbie
Original poster
May 5, 2011
5
0
The arrays are being loaded in using [NSArray alloc]initWithObjects.

I think maybe a combination of if/else statements and a switch statement could get the ability to scramble things up well. I'm brand new to this, so I'll have to work with it a bit.
 

wlh99

macrumors 6502
Feb 7, 2008
272
0
The arrays are being loaded in using [NSArray alloc]initWithObjects.

I think maybe a combination of if/else statements and a switch statement could get the ability to scramble things up well. I'm brand new to this, so I'll have to work with it a bit.

Be clear. By "scramble" do you intend to change the order of the objects in the array? It sounded before like you loaded the array in a pre-determined order (but not necessarily 1-10). What you needed wasn't a way to reorder them, but to check that the correct matches were made against another picker.

If you truly are not changing the order this is very simple, no else statements or switch statements are needed.

similar to this:
Code:
BOOL match = FALSE;
if ((picker1 == 0) && (picker2 == 9)) match = TRUE;
if ((picker1 == 1) && (picker2 == 5)) match = TRUE;
etc..

if (match = TRUE) {
     do whatever you want here;
}
 
Last edited by a moderator:

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
If you truly are not changing the order this is very simple, no else statements or switch statements are needed.

similar to this:
Code:
BOOL match = FALSE;
if ((picker1 == 0) && (picker2 == 9)) match = TRUE;
if ((picker1 == 1) && (picker2 == 5)) match = TRUE;
etc..

if (match = TRUE) {
     do whatever you want here;
}
You don't need to use else statements, but using them makes the code more efficient. Once you have a match you don't need to check any further possible conditions for a match, so the else helps you skip them.

Similar to this:
Code:
BOOL match = FALSE;
if ((picker1 == 0) && (picker2 == 9)) match = TRUE;
else if ((picker1 == 1) && (picker2 == 5)) match = TRUE;
etc..

if (match [COLOR="Red"]==[/COLOR] TRUE) {
     do whatever you want here;
}

P.S. Missed the problem with checking the match at the end. Be careful there.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.