0

OK I'll try and make this not as confusing as the title.

I have 4 UIImageViews, view1, view1, view3 and view4. And I need to randomize them and randomize the count, based off a certain difficulty and level deep a player is, in a game I'm working on.

So I have an array, say, [currentSequence] and it contains those 4 uiimageviews. Well, I need to be able to totally randomize it. Say, this can be what's in it. vie1, view1, vie3, view3, view3, view4, view4, view1, view1, view2, etc etc.

It's a memory matching game. So order is important. I need these objects randomized once and to hold, until I wipe them out when the player enters a new round and a new set made.

I also plan on having a limited number of sequences in a round, like 6. So if we're at sequence 1, the views that will appear can be, view1, view2, view2, view4. Then sequence two starts with the same order, but it will add a few more objects.

Think the old game, Simon.

Right now I just need a solution for randomizing the count and objects, for a limited set of objects (4).

Thanks.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Mike S
  • 101
  • 11

1 Answers1

0

Try something like this:

NSUInteger capacity = 20;
NSArray *objects = @[@(1), @(2), @(3), @(4)];
NSMutableArray *randomized = [NSMutableArray arrayWithCapacity:capacity];

for (int i = 0; i < capacity; i++) {
    NSUInteger rnd = floor(arc4random() % 4);
    randomized[i] = objects[rnd];
}

But that doesn't randomize the amount of objects placed in the randomized array.

To randomize that as well, try this:

NSArray *objects = @[@(1), @(2), @(3), @(4)];
NSMutableArray *randomized = [@[] mutableCopy];
NSUInteger terminationUnlikeliness = 10; /* increase to make termination less likely */

for (int i = 0; floor(arc4random() % terminationUnlikeliness) != 0; i++) {
    NSUInteger rnd = floor(arc4random() % 4);
    randomized[i] = objects[rnd];
}
Simon M
  • 2,931
  • 2
  • 22
  • 37
  • I tried the latter solution with randomization and that did randomize, but I think I may not have been totally clear. I am working with only 4 UIImageViews, but not only do I need to randomize them, but I need to multiply (randomly) the number of references to those ImageViews, in the array. So I have view1, view2, view3 and view4. I need this to be totally randomized to a pre-determined number. I plan on incrementally increase that number based off the round the user is in. So if I'm in round 1, have a cap of say, 20 views in that array. Round 2? Make it 30, etc etc. – Mike S Jun 26 '13 at 15:30
  • So these 4 views, need to be repeated, in a random fashion. So say, in round 1, in the array, it's view1, view2, view3,view3,view3, view1,view1,view4,view4,view4,view2,view2,view2,view4, etc. etc. Then the next round, I'll need more of those. But in between rounds, I plan on having a set of 6 sequences. Each sequence will also add, say, X views to be highlighted, for the user to replicate tapping, in proper order. Thanks. I hope this helps. – Mike S Jun 26 '13 at 15:35
  • I think I've come up with a solution. I'll try it later-ish and hopefully update and close this out with my solution, if it works – Mike S Jun 26 '13 at 19:53
  • @MikeS Good stuff, hope your solution works :) – Simon M Jun 26 '13 at 22:12