Reputation: 23
The view of my application displays a grid of 16 numbers. Every time a button is pressed I want the arrangement of the numbers to change, and be random every time. The application works by having 16 small UIImageViews, each with an image (the number) associated with it. At the moment I 'randomise' the arrangement by generating a random number and having that number correspond to a particular arrangement. Like this:
int number = rand() % 7;
if (number == 1) {
space1.image = three;
space2.image = ten;
space3.image = one;
space4.image = eight;
space5.image = two;
space6.image = five;
space7.image = thirteen;
space8.image = six;
space9.image = four;
space10.image = fifteen;
space11.image = sixteen;
space12.image = nine;
space13.image = fourteen;
space14.image = eleven;
space15.image = twelve;
space16.image = seven;
}
else if (number == 2) {
space1.image = ten;
................
.............etc
Obviously there are millions of possible arrangements and I am only capturing 7 of them here. Can anyone suggest a way of truly randomising the arrangement?
I hope I have been clear enough. Thanks in advance for any help.
George
Upvotes: 0
Views: 139
Reputation: 694
You could simply implement the Fisher-Yates shuffle algorithm.
Instead of having multiple combination hard-coded, you have a default combination and when you apply the algorithm on it, it gets transformed to a random permutation.
You could implement it this way for example :
NSMutableArray * space; // Array containing your UIImageViews
// Initial combination
[(UIImageView *)space[0] setImage:one];
[(UIImageView *)space[1] setImage:two];
...
[(UIImageView *)space[14] setImage:fifteen];
[(UIImageView *)space[15] setImage:sixteen];
[self fisherYatesShuffle:space];
The FisherYates Shuffle implementation would be:
- (void)fisherYatesShuffle:(NSArray *) array {
for(int i = array.count - 1; i >= 0; i++) {
int j = arc4random() % i;
[space exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
Upvotes: 1
Reputation:
Instead of making 16 identical instance variable, store the UIImageView
object in an array, the UIImage
objects in a mutable array, and randomize the elements of the image array:
NSMutableArray *imageViews, *images;
imageViews = [[NSMutableArray alloc] init];
images = [[NSMutableArray alloc] init];
for (int i = 0; i < 16; i++) {
UImageView *imageView = [[UIImageView alloc] initWithFrame:someFrame];
[imageViews addObject:imageView];
[imageView release];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"image_%d"], i];
[images addObject:image];
}
- (void)randomize
{
for (int i = 0; i < 100; i++) {
int idx1 = arc4random() % images.count;
int idx2 = arc4random() % images.count;
[images exchangeObjectAtIndex:idx1 withObjectAtIndex:idx2];
}
for (int i = 0; i < imagesViews.count) {
[[imageViews objectAtIndex:i] setImage:[images objectAtIndex:i]];
}
}
Upvotes: 1
Reputation: 1054
Hint in C, because this is not really specific to Obj-C or IDE you're using.
Image images[n];
numbers = {0, 1, 2, /* ... n - 1 */ };
shuffle(numbers);
Images pmut[n];
for (int i = 0; i < n; ++i) {
pmut[i] = images[numbers[i]];
}
Upvotes: 0